preprocessor: fix scoping problem with temporary variables

issue#70
Houtan Bastani 2015-09-04 14:58:14 +02:00
parent 0f75ddbd64
commit 3117bf79f2
2 changed files with 24 additions and 27 deletions

View File

@ -2318,10 +2318,10 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
<< " %" << endl << " %" << endl
<< endl << endl
<< jacobian_output.str() << jacobian_output.str()
<< "end" << endl; << endl
// Initialize g2 matrix // Initialize g2 matrix
DynamicOutput << "if nargout >= 3," << endl << "if nargout >= 3," << endl
<< " %" << endl << " %" << endl
<< " % Hessian matrix" << endl << " % Hessian matrix" << endl
<< " %" << endl << " %" << endl
@ -2332,7 +2332,6 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
<< " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl;
else // Either hessian is all zero, or we didn't compute it else // Either hessian is all zero, or we didn't compute it
DynamicOutput << " g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl; DynamicOutput << " g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl;
DynamicOutput << "end" << endl;
// Initialize g3 matrix // Initialize g3 matrix
DynamicOutput << "if nargout >= 4," << endl DynamicOutput << "if nargout >= 4," << endl
@ -2348,7 +2347,9 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
else // Either 3rd derivatives is all zero, or we didn't compute it else // Either 3rd derivatives is all zero, or we didn't compute it
DynamicOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; DynamicOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl;
DynamicOutput << "end" << endl; DynamicOutput << "end" << endl
<< "end" << endl
<< "end" << endl;
} }
else if (output_type == oCDynamicModel) else if (output_type == oCDynamicModel)
{ {
@ -2362,28 +2363,25 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
<< " /* Jacobian */" << endl << " /* Jacobian */" << endl
<< " if (g1 == NULL)" << endl << " if (g1 == NULL)" << endl
<< " return;" << endl << " return;" << endl
<< " else" << endl << endl
<< " {" << endl
<< jacobian_output.str() << jacobian_output.str()
<< " }" << endl; << endl;
if (second_derivatives.size()) if (second_derivatives.size())
DynamicOutput << " /* Hessian for endogenous and exogenous variables */" << endl DynamicOutput << " /* Hessian for endogenous and exogenous variables */" << endl
<< " if (v2 == NULL)" << endl << " if (v2 == NULL)" << endl
<< " return;" << endl << " return;" << endl
<< " else" << endl << endl
<< " {" << endl
<< hessian_output.str() << hessian_output.str()
<< " }" << endl; << endl;
if (third_derivatives.size()) if (third_derivatives.size())
DynamicOutput << " /* Third derivatives for endogenous and exogenous variables */" << endl DynamicOutput << " /* Third derivatives for endogenous and exogenous variables */" << endl
<< " if (v3 == NULL)" << endl << " if (v3 == NULL)" << endl
<< " return;" << endl << " return;" << endl
<< " else" << endl << endl
<< " {" << endl
<< third_derivatives_output.str() << third_derivatives_output.str()
<< " }" << endl; << endl;
DynamicOutput << "}" << endl << endl; DynamicOutput << "}" << endl << endl;
} }

View File

@ -1392,7 +1392,6 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
<< " if ~isreal(g1)" << endl << " if ~isreal(g1)" << endl
<< " g1 = real(g1)+2*imag(g1);" << endl << " g1 = real(g1)+2*imag(g1);" << endl
<< " end" << endl << " end" << endl
<< "end" << endl
<< "if nargout >= 3," << endl << "if nargout >= 3," << endl
<< " %" << endl << " %" << endl
<< " % Hessian matrix" << endl << " % Hessian matrix" << endl
@ -1405,7 +1404,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
<< " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," << g2ncols << ");" << endl; << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," << g2ncols << ");" << endl;
else else
StaticOutput << " g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl; StaticOutput << " g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl;
StaticOutput << "end" << endl;
// Initialize g3 matrix // Initialize g3 matrix
StaticOutput << "if nargout >= 4," << endl StaticOutput << "if nargout >= 4," << endl
<< " %" << endl << " %" << endl
@ -1419,6 +1418,9 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
<< " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl;
else // Either 3rd derivatives is all zero, or we didn't compute it else // Either 3rd derivatives is all zero, or we didn't compute it
StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl;
StaticOutput << "end" << endl
<< "end" << endl
<< "end" << endl;
} }
else if (output_type == oCStaticModel) else if (output_type == oCStaticModel)
{ {
@ -1432,27 +1434,24 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
<< " /* Jacobian */" << endl << " /* Jacobian */" << endl
<< " if (g1 == NULL)" << endl << " if (g1 == NULL)" << endl
<< " return;" << endl << " return;" << endl
<< " else" << endl << endl
<< " {" << endl
<< jacobian_output.str() << jacobian_output.str()
<< " }" << endl; << endl;
if (second_derivatives.size()) if (second_derivatives.size())
StaticOutput << " /* Hessian for endogenous and exogenous variables */" << endl StaticOutput << " /* Hessian for endogenous and exogenous variables */" << endl
<< " if (v2 == NULL)" << endl << " if (v2 == NULL)" << endl
<< " return;" << endl << " return;" << endl
<< " else" << endl << endl
<< " {" << endl
<< hessian_output.str() << hessian_output.str()
<< " }" << endl; << endl;
if (third_derivatives.size()) if (third_derivatives.size())
StaticOutput << " /* Third derivatives for endogenous and exogenous variables */" << endl StaticOutput << " /* Third derivatives for endogenous and exogenous variables */" << endl
<< " if (v3 == NULL)" << endl << " if (v3 == NULL)" << endl
<< " return;" << endl << " return;" << endl
<< " else" << endl << endl
<< " {" << endl << third_derivatives_output.str()
<< third_derivatives_output.str() << endl;
<< " }" << endl;
} }
else else
{ {