write Hessians of static and dynamic functions more efficiently

issue#70
Houtan Bastani 2015-08-20 12:18:19 +02:00
parent 1c27ff3cc4
commit 046bc44e8a
2 changed files with 69 additions and 44 deletions

View File

@ -2157,6 +2157,16 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
int col_nb = id1 * dynJacobianColsNbr + id2; int col_nb = id1 * dynJacobianColsNbr + id2;
int col_nb_sym = id2 * dynJacobianColsNbr + id1; int col_nb_sym = id2 * dynJacobianColsNbr + id1;
ostringstream for_sym;
if (output_type == oJuliaDynamicModel)
{
for_sym << "g1[" << eq + 1 << "," << col_nb + 1 << "]";
hessian_output << " @inbounds " << for_sym.str() << " = ";
d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms);
hessian_output << endl;
}
else
{
sparseHelper(2, hessian_output, k, 0, output_type); sparseHelper(2, hessian_output, k, 0, output_type);
hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "=" << eq + 1 << ";" << endl;
@ -2169,9 +2179,14 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
hessian_output << ";" << endl; hessian_output << ";" << endl;
k++; k++;
}
// Treating symetric elements // Treating symetric elements
if (id1 != id2) if (id1 != id2)
if (output_type == oJuliaDynamicModel)
hessian_output << " @inbounds g1[" << eq + 1 << "," << col_nb_sym + 1 << "] = "
<< for_sym.str() << endl;
else
{ {
sparseHelper(2, hessian_output, k, 0, output_type); sparseHelper(2, hessian_output, k, 0, output_type);
hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "=" << eq + 1 << ";" << endl;
@ -2379,14 +2394,12 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
<< " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl
<< " #" << endl << endl << " #" << endl << endl
<< " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl
<< model_output.str()
<< " #" << endl << " #" << endl
<< " # Hessian matrix" << endl << " # Hessian matrix" << endl
<< " #" << endl; << " #" << endl;
if (second_derivatives.size()) if (second_derivatives.size())
DynamicOutput << model_output.str() DynamicOutput << hessian_output.str();
<< " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl
<< hessian_output.str()
<< " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl;
// Initialize g3 matrix // Initialize g3 matrix
int ncols = hessianColsNbr * dynJacobianColsNbr; int ncols = hessianColsNbr * dynJacobianColsNbr;

View File

@ -1233,6 +1233,16 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
int col_nb = tsid1*symbol_table.endo_nbr()+tsid2; int col_nb = tsid1*symbol_table.endo_nbr()+tsid2;
int col_nb_sym = tsid2*symbol_table.endo_nbr()+tsid1; int col_nb_sym = tsid2*symbol_table.endo_nbr()+tsid1;
ostringstream for_sym;
if (output_type == oJuliaDynamicModel)
{
for_sym << "g1[" << eq + 1 << "," << col_nb + 1 << "]";
hessian_output << " @inbounds " << for_sym.str() << " = ";
d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms);
hessian_output << endl;
}
else
{
sparseHelper(2, hessian_output, k, 0, output_type); sparseHelper(2, hessian_output, k, 0, output_type);
hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "=" << eq + 1 << ";" << endl;
@ -1245,9 +1255,14 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
hessian_output << ";" << endl; hessian_output << ";" << endl;
k++; k++;
}
// Treating symetric elements // Treating symetric elements
if (symb_id1 != symb_id2) if (symb_id1 != symb_id2)
if (output_type == oJuliaDynamicModel)
hessian_output << " @inbounds g1[" << eq + 1 << "," << col_nb_sym + 1 << "] = "
<< for_sym.str() << endl;
else
{ {
sparseHelper(2, hessian_output, k, 0, output_type); sparseHelper(2, hessian_output, k, 0, output_type);
hessian_output << "=" << eq + 1 << ";" << endl; hessian_output << "=" << eq + 1 << ";" << endl;
@ -1453,16 +1468,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
<< " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl
<< " #" << endl << endl << " #" << endl << endl
<< " static!(y, x, params, residual, g1)" << endl << " static!(y, x, params, residual, g1)" << endl
<< model_output.str()
<< " #" << endl << " #" << endl
<< " # Hessian matrix" << endl << " # Hessian matrix" << endl
<< " #" << endl; << " #" << endl;
if (second_derivatives.size()) if (second_derivatives.size())
StaticOutput << model_output.str() StaticOutput << hessian_output.str();
<< " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl
<< hessian_output.str()
<< " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << ","
<< g2ncols << ");" << endl;
// Initialize g3 matrix // Initialize g3 matrix
int ncols = hessianColsNbr * JacobianColsNbr; int ncols = hessianColsNbr * JacobianColsNbr;
@ -1486,7 +1498,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
if (third_derivatives.size()) if (third_derivatives.size())
StaticOutput << model_output.str() StaticOutput << model_output.str()
<< " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl << " v3 = Array(Float64, " << NNZDerivatives[2] << ", 3)" << endl
<< third_derivatives_output.str() << third_derivatives_output.str()
<< " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");"
<< endl; << endl;