Block decomposition: move collectFirstOrderDerivativesEndogenous in ModelTree

By the way, use camel case for the function name.
issue#70
Sébastien Villemot 2020-03-30 14:51:53 +02:00
parent ad48697abe
commit 4d30f17e0b
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
6 changed files with 22 additions and 39 deletions

View File

@ -3813,21 +3813,6 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
}
}
map<tuple<int, int, int>, expr_t>
DynamicModel::collect_first_order_derivatives_endogenous()
{
map<tuple<int, int, int>, expr_t> endo_derivatives;
for (auto & [indices, d1] : derivatives[1])
if (getTypeByDerivID(indices[1]) == SymbolType::endogenous)
{
int eq = indices[0];
int var = symbol_table.getTypeSpecificID(getSymbIDByDerivID(indices[1]));
int lag = getLagByDerivID(indices[1]);
endo_derivatives[{ eq, var, lag }] = d1;
}
return endo_derivatives;
}
void
DynamicModel::runTrendTest(const eval_context_t &eval_context)
{
@ -4816,7 +4801,7 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
if (linear_decomposition)
{
first_order_endo_derivatives = collect_first_order_derivatives_endogenous();
first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
is_equation_linear = equationLinear(first_order_endo_derivatives);
tie(contemporaneous_jacobian, static_jacobian) = evaluateAndReduceJacobian(eval_context, cutoff, false);
@ -4856,7 +4841,7 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
computePrologueAndEpilogue(static_jacobian);
first_order_endo_derivatives = collect_first_order_derivatives_endogenous();
first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
equationTypeDetermination(first_order_endo_derivatives, mfs);

View File

@ -162,8 +162,6 @@ private:
void computeDynJacobianCols(bool jacobianExo);
//! Computes derivatives of the Jacobian w.r. to trend vars and tests that they are equal to zero
void testTrendDerivativesEqualToZero(const eval_context_t &eval_context);
//! Collect only the first derivatives
map<tuple<int, int, int>, expr_t> collect_first_order_derivatives_endogenous();
//! Allocates the derivation IDs for all dynamic variables of the model
/*! Also computes max_{endo,exo}_{lead_lag}, and initializes dynJacobianColsNbr to the number of dynamic endos */

View File

@ -2413,3 +2413,18 @@ ModelTree::reorderAuxiliaryEquations()
for (int i = 0; i < n; i++)
aux_equations[i] = aux_equations_old[index[ordered[i]]];
}
map<tuple<int, int, int>, expr_t>
ModelTree::collectFirstOrderDerivativesEndogenous()
{
map<tuple<int, int, int>, expr_t> endo_derivatives;
for (auto &[indices, d1] : derivatives[1])
if (getTypeByDerivID(indices[1]) == SymbolType::endogenous)
{
int eq = indices[0];
int var = symbol_table.getTypeSpecificID(getSymbIDByDerivID(indices[1]));
int lag = getLagByDerivID(indices[1]);
endo_derivatives[{ eq, var, lag }] = d1;
}
return endo_derivatives;
}

View File

@ -365,6 +365,10 @@ protected:
virtual int getBlockInitialOtherEndogenousID(int block_number, int variable_number) const = 0;
//! Initialize equation_reordered & variable_reordered
void initializeVariablesAndEquations();
//! Returns the 1st derivatives w.r.t. endogenous in a different format
/*! Returns a map (equation, type-specific ID, lag) → derivative.
Assumes that derivatives have already been computed. */
map<tuple<int, int, int>, expr_t> collectFirstOrderDerivativesEndogenous();
private:
//! Internal helper for the copy constructor and assignment operator

View File

@ -1087,23 +1087,6 @@ StaticModel::Write_Inf_To_Bin_File_Block(const string &basename, int num,
SaveCode.close();
}
map<tuple<int, int, int>, expr_t>
StaticModel::collect_first_order_derivatives_endogenous()
{
map<tuple<int, int, int>, expr_t> endo_derivatives;
for (auto & [indices, d1] : derivatives[1])
{
if (getTypeByDerivID(indices[1]) == SymbolType::endogenous)
{
int eq = indices[0];
int var = symbol_table.getTypeSpecificID(getSymbIDByDerivID(indices[1]));
int lag = 0;
endo_derivatives[{ eq, var, lag }] = d1;
}
}
return endo_derivatives;
}
void
StaticModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool bytecode)
{
@ -1153,7 +1136,7 @@ StaticModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_co
computePrologueAndEpilogue(static_jacobian);
auto first_order_endo_derivatives = collect_first_order_derivatives_endogenous();
auto first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
equationTypeDetermination(first_order_endo_derivatives, mfs);

View File

@ -91,8 +91,6 @@ private:
map<tuple<int, int, int, int, int>, int> get_Derivatives(int block);
//! Computes chain rule derivatives of the Jacobian w.r. to endogenous variables
void computeChainRuleJacobian();
//! Collect only the first derivatives
map<tuple<int, int, int>, expr_t> collect_first_order_derivatives_endogenous();
//! Collecte the derivatives w.r. to endogenous of the block, to endogenous of previouys blocks and to exogenous
void collect_block_first_order_derivatives();