From 41052ccb74d43c738d476110f7fc8eebf069b1e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 2 Mar 2023 14:53:43 +0100
Subject: [PATCH] =?UTF-8?q?Optimization=20for=20derivation=20of=20STEADY?=
 =?UTF-8?q?=5FSTATE(=E2=80=A6)=20operator=20in=20a=20dynamic=20context?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In a dynamic context, the only potentially non-null derivatives of
STEADY_STATE(…) are the parameters. We know that the derivatives w.r.t. other
variables are zero, so store that information in non_null_derivatives.
---
 src/ExprNode.cc | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/ExprNode.cc b/src/ExprNode.cc
index 1da16358..09f40905 100644
--- a/src/ExprNode.cc
+++ b/src/ExprNode.cc
@@ -2136,13 +2136,18 @@ UnaryOpNode::prepareForDerivation()
 
   preparedForDerivation = true;
 
-  arg->prepareForDerivation();
-
-  // Non-null derivatives are those of the argument (except for STEADY_STATE)
-  non_null_derivatives = arg->non_null_derivatives;
-  if (op_code == UnaryOpcode::steadyState || op_code == UnaryOpcode::steadyStateParamDeriv
-      || op_code == UnaryOpcode::steadyStateParam2ndDeriv)
+  /* Non-null derivatives are those of the argument (except for STEADY_STATE in
+     a dynamic context, in which case the potentially non-null derivatives are
+     all the parameters) */
+  if ((op_code == UnaryOpcode::steadyState || op_code == UnaryOpcode::steadyStateParamDeriv
+       || op_code == UnaryOpcode::steadyStateParam2ndDeriv)
+      && datatree.isDynamic())
     datatree.addAllParamDerivId(non_null_derivatives);
+  else
+    {
+      arg->prepareForDerivation();
+      non_null_derivatives = arg->non_null_derivatives;
+    }
 }
 
 expr_t
-- 
GitLab