From 4f7794a8f94209608f74a925f9cbd98f4d56d8ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 8 Dec 2022 14:32:26 +0100
Subject: [PATCH] Sparse representation: compatibility fix with MATLAB < R2020a
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

For those older MATLABs, the “sparse” function does not accept vectors of
integer data type as indices.

Ref. dynare#1875
---
 src/ModelTree.hh | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 9f01ad33..6e2a6046 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -2570,7 +2570,12 @@ ModelTree::writeSparseModelMFiles(const string &basename) const
     output << "if ~isreal(g1_v)" << endl
            << "    g1_v = real(g1_v)+2*imag(g1_v);" << endl
            << "end" << endl;
-  output << "g1 = sparse(sparse_rowval, sparse_colval, g1_v, " << equations.size() << ", " << getJacobianColsNbr(true) << ");" << endl
+  // On MATLAB < R2020a, sparse() does not accept int32 indices
+  output << "if ~isoctave && matlab_ver_less_than('9.8')" << endl
+         << "    sparse_rowval = double(sparse_rowval);" << endl
+         << "    sparse_colval = double(sparse_colval);" << endl
+         << "end" << endl
+         << "g1 = sparse(sparse_rowval, sparse_colval, g1_v, " << equations.size() << ", " << getJacobianColsNbr(true) << ");" << endl
          << "end" << endl;
   output.close();
 
@@ -2635,7 +2640,12 @@ ModelTree::writeSparseModelMFiles(const string &basename) const
               output << "if nargout > 3" << endl
                      << "    g1_v = NaN(" << blocks_jacobian_sparse_column_major_order[blk].size() << ", 1);" << endl;
               writeSparsePerBlockJacobianHelper<output_type>(blk, output, temporary_terms_written);
-              output << "    g1 = sparse(sparse_rowval, sparse_colval, g1_v, " << blocks[blk].mfs_size << ", "
+              // On MATLAB < R2020a, sparse() does not accept int32 indices
+              output << "    if ~isoctave && matlab_ver_less_than('9.8')" << endl
+                     << "        sparse_rowval = double(sparse_rowval);" << endl
+                     << "        sparse_colval = double(sparse_colval);" << endl
+                     << "    end" << endl
+                     << "    g1 = sparse(sparse_rowval, sparse_colval, g1_v, " << blocks[blk].mfs_size << ", "
                      << (one_boundary ? 1 : 3)*blocks[blk].mfs_size << ");" << endl
                      << "end" << endl;
             }
-- 
GitLab