From 4808c8a1ceeb88ce86d98a730c67c559564dc237 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 11:32:26 +0200 Subject: [PATCH] replace zeros with fill! and remove spzeros in static and dynamic functions --- DynamicModel.cc | 32 +++++++++++++++++++++++++------- StaticModel.cc | 32 +++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 144f97e3..4c1daab6 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2339,9 +2339,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl << "#" << endl + << "# Output" << endl + << "# residual: " << nrows << " x 1" << endl + << "#" << endl + << "fill!(residual, 0.0)" << endl << endl + << "#" << endl << "# Model equations" << endl << "#" << endl - << "residual = zeros(" << nrows << ");" << endl << model_output.str() << model_eq_output.str() << "end" << endl << endl @@ -2350,9 +2354,14 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << nrows << " x 1" << endl + << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " #" << endl + << " fill!(g1, 0.0)" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl << model_output.str() - << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl << " #" << endl << " # Jacobian matrix" << endl << " #" << endl @@ -2363,6 +2372,12 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << nrows << " x 1" << endl + << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl + << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl << " #" << endl << " # Hessian matrix" << endl @@ -2372,28 +2387,31 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl << hessian_output.str() << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; - else // Either hessian is all zero, or we didn't compute it - DynamicOutput << " g2 = spzeros(" << nrows << "," << hessianColsNbr << ")" << endl; // Initialize g3 matrix + int ncols = hessianColsNbr * dynJacobianColsNbr; DynamicOutput << "end" << endl << endl << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " << "params::Vector{Float64}," << endl << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << nrows << " x 1" << endl + << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl + << " # g3: sparse zeros " << nrows << " x " << ncols << endl + << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl << " #" << endl << " # Third order derivatives" << endl << " #" << endl; - int ncols = hessianColsNbr * dynJacobianColsNbr; if (third_derivatives.size()) DynamicOutput << model_output.str() << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl << third_derivatives_output.str() << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; - else // Either 3rd derivatives is all zero, or we didn't compute it - DynamicOutput << " g3 = spzeros(" << nrows << "," << ncols << ")" << endl; DynamicOutput << "end" << endl; } } diff --git a/StaticModel.cc b/StaticModel.cc index dcfa9c3b..e21e325e 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -1409,7 +1409,11 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c StaticOutput << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl - << "residual = zeros(" << equations.size() << ")" << endl + << "#" << endl + << "# Output" << endl + << "# residual: " << equations.size() << " x 1" << endl + << "#" << endl + << "fill!(residual, 0.0)" << endl << endl << "#" << endl << "# Model equations" << endl << "#" << endl @@ -1422,9 +1426,14 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << equations.size() << " x 1" << endl + << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " #" << endl + << " fill!(g1, 0.0)" << endl << endl << " static!(y, x, params, residual)" << endl << model_output.str() - << " g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");" << endl << " #" << endl << " # Jacobian matrix" << endl << " #" << endl << endl @@ -1437,6 +1446,12 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << equations.size() << " x 1" << endl + << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl + << " #" << endl << endl << " static!(y, x, params, residual, g1)" << endl << " #" << endl << " # Hessian matrix" << endl @@ -1448,30 +1463,33 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << hessian_output.str() << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," << g2ncols << ");" << endl; - else - StaticOutput << " g2 = spzeros(" << equations.size() << "," << g2ncols << ")" << endl; // Initialize g3 matrix + int ncols = hessianColsNbr * JacobianColsNbr; StaticOutput << "end" << endl << endl << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64}," << endl << " g3::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << equations.size() << " x 1" << endl + << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl + << " # g3: sparse zeros " << nrows << " x " << ncols << endl + << " #" << endl << endl << " static!(y, x, params, residual, g1, g2)" << endl << " #" << endl << " # Third order derivatives" << endl << " #" << endl; - int ncols = hessianColsNbr * JacobianColsNbr; if (third_derivatives.size()) StaticOutput << model_output.str() << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl << third_derivatives_output.str() << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; - else // Either 3rd derivatives is all zero, or we didn't compute it - StaticOutput << " g3 = spzeros(" << nrows << "," << ncols << ")" << endl; StaticOutput << "end" << endl; } }