From 064a3dcd773c8c27afa5a64385d84b1444202820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 19 Jan 2022 15:05:59 +0100 Subject: [PATCH] Fix orig_lead_lag field for unary op auxiliary variables Ref. dynare#1843 --- src/ExprNode.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ExprNode.cc b/src/ExprNode.cc index cc7b73fb..2aaf58c7 100644 --- a/src/ExprNode.cc +++ b/src/ExprNode.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2007-2021 Dynare Team + * Copyright © 2007-2022 Dynare Team * * This file is part of Dynare. * @@ -3525,7 +3525,10 @@ UnaryOpNode::substituteUnaryOpNodes(const lag_equivalence_table_t &nodes, subst_ must be substituted. We create the auxiliary variable and fill the substitution table for all those similar nodes, in an iteration going from leads to lags. */ - int base_index = 0; + int base_index = it->second.rbegin()->first; // Within the equivalence class, + // index of the node that will + // be used as the definition for + // the aux var. VariableNode *aux_var = nullptr; for (auto rit = it->second.rbegin(); rit != it->second.rend(); ++rit) if (rit == it->second.rbegin()) @@ -3540,9 +3543,9 @@ UnaryOpNode::substituteUnaryOpNodes(const lag_equivalence_table_t &nodes, subst_ exit(EXIT_FAILURE); } + auto argsubst_shifted = argsubst->decreaseLeadsLags(index - base_index); int symb_id; - auto vn = dynamic_cast(argsubst); - if (!vn) + if (auto vn = dynamic_cast(argsubst_shifted); !vn) symb_id = datatree.symbol_table.addUnaryOpAuxiliaryVar(this->idx, dynamic_cast(rit->second), unary_op); else symb_id = datatree.symbol_table.addUnaryOpAuxiliaryVar(this->idx, dynamic_cast(rit->second), unary_op, @@ -3551,7 +3554,6 @@ UnaryOpNode::substituteUnaryOpNodes(const lag_equivalence_table_t &nodes, subst_ neweqs.push_back(datatree.AddEqual(aux_var, dynamic_cast(rit->second))); subst_table[rit->second] = dynamic_cast(aux_var); - base_index = rit->first; } else subst_table[rit->second] = dynamic_cast(aux_var->decreaseLeadsLags(base_index - rit->first));