diff --git a/DynamicModel.cc b/DynamicModel.cc index eccc4e9c..de35ef8d 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -4018,8 +4018,14 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type, bool deterministic_model for (int i = 0; i < (int) neweqs.size(); i++) addEquation(neweqs[i]); - // Add the new set of equations at the *beginning* of aux_equations - copy(neweqs.rbegin(), neweqs.rend(), front_inserter(aux_equations)); + // Order of auxiliary variable definition equations: + // - expectation (entered before this function is called) + // - lead variables from lower lead to higher lead (need to be listed in reverse order) + // - lag variables from lower lag to higher lag + if ((type == avEndoLead) || (type == avExoLead)) + copy(neweqs.rbegin(), neweqs.rend(), back_inserter(aux_equations)); + else + copy(neweqs.begin(), neweqs.end(), back_inserter(aux_equations)); if (neweqs.size() > 0) { diff --git a/StaticModel.cc b/StaticModel.cc index adc84c23..2b8c1584 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -1702,7 +1702,7 @@ StaticModel::writeLatexFile(const string &basename) const void StaticModel::writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const { - for (int i = (int) aux_equations.size()-1; i >= 0; i--) + for (int i = 0; i < (int) aux_equations.size(); i++) { dynamic_cast(aux_equations[i])->writeOutput(output, output_type); output << ";" << endl; @@ -1730,7 +1730,7 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const << "% from model file (.mod)" << endl << endl; - for (int i = (int) aux_equations.size()-1; i >= 0; i--) + for (int i = 0; i < (int) aux_equations.size(); i++) { dynamic_cast(aux_equations[i])->writeOutput(output, oMatlabStaticModel); output << ";" << endl;