Commit 33b95371 authored by michel's avatar michel
Browse files

v4.1: added to preprocessor int NNZDerivatives[3] with number of non-zeros...

v4.1: added to preprocessor int NNZDerivatives[3] with number of non-zeros derivatives at order 1, 2, and 3
      used it to initialize sparse g2 and g3 in Matlab <modfile>_dynamic.m


git-svn-id: https://www.dynare.org/svn/dynare/trunk@2668 ac1d8469-bf42-47a9-8791-bf33cf982152
parent ec4b45cc
......@@ -1741,7 +1741,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput) const
{
// Writing initialization instruction for matrix g2
DynamicOutput << "if nargout >= 3," << endl
<< " g2 = sparse([],[],[], " << nrows << ", " << hessianColsNbr << ", " << 5*hessianColsNbr << ");" << endl
<< " g2 = sparse([],[],[], " << nrows << ", " << hessianColsNbr << ", " << NNZDerivatives[1] << ");" << endl
<< endl
<< "%" << endl
<< "% Hessian matrix" << endl
......@@ -1755,7 +1755,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput) const
{
int ncols = hessianColsNbr * dynJacobianColsNbr;
DynamicOutput << "if nargout >= 4," << endl
<< " g3 = sparse([],[],[], " << nrows << ", " << ncols << ", " << 5*ncols << ");" << endl
<< " g3 = sparse([],[],[], " << nrows << ", " << ncols << ", " << NNZDerivatives[2] << ");" << endl
<< endl
<< "%" << endl
<< "% Third order derivatives" << endl
......
......@@ -28,6 +28,8 @@ ModelTree::ModelTree(SymbolTable &symbol_table_arg,
DataTree(symbol_table_arg, num_constants_arg),
mode(eStandardMode)
{
for(int i=0; i < 3; i++)
NNZDerivatives[i] = 0;
}
int
......@@ -59,6 +61,7 @@ ModelTree::computeJacobian(const set<int> &vars)
if (d1 == Zero)
continue;
first_derivatives[make_pair(eq, *it)] = d1;
++NNZDerivatives[0];
}
}
......@@ -84,6 +87,10 @@ ModelTree::computeHessian(const set<int> &vars)
if (d2 == Zero)
continue;
second_derivatives[make_pair(eq, make_pair(var1, var2))] = d2;
if (var2 == var1)
++NNZDerivatives[1];
else
NNZDerivatives[1] += 2;
}
}
}
......@@ -114,6 +121,12 @@ ModelTree::computeThirdDerivatives(const set<int> &vars)
if (d3 == Zero)
continue;
third_derivatives[make_pair(eq, make_pair(var1, make_pair(var2, var3)))] = d3;
if (var3 == var2 && var2 == var1)
++NNZDerivatives[2];
else if (var3 == var2 || var2 == var1)
NNZDerivatives[2] += 3;
else
NNZDerivatives[2] += 6;
}
}
}
......
......@@ -45,6 +45,9 @@ protected:
//! Stores declared equations
vector<BinaryOpNode *> equations;
//! Number of non-zero derivatives
int NNZDerivatives[3];
typedef map<pair<int, int>, NodeID> first_derivatives_type;
//! First order derivatives
/*! First index is equation number, second is variable w.r. to which is computed the derivative.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment