second derivatives of jacobian w.r.t. parameters

issue#70
Houtan Bastani 2010-03-15 16:02:07 +01:00
parent be6b9dcd09
commit c7093a8385
2 changed files with 43 additions and 1 deletions

View File

@ -2657,6 +2657,20 @@ DynamicModel::computeParamsDerivatives()
jacobian_params_derivatives[make_pair(eq, make_pair(var, param))] = d2;
}
for (second_derivatives_type::const_iterator it2 = jacobian_params_derivatives.begin();
it2 != jacobian_params_derivatives.end(); it2++)
{
int eq = it2->first.first;
int var = it2->first.second.first;
int param1 = it2->first.second.second;
NodeID d1 = it2->second;
NodeID d2 = d1->getDerivative(param);
if (d2 == Zero)
continue;
jacobian_params_second_derivatives[make_pair(eq, make_pair(var, make_pair(param1, param)))] = d2;
}
for (second_derivatives_type::const_iterator it2 = second_derivatives.begin();
it2 != second_derivatives.end(); it2++)
{
@ -2750,9 +2764,30 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const
paramsDerivsFile << ";" << endl;
}
// Write jacobian second derivatives
paramsDerivsFile << "gpp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", "
<< symbol_table.param_nbr() << ", " << symbol_table.param_nbr() << ");" << endl;
for (third_derivatives_type::const_iterator it = jacobian_params_second_derivatives.begin();
it != jacobian_params_second_derivatives.end(); it++)
{
int eq = it->first.first;
int var = it->first.second.first;
int param1 = it->first.second.second.first;
int param2 = it->first.second.second.second;
NodeID d2 = it->second;
int var_col = getDynJacobianCol(var) + 1;
int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1;
int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1;
paramsDerivsFile << "gpp(" << eq+1 << ", " << var_col << ", " << param1_col << ", " << param2_col << ") = ";
d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms);
paramsDerivsFile << ";" << endl;
}
// Write hessian derivatives
paramsDerivsFile << "gp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", "
paramsDerivsFile << "hp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", "
<< dynJacobianColsNbr << ", " << symbol_table.param_nbr() << ");" << endl;
for (third_derivatives_type::const_iterator it = hessian_params_derivatives.begin();

View File

@ -71,6 +71,13 @@ private:
*/
second_derivatives_type jacobian_params_derivatives;
//! Second derivatives of the jacobian w.r. to parameters
/*! First index is equation number, second is endo/exo/exo_det variable, and third and fourth are parameters.
Only non-null derivatives are stored in the map.
Variable and parameter indices are those of the getDerivID() method.
*/
third_derivatives_type jacobian_params_second_derivatives;
//! Derivatives of the hessian w.r. to parameters
/*! First index is equation number, first and second are endo/exo/exo_det variable, and third is parameter.
Only non-null derivatives are stored in the map.