TCM: remove useless 3rd dimension of A0 and A0star (supposedly for lags)

fix-tolerance-parameters
Sébastien Villemot 2022-01-28 16:38:50 +01:00
parent 1e77f7c5a7
commit 01bea3f5e7
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
6 changed files with 28 additions and 29 deletions

View File

@ -3568,15 +3568,15 @@ DynamicModel::fillTrendComponentModelTable() const
trend_component_model_table.setVals(eqnums, trend_eqnums, lhsr, lhs_expr_tr);
}
pair<map<string, map<tuple<int, int, int>, expr_t>>, map<string, map<tuple<int, int, int>, expr_t>>>
pair<map<string, map<tuple<int, int>, expr_t>>, map<string, map<tuple<int, int>, expr_t>>>
DynamicModel::computeErrorComponentMatrices(const ExprNode::subst_table_t &diff_subst_table) const
{
map<string, map<tuple<int, int, int>, expr_t>> A0r, A0starr;
map<string, map<tuple<int, int>, expr_t>> A0r, A0starr;
for (const auto &[model_name, eqns] : trend_component_model_table.getEqNums())
{
int i = 0;
map<tuple<int, int, int>, expr_t> A0, A0star;
map<tuple<int, int>, expr_t> A0, A0star;
vector<int> target_lhs = trend_component_model_table.getTargetLhs(model_name);
vector<int> nontarget_eqnums = trend_component_model_table.getNonTargetEqNums(model_name);
vector<int> undiff_nontarget_lhs = getUndiffLHSForPac(model_name, diff_subst_table);

View File

@ -294,7 +294,7 @@ private:
//! Compute error component matrices of trend component_models
/*! Returns a pair (A0r, A0starr) */
pair<map<string, map<tuple<int, int, int>, expr_t>>, map<string, map<tuple<int, int, int>, expr_t>>> computeErrorComponentMatrices(const ExprNode::subst_table_t &diff_subst_table) const;
pair<map<string, map<tuple<int, int>, expr_t>>, map<string, map<tuple<int, int>, expr_t>>> computeErrorComponentMatrices(const ExprNode::subst_table_t &diff_subst_table) const;
/* For a VAR model, given the symbol ID of a LHS variable, and a (negative)
lag, returns all the corresponding deriv_ids (by properly dealing with two

View File

@ -290,8 +290,8 @@ void
ExprNode::fillErrorCorrectionRow(int eqn,
const vector<int> &nontarget_lhs,
const vector<int> &target_lhs,
map<tuple<int, int, int>, expr_t> &A0,
map<tuple<int, int, int>, expr_t> &A0star) const
map<tuple<int, int>, expr_t> &A0,
map<tuple<int, int>, expr_t> &A0star) const
{
vector<pair<expr_t, int>> terms;
decomposeAdditiveTerms(terms, 1);
@ -330,6 +330,11 @@ ExprNode::fillErrorCorrectionRow(int eqn,
auto [orig_vid, orig_lag] = datatree.symbol_table.unrollDiffLeadLagChain(var_id, lag);
if (find(target_lhs.begin(), target_lhs.end(), orig_vid) == target_lhs.end())
{
if (orig_lag != -1)
{
cerr << "ERROR in trend component model: variables in the error correction term should appear with a lag of -1" << endl;
exit(EXIT_FAILURE);
}
// This an LHS variable, so fill A0
if (constant != 1)
{
@ -342,13 +347,13 @@ ExprNode::fillErrorCorrectionRow(int eqn,
exit(EXIT_FAILURE);
}
int colidx = static_cast<int>(distance(nontarget_lhs.begin(), find(nontarget_lhs.begin(), nontarget_lhs.end(), orig_vid)));
if (A0.find({eqn, -orig_lag, colidx}) != A0.end())
if (A0.find({eqn, colidx}) != A0.end())
{
cerr << "ExprNode::fillErrorCorrection: Error filling A0 matrix: "
<< "lag/symb_id encountered more than once in equation" << endl;
<< "symb_id encountered more than once in equation" << endl;
exit(EXIT_FAILURE);
}
A0[{eqn, -orig_lag, colidx}] = datatree.AddVariable(m.first);
A0[{eqn, colidx}] = datatree.AddVariable(m.first);
}
else
{
@ -357,7 +362,7 @@ ExprNode::fillErrorCorrectionRow(int eqn,
expr_t e = datatree.AddTimes(datatree.AddVariable(m.first), datatree.AddPossiblyNegativeConstant(-constant));
if (param_id != -1)
e = datatree.AddTimes(e, datatree.AddVariable(param_id));
if (auto coor = tuple(eqn, -orig_lag, colidx); A0star.find(coor) == A0star.end())
if (auto coor = make_pair(eqn, colidx); A0star.find(coor) == A0star.end())
A0star[coor] = e;
else
A0star[coor] = datatree.AddPlus(e, A0star[coor]);

View File

@ -669,8 +669,8 @@ public:
//! Fills the EC matrix structure
void fillErrorCorrectionRow(int eqn, const vector<int> &nontarget_lhs, const vector<int> &target_lhs,
map<tuple<int, int, int>, expr_t> &A0,
map<tuple<int, int, int>, expr_t> &A0star) const;
map<tuple<int, int>, expr_t> &A0,
map<tuple<int, int>, expr_t> &A0star) const;
//! Replaces variables found in BinaryOpNode::findConstantEquations() with their constant values
virtual expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const = 0;

View File

@ -113,8 +113,8 @@ TrendComponentModelTable::setAR(map<string, map<tuple<int, int, int>, expr_t>> A
}
void
TrendComponentModelTable::setA0(map<string, map<tuple<int, int, int>, expr_t>> A0_arg,
map<string, map<tuple<int, int, int>, expr_t>> A0star_arg)
TrendComponentModelTable::setA0(map<string, map<tuple<int, int>, expr_t>> A0_arg,
map<string, map<tuple<int, int>, expr_t>> A0star_arg)
{
A0 = move(A0_arg);
A0star = move(A0star_arg);
@ -361,30 +361,24 @@ TrendComponentModelTable::writeOutput(const string &basename, ostream &output) c
ar_ec_output << ";" << endl;
}
int a0_lag = 0;
for (const auto &[key, expr] : A0.at(name))
a0_lag = max(a0_lag, get<1>(key));
ar_ec_output << endl
<< " % A0" << endl
<< " A0 = zeros(" << nontarget_lhs_vec.size() << ", " << nontarget_lhs_vec.size() << ", " << a0_lag << ");" << endl;
<< " A0 = zeros(" << nontarget_lhs_vec.size() << ", " << nontarget_lhs_vec.size() << ");" << endl;
for (const auto &[key, expr] : A0.at(name))
{
auto [eqn, lag, colidx] = key;
ar_ec_output << " A0(" << eqn + 1 << ", " << colidx + 1 << ", " << lag << ") = ";
auto [eqn, colidx] = key;
ar_ec_output << " A0(" << eqn + 1 << ", " << colidx + 1 << ") = ";
expr->writeOutput(ar_ec_output, ExprNodeOutputType::matlabDynamicModel);
ar_ec_output << ";" << endl;
}
int a0star_lag = 0;
for (const auto &[key, expr] : A0star.at(name))
a0star_lag = max(a0star_lag, get<1>(key));
ar_ec_output << endl
<< " % A0star" << endl
<< " A0star = zeros(" << nontarget_lhs_vec.size() << ", " << target_lhs_vec.size() << ", " << a0star_lag << ");" << endl;
<< " A0star = zeros(" << nontarget_lhs_vec.size() << ", " << target_lhs_vec.size() << ");" << endl;
for (const auto &[key, expr] : A0star.at(name))
{
auto [eqn, lag, colidx] = key;
ar_ec_output << " A0star(" << eqn + 1 << ", " << colidx + 1 << ", " << lag << ") = ";
auto [eqn, colidx] = key;
ar_ec_output << " A0star(" << eqn + 1 << ", " << colidx + 1 << ") = ";
expr->writeOutput(ar_ec_output, ExprNodeOutputType::matlabDynamicModel);
ar_ec_output << ";" << endl;
}

View File

@ -53,7 +53,7 @@ private:
map<string, map<tuple<int, int, int>, expr_t>> AR; // name -> (eqn, lag, lhs_symb_id) -> expr_t
/* Note that A0 in the trend-component model context is not the same thing as
in the structural VAR context. */
map<string, map<tuple<int, int, int>, expr_t>> A0, A0star; // name -> (eqn, lag, col) -> expr_t
map<string, map<tuple<int, int>, expr_t>> A0, A0star; // name -> (eqn, col) -> expr_t
public:
explicit TrendComponentModelTable(SymbolTable &symbol_table_arg);
@ -91,8 +91,8 @@ public:
void setOrigDiffVar(map<string, vector<int>> orig_diff_var_arg);
void setTargetVar(map<string, vector<int>> target_vars_arg);
void setAR(map<string, map<tuple<int, int, int>, expr_t>> AR_arg);
void setA0(map<string, map<tuple<int, int, int>, expr_t>> A0_arg,
map<string, map<tuple<int, int, int>, expr_t>> A0star_arg);
void setA0(map<string, map<tuple<int, int>, expr_t>> A0_arg,
map<string, map<tuple<int, int>, expr_t>> A0star_arg);
//! Write output of this class
void writeOutput(const string &basename, ostream &output) const;