From 83bfc792afc73ad938a9c807f3077b6ad948fa7b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 7 Dec 2022 16:10:03 +0100
Subject: [PATCH] Sparse representation: fix bug in output of indices in driver
 and JSON files

It would previously forget to write the indices for the highest derivation
order (e.g. if order=3, then it would only write sparce indices up to 2nd
order).

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

diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index de892f53..9f01ad33 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -2242,7 +2242,7 @@ ModelTree::writeDriverSparseIndicesHelper(ostream &output) const
   output << "]);" << endl;
 
   // Write indices for the sparse higher-order derivatives
-  for (int i {2}; i < computed_derivs_order; i++)
+  for (int i {2}; i <= computed_derivs_order; i++)
     {
       output << "M_." << model_name << "_g" << i << "_sparse_indices = int32([";
       for (const auto &[vidx, d] : derivatives[i])
@@ -2292,7 +2292,7 @@ ModelTree::writeJsonSparseIndicesHelper(ostream &output) const
   output << ']' << endl;
 
   // Write indices for the sparse higher-order derivatives
-  for (int i {2}; i < computed_derivs_order; i++)
+  for (int i {2}; i <= computed_derivs_order; i++)
     {
       output << R"(, ")" << model_name << "_g" << i << R"(_sparse_indices": [)";
       for (bool printed_something {false};
-- 
GitLab