From 53b55914e6460cda95e6f8be0236f39ce35cd529 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 20 Oct 2020 17:34:11 +0200
Subject: [PATCH] LMMCP / linear perfect foresight: fix bug for models with a
 single equation

The routines use the find() function applied to a subset of columns of the
Jacobian, which in this case is a row vector. When passed a row vector, find()
returns row vectors (while it returns column vectors when passed a column
vector or a matrix). This case was not correctly handled.

(cherry picked from commit 87cc51932134aea2be55d7cfff4e39c61267c162)
---
 .../linear_perfect_foresight_problem.m        | 22 ++++++++++++++++++-
 .../perfect_foresight_mcp_problem.m           | 22 ++++++++++++++++++-
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/matlab/perfect-foresight-models/linear_perfect_foresight_problem.m b/matlab/perfect-foresight-models/linear_perfect_foresight_problem.m
index 1e73e6b0da..f2a84744a2 100644
--- a/matlab/perfect-foresight-models/linear_perfect_foresight_problem.m
+++ b/matlab/perfect-foresight-models/linear_perfect_foresight_problem.m
@@ -16,7 +16,7 @@ function [residuals,JJacobian] = linear_perfect_foresight_problem(y, dynamicjaco
 % SPECIAL REQUIREMENTS
 %   None.
 
-% Copyright (C) 2015-2019 Dynare Team
+% Copyright (C) 2015-2020 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -55,15 +55,35 @@ for it = maximum_lag+(1:T)
     if nargout == 2
         if T==1 && it==maximum_lag+1
             [rows, cols, vals] = find(dynamicjacobian(:,i_cols_0));
+            if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{1} = [rows, i_cols_J0(cols), vals];
         elseif it == maximum_lag+1
             [rows,cols,vals] = find(dynamicjacobian(:,i_cols_1));
+            if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{1} = [offset+rows, i_cols_J1(cols), vals];
         elseif it == maximum_lag+T
             [rows,cols,vals] = find(dynamicjacobian(:,i_cols_T));
+            if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals];
         else
             [rows,cols,vals] = find(dynamicjacobian(:,i_cols_j));
+            if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{it-maximum_lag} = [offset+rows, i_cols_J(cols), vals];
             i_cols_J = i_cols_J + ny;
         end
diff --git a/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m b/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m
index 561e0162da..40e344e136 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m
@@ -44,7 +44,7 @@ function [residuals,JJacobian] = perfect_foresight_mcp_problem(y, dynamic_functi
 % SPECIAL REQUIREMENTS
 %   None.
 
-% Copyright (C) 1996-2019 Dynare Team
+% Copyright (C) 1996-2020 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -83,15 +83,35 @@ for it = maximum_lag+(1:T)
         residuals(i_rows) = res(eq_index);
         if T==1 && it==maximum_lag+1
             [rows, cols, vals] = find(jacobian(:,i_cols_0));
+            if size(jacobian, 1) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{1} = [rows, i_cols_J0(cols), vals];
         elseif it == maximum_lag+1
             [rows,cols,vals] = find(jacobian(eq_index,i_cols_1));
+            if numel(eq_index) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{1} = [offset+rows, i_cols_J1(cols), vals];
         elseif it == maximum_lag+T
             [rows,cols,vals] = find(jacobian(eq_index,i_cols_T));
+            if numel(eq_index) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals];
         else
             [rows,cols,vals] = find(jacobian(eq_index,i_cols_j));
+            if numel(eq_index) == 1 % find() will return row vectors in this case
+                rows = rows';
+                cols = cols';
+                vals = vals';
+            end
             iJacobian{it-maximum_lag} = [offset+rows, i_cols_J(cols), vals];
             i_cols_J = i_cols_J + ny;
         end
-- 
GitLab