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
time-shift
michel 2009-05-12 20:32:27 +00:00
parent ec4b45cc7c
commit 33b9537149
3 changed files with 18 additions and 2 deletions

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -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.