From 3d7299c1e490a6813de2b2cb450b738b4c7acdf9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 1 Mar 2022 14:25:24 +0100
Subject: [PATCH] =?UTF-8?q?New=20option=20=E2=80=9Cnon=5Fzero=E2=80=9D=20t?=
 =?UTF-8?q?o=20the=20=E2=80=9Cresid=E2=80=9D=20command?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When this option is given, the “resid” command only displays non-zero residuals
---
 doc/manual/source/the-model-file.rst          |  6 ++++
 matlab/resid.m                                | 34 +++++++++++--------
 preprocessor                                  |  2 +-
 tests/decision_rules/third_order/FV2011.mod   |  4 +--
 tests/ep/burnside.mod                         |  2 +-
 tests/example1_with_tags.mod                  |  2 +-
 tests/loglinear/example4_exp.mod              |  6 ++--
 tests/loglinear/example4_exp_histval.mod      |  4 +--
 tests/loglinear/example4_loglinear.mod        |  6 ++--
 .../loglinear/example4_loglinear_histval.mod  |  4 +--
 tests/risky_ss/jermann98.mod                  |  2 +-
 tests/risky_ss/jermann98_2.mod                |  2 +-
 tests/risky_ss/jermann98_3.mod                |  2 +-
 tests/second_order/burnside_1.mod             |  2 +-
 tests/second_order/ds1.mod                    |  2 +-
 tests/second_order/ds2.mod                    |  4 +--
 tests/simul/Solow_no_varexo.mod               |  2 +-
 17 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst
index 4f7e6cf85c..738448c7af 100644
--- a/doc/manual/source/the-model-file.rst
+++ b/doc/manual/source/the-model-file.rst
@@ -1928,6 +1928,12 @@ in this case ``initval`` is used to specify the terminal conditions.
     in the last ``initval`` or ``endval`` block (or the steady state
     file if you provided one, see :ref:`st-st`).
 
+    *Options*
+
+    .. option:: non_zero
+
+        Only display non-zero residuals.
+
 .. command:: initval_file (OPTIONS...);
 
     |br| In a deterministic setup, this command is used to specify a
diff --git a/matlab/resid.m b/matlab/resid.m
index 19d316b564..9bcb336a85 100644
--- a/matlab/resid.m
+++ b/matlab/resid.m
@@ -1,10 +1,10 @@
-function z = resid(junk)
-% function z = resid(junk)
+function z = resid(options_resid_)
+% function z = resid(options_resid_)
 %
 % Computes static residuals associated with the guess values.
 %
 % INPUTS
-%    junk:   dummy value for backward compatibility
+%    options_resid_:   options to resid
 %
 % OUTPUTS
 %    z:      residuals
@@ -12,7 +12,7 @@ function z = resid(junk)
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright (C) 2001-2020 Dynare Team
+% Copyright (C) 2001-2022 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -31,10 +31,7 @@ function z = resid(junk)
 
 global M_ options_ oo_
 
-tagname = 'name';
-if nargin && ischar(junk)
-    tagname = junk;
-end
+non_zero = isfield(options_resid_, 'non_zero') && options_resid_.non_zero;
 
 tags  = M_.equations_tags;
 istag = 0;
@@ -80,24 +77,31 @@ if nargout == 0
     ind = [];
     disp('Residuals of the static equations:')
     skipline()
+    any_non_zero_residual = false;
     for i=1:M_.orig_eq_nbr
         if abs(z(i+M_.ramsey_eq_nbr)) < options_.solve_tolf/100
             tmp = 0;
         else
             tmp = z(i+M_.ramsey_eq_nbr);
+            any_non_zero_residual = true;
         end
         if istag
             tg = tags(cell2mat(tags(:,1)) == i,2:3); % all tags for equation i
-            ind = strmatch( tagname, cellstr( tg(:,1) ) );
+            ind = strmatch('name', cellstr( tg(:,1) ) );
         end
-        if ~istag || length(ind) == 0
-            disp(['Equation number ' int2str(i) ' : ' num2str(tmp)])
-        else
-            t1 = tg( ind , 2 );
-            s = cell2mat(t1);
-            disp( ['Equation number ', int2str(i) ,' : ', num2str(tmp) ,' : ' s])
+        if ~(non_zero && tmp == 0)
+            if ~istag || length(ind) == 0
+                disp(['Equation number ' int2str(i) ' : ' num2str(tmp)])
+            else
+                t1 = tg( ind , 2 );
+                s = cell2mat(t1);
+                disp( ['Equation number ', int2str(i) ,' : ', num2str(tmp) ,' : ' s])
+            end
         end
     end
+    if non_zero && ~any_non_zero_residual
+        disp('All residuals are zero')
+    end
     skipline(2)
 end
 
diff --git a/preprocessor b/preprocessor
index 17c5e1935d..9db21a231c 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit 17c5e1935d825bb815059a0bfcf67ee71a98daee
+Subproject commit 9db21a231c5ab8f8aa26680ffecf3abfcf45bfab
diff --git a/tests/decision_rules/third_order/FV2011.mod b/tests/decision_rules/third_order/FV2011.mod
index 4fe714a1a5..5b102eaf74 100644
--- a/tests/decision_rules/third_order/FV2011.mod
+++ b/tests/decision_rules/third_order/FV2011.mod
@@ -110,7 +110,7 @@ var u_sigma_tb; stderr 1;
 var u_sigma_r; stderr 1;
 end;
 
-resid(1);
+resid;
 
 options_.solve_tolf=1E-12;
 steady(solve_algo=3);
@@ -118,4 +118,4 @@ steady(solve_algo=3);
 check;
 stoch_simul(order=3,pruning,irf=0,nofunctions,contemporaneous_correlation,TeX) C I Y H r D K lambda phi; 
 
-comparison_policy_functions_dynare_mathematica;
\ No newline at end of file
+comparison_policy_functions_dynare_mathematica;
diff --git a/tests/ep/burnside.mod b/tests/ep/burnside.mod
index 595b4d9323..958cb8f62f 100644
--- a/tests/ep/burnside.mod
+++ b/tests/ep/burnside.mod
@@ -22,7 +22,7 @@ x = xbar;
 y = beta*exp(theta*xbar)/(1-beta*exp(theta*xbar));
 end;
 
-resid(1);
+resid;
 
 steady;
 
diff --git a/tests/example1_with_tags.mod b/tests/example1_with_tags.mod
index af027aac19..749382d4e1 100644
--- a/tests/example1_with_tags.mod
+++ b/tests/example1_with_tags.mod
@@ -48,4 +48,4 @@ e = 0;
 u = 0;
 end;
 
-resid('title');
\ No newline at end of file
+resid(non_zero);
diff --git a/tests/loglinear/example4_exp.mod b/tests/loglinear/example4_exp.mod
index 40b6613886..9ebad8f8ba 100644
--- a/tests/loglinear/example4_exp.mod
+++ b/tests/loglinear/example4_exp.mod
@@ -4,7 +4,7 @@
  */
 
 /*
- * Copyright (C) 2001-2016 Dynare Team
+ * Copyright (C) 2001-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -56,7 +56,7 @@ k = log(11.08360443260358);
 a = 0;
 b = 0;
 end;
-resid(1);
+resid;
 shocks;
 var e; stderr 0.009;
 var u; stderr 0.009;
@@ -78,4 +78,4 @@ conditional_forecast(parameter_set=calibration, controlled_varexo=(u,e));
 
 oo_exp=oo_;
 conditional_forecasts_exp=oo_.conditional_forecast;
-save results_exp.mat oo_exp conditional_forecasts_exp
\ No newline at end of file
+save results_exp.mat oo_exp conditional_forecasts_exp
diff --git a/tests/loglinear/example4_exp_histval.mod b/tests/loglinear/example4_exp_histval.mod
index d0b7a90bc7..ed01394dbb 100644
--- a/tests/loglinear/example4_exp_histval.mod
+++ b/tests/loglinear/example4_exp_histval.mod
@@ -4,7 +4,7 @@
  */
 
 /*
- * Copyright (C) 2001-2016 Dynare Team
+ * Copyright (C) 2001-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -56,7 +56,7 @@ k = log(11.08360443260358);
 a = 0;
 b = 0;
 end;
-resid(1);
+resid;
 shocks;
 var e; stderr 0.009;
 var u; stderr 0.009;
diff --git a/tests/loglinear/example4_loglinear.mod b/tests/loglinear/example4_loglinear.mod
index e0f7a6f1d6..1f7df590e2 100644
--- a/tests/loglinear/example4_loglinear.mod
+++ b/tests/loglinear/example4_loglinear.mod
@@ -4,7 +4,7 @@
  */
 
 /*
- * Copyright (C) 2001-2010 Dynare Team
+ * Copyright (C) 2001-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -56,7 +56,7 @@ k = 11.08360443260358;
 a = 1;
 b = 1;
 end;
-resid(1);
+resid;
 steady;
 
 shocks;
@@ -132,4 +132,4 @@ end
 stoch_simul(loglinear,order=1,periods=100000);
 if abs(mean(y)-0.0776)>0.02
     error('Simulations are wrong')
-end
\ No newline at end of file
+end
diff --git a/tests/loglinear/example4_loglinear_histval.mod b/tests/loglinear/example4_loglinear_histval.mod
index c5617e12bf..a21705f730 100644
--- a/tests/loglinear/example4_loglinear_histval.mod
+++ b/tests/loglinear/example4_loglinear_histval.mod
@@ -4,7 +4,7 @@
  */
 
 /*
- * Copyright (C) 2001-2010 Dynare Team
+ * Copyright (C) 2001-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -57,7 +57,7 @@ a = 1;
 b = 1;
 end;
 
-resid(1);
+resid;
 shocks;
 var e; stderr 0.009;
 var u; stderr 0.009;
diff --git a/tests/risky_ss/jermann98.mod b/tests/risky_ss/jermann98.mod
index 5633d5bbc8..80e50c1563 100644
--- a/tests/risky_ss/jermann98.mod
+++ b/tests/risky_ss/jermann98.mod
@@ -78,7 +78,7 @@ mu     = ((c-(chihab*c/g))^(-tau))-chihab*bet*((c*g-chihab*c)^(-tau));
 ez     = 0;
 end;
 
-resid(1);
+resid;
 
 steady;                      
 
diff --git a/tests/risky_ss/jermann98_2.mod b/tests/risky_ss/jermann98_2.mod
index 4b28bd1d30..0309f66ee8 100644
--- a/tests/risky_ss/jermann98_2.mod
+++ b/tests/risky_ss/jermann98_2.mod
@@ -78,7 +78,7 @@ mu     = (((c-(chihab*c/g))^(-tau))-chihab*bet*((c*g-chihab*c)^(-tau)))/1e4;
 ez     = 0;
 end;
 
-resid(1);
+resid;
 
 steady;                      
 
diff --git a/tests/risky_ss/jermann98_3.mod b/tests/risky_ss/jermann98_3.mod
index e008cd07e7..6135956fc2 100644
--- a/tests/risky_ss/jermann98_3.mod
+++ b/tests/risky_ss/jermann98_3.mod
@@ -78,7 +78,7 @@ mu     = (((c-(chihab*c/g))^(-tau))-chihab*bet*((c*g-chihab*c)^(-tau)))/1e4;
 ez     = 0;
 end;
 
-resid(1);
+resid;
 
 steady;                      
 
diff --git a/tests/second_order/burnside_1.mod b/tests/second_order/burnside_1.mod
index 5c9906f7c0..3ce22a0bb7 100644
--- a/tests/second_order/burnside_1.mod
+++ b/tests/second_order/burnside_1.mod
@@ -22,7 +22,7 @@ x = xbar;
 y = beta*exp(theta*xbar)/(1-beta*exp(theta*xbar));
 end;
 
-resid(1);
+resid;
 
 steady;
 
diff --git a/tests/second_order/ds1.mod b/tests/second_order/ds1.mod
index ef4e636597..0b0958e171 100644
--- a/tests/second_order/ds1.mod
+++ b/tests/second_order/ds1.mod
@@ -62,7 +62,7 @@ initval;
   ze_1 = log(betabar)+yk_1;
 end;
 
-resid(1);
+resid;
 steady;
 model_diagnostics;
 check;
diff --git a/tests/second_order/ds2.mod b/tests/second_order/ds2.mod
index 45a92208e6..90b7f5706b 100644
--- a/tests/second_order/ds2.mod
+++ b/tests/second_order/ds2.mod
@@ -62,7 +62,7 @@ initval;
   ze_1 = log(betabar)+yk_1;
 end;
 
-resid(1);
+resid;
 steady;
 model_diagnostics;
 check;
@@ -74,4 +74,4 @@ oo2 = oo_.dr;
 if any(abs(oo1.ghxx-oo2.ghxx) > 1e-14);error('ds1 with missing variable doesn''t reproduce ds2');end;
 if any(abs(oo1.ghuu-oo2.ghuu) > 1e-14);error('ds1 with missing variable doesn''t reproduce ds2');end;
 if any(abs(oo1.ghxu-oo2.ghxu) > 1e-14);error('ds1 with missing variable doesn''t reproduce ds2');end;
-if any(abs(oo1.ghs2-oo2.ghs2) > 1e-14);error('ds1 with missing variable doesn''t reproduce ds2');end;
\ No newline at end of file
+if any(abs(oo1.ghs2-oo2.ghs2) > 1e-14);error('ds1 with missing variable doesn''t reproduce ds2');end;
diff --git a/tests/simul/Solow_no_varexo.mod b/tests/simul/Solow_no_varexo.mod
index 42de8ec69c..effb75d7dd 100644
--- a/tests/simul/Solow_no_varexo.mod
+++ b/tests/simul/Solow_no_varexo.mod
@@ -58,7 +58,7 @@ y=k^alpha;
 c=(1-s)*y;
 invest=y-c;
 end;
-resid(1);
+resid;
 steady;
 
 simul(periods=200, solve_algo=4);
-- 
GitLab