From 0f37649755932784b4e27bc70373061ac1dfc0c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 19 Feb 2019 12:53:02 +0100 Subject: [PATCH] Dynare++ tensor library: modernize normal moments computation --- dynare++/tl/cc/normal_moments.cc | 48 +++++++++++++++----------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/dynare++/tl/cc/normal_moments.cc b/dynare++/tl/cc/normal_moments.cc index f3b7b0877..3f6f96853 100644 --- a/dynare++/tl/cc/normal_moments.cc +++ b/dynare++/tl/cc/normal_moments.cc @@ -1,5 +1,7 @@ // Copyright 2004, Ondra Kamenik +#include + #include "normal_moments.hh" #include "permutation.hh" #include "kron_prod.hh" @@ -30,16 +32,15 @@ UNormalMoments::generateMoments(int maxdim, const TwoDMatrix &v) auto *mom2 = new URSingleTensor(nv, 2); mom2->getData() = v.getData(); insert(mom2); - auto *kronv = new URSingleTensor(nv, 2); + auto kronv = std::make_unique(nv, 2); kronv->getData() = v.getData(); for (int d = 4; d <= maxdim; d += 2) { - auto *newkronv = new URSingleTensor(nv, d); + auto newkronv = std::make_unique(nv, d); KronProd::kronMult(ConstVector(v.getData()), ConstVector(kronv->getData()), newkronv->getData()); - delete kronv; - kronv = newkronv; + kronv = std::move(newkronv); auto *mom = new URSingleTensor(nv, d); // apply $F_n$ to |kronv| /* Here we go through all equivalences, select only those having 2 @@ -52,24 +53,21 @@ UNormalMoments::generateMoments(int maxdim, const TwoDMatrix &v) how the |Equivalence::apply| method works. */ mom->zeros(); const EquivalenceSet eset = ebundle.get(d); - for (const auto & cit : eset) - { - if (selectEquiv(cit)) - { - Permutation per(cit); - per.inverse(); - for (Tensor::index it = kronv->begin(); it != kronv->end(); ++it) - { - IntSequence ind(kronv->dimen()); - per.apply(it.getCoor(), ind); - Tensor::index it2(*mom, ind); - mom->get(*it2, 0) += kronv->get(*it, 0); - } - } - } + for (const auto &cit : eset) + if (selectEquiv(cit)) + { + Permutation per(cit); + per.inverse(); + for (Tensor::index it = kronv->begin(); it != kronv->end(); ++it) + { + IntSequence ind(kronv->dimen()); + per.apply(it.getCoor(), ind); + Tensor::index it2(*mom, ind); + mom->get(*it2, 0) += kronv->get(*it, 0); + } + } insert(mom); } - delete kronv; } /* We return |true| for an equivalence whose each class has 2 elements. */ @@ -79,11 +77,9 @@ UNormalMoments::selectEquiv(const Equivalence &e) { if (2*e.numClasses() != e.getN()) return false; - for (const auto & si : e) - { - if (si.length() != 2) - return false; - } + for (const auto &si : e) + if (si.length() != 2) + return false; return true; } @@ -92,7 +88,7 @@ UNormalMoments::selectEquiv(const Equivalence &e) FNormalMoments::FNormalMoments(const UNormalMoments &moms) : TensorContainer(1) { - for (const auto & mom : moms) + for (const auto &mom : moms) { auto *fm = new FRSingleTensor(*(mom.second)); insert(fm);