From 3041db17c61f2d75f427a27ccbb92f091ee0b974 Mon Sep 17 00:00:00 2001 From: sebastien Date: Mon, 29 Jun 2009 09:27:09 +0000 Subject: [PATCH] trunk preprocessor: fixed bug with sparse Hessian and USE_DLL git-svn-id: https://www.dynare.org/svn/dynare/trunk@2793 ac1d8469-bf42-47a9-8791-bf33cf982152 --- DynamicModel.cc | 16 ++++++++-------- ModelTree.cc | 13 ++++++++++++- ModelTree.hh | 9 +++++++-- StaticModel.cc | 16 ++++++++-------- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index ffb42c94..408fc410 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -1940,7 +1940,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput) const ostringstream g1; g1 << " g1"; - matrixHelper(g1, eq, getDynJacobianCol(var), output_type); + jacobianHelper(g1, eq, getDynJacobianCol(var), output_type); jacobian_output << g1.str() << "=" << g1.str() << "+"; d1->writeOutput(jacobian_output, output_type, temporary_terms); @@ -1964,15 +1964,15 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput) const int col_nb_sym = id2 * dynJacobianColsNbr + id1; hessian_output << "v2"; - matrixHelper(hessian_output, k, 0, output_type); + hessianHelper(hessian_output, k, 0, output_type); hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 1, output_type); + hessianHelper(hessian_output, k, 1, output_type); hessian_output << "=" << col_nb + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 2, output_type); + hessianHelper(hessian_output, k, 2, output_type); hessian_output << "="; d2->writeOutput(hessian_output, output_type, temporary_terms); hessian_output << ";" << endl; @@ -1983,17 +1983,17 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput) const if (id1 != id2) { hessian_output << "v2"; - matrixHelper(hessian_output, k, 0, output_type); + hessianHelper(hessian_output, k, 0, output_type); hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 1, output_type); + hessianHelper(hessian_output, k, 1, output_type); hessian_output << "=" << col_nb_sym + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 2, output_type); + hessianHelper(hessian_output, k, 2, output_type); hessian_output << "=v2"; - matrixHelper(hessian_output, k-1, 2, output_type); + hessianHelper(hessian_output, k-1, 2, output_type); hessian_output << ";" << endl; k++; diff --git a/ModelTree.cc b/ModelTree.cc index ddb302a4..bcf80fd1 100644 --- a/ModelTree.cc +++ b/ModelTree.cc @@ -281,7 +281,7 @@ ModelTree::addEquation(NodeID eq) } void -ModelTree::matrixHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const +ModelTree::jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const { output << LEFT_ARRAY_SUBSCRIPT(output_type); if (IS_MATLAB(output_type)) @@ -290,3 +290,14 @@ ModelTree::matrixHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputTy output << eq_nb + col_nb * equations.size(); output << RIGHT_ARRAY_SUBSCRIPT(output_type); } + +void +ModelTree::hessianHelper(ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const +{ + output << LEFT_ARRAY_SUBSCRIPT(output_type); + if (IS_MATLAB(output_type)) + output << row_nb + 1 << ", " << col_nb + 1; + else + output << row_nb + col_nb * NNZDerivatives[1]; + output << RIGHT_ARRAY_SUBSCRIPT(output_type); +} diff --git a/ModelTree.hh b/ModelTree.hh index 3d82b469..f1c12047 100644 --- a/ModelTree.hh +++ b/ModelTree.hh @@ -99,8 +99,13 @@ protected: //! Writes model equations void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const; - //! Writes either (i+1,j+1) or [i+j*n_i] whether we are in Matlab or C mode - void matrixHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const; + //! Helper for writing the Jacobian elements in MATLAB and C + /*! Writes either (i+1,j+1) or [i+j*no_eq] */ + void jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const; + + //! Helper for writing the sparse Hessian elements in MATLAB and C + /*! Writes either (i+1,j+1) or [i+j*NNZDerivatives[1]] */ + void hessianHelper(ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const; //! Writes LaTeX model file void writeLatexModelFile(const string &filename, ExprNodeOutputType output_type) const; diff --git a/StaticModel.cc b/StaticModel.cc index 802922cb..1859b78d 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -156,7 +156,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput) const ostringstream g1; g1 << " g1"; - matrixHelper(g1, eq, symbol_table.getTypeSpecificID(symb_id), output_type); + jacobianHelper(g1, eq, symbol_table.getTypeSpecificID(symb_id), output_type); jacobian_output << g1.str() << "=" << g1.str() << "+"; d1->writeOutput(jacobian_output, output_type, temporary_terms); @@ -180,15 +180,15 @@ StaticModel::writeStaticModel(ostream &StaticOutput) const int col_nb_sym = tsid2*symbol_table.endo_nbr()+tsid1; hessian_output << "v2"; - matrixHelper(hessian_output, k, 0, output_type); + hessianHelper(hessian_output, k, 0, output_type); hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 1, output_type); + hessianHelper(hessian_output, k, 1, output_type); hessian_output << "=" << col_nb + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 2, output_type); + hessianHelper(hessian_output, k, 2, output_type); hessian_output << "="; d2->writeOutput(hessian_output, output_type, temporary_terms); hessian_output << ";" << endl; @@ -199,17 +199,17 @@ StaticModel::writeStaticModel(ostream &StaticOutput) const if (symb_id1 != symb_id2) { hessian_output << "v2"; - matrixHelper(hessian_output, k, 0, output_type); + hessianHelper(hessian_output, k, 0, output_type); hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 1, output_type); + hessianHelper(hessian_output, k, 1, output_type); hessian_output << "=" << col_nb_sym + 1 << ";" << endl; hessian_output << "v2"; - matrixHelper(hessian_output, k, 2, output_type); + hessianHelper(hessian_output, k, 2, output_type); hessian_output << "=v2"; - matrixHelper(hessian_output, k-1, 2, output_type); + hessianHelper(hessian_output, k-1, 2, output_type); hessian_output << ";" << endl; k++;