1 /*
2  * Copyright © 2004 Ondra Kamenik
3  * Copyright © 2019 Dynare Team
4  *
5  * This file is part of Dynare.
6  *
7  * Dynare is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Dynare is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "t_polynomial.hh"
22 #include "kron_prod.hh"
23 
24 // PowerProvider::getNext() unfolded code
25 /* This method constructs unfolded ‘ut’ of higher dimension, deleting
26    the previous. */
27 
28 const URSingleTensor &
getNext(dummy<URSingleTensor>)29 PowerProvider::getNext(dummy<URSingleTensor>)
30 {
31   if (ut)
32     {
33       auto ut_new = std::make_unique<URSingleTensor>(nv, ut->dimen()+1);
34       KronProd::kronMult(ConstVector(origv), ConstVector(ut->getData()), ut_new->getData());
35       ut = std::move(ut_new);
36     }
37   else
38     {
39       ut = std::make_unique<URSingleTensor>(nv, 1);
40       ut->getData() = origv;
41     }
42   return *ut;
43 }
44 
45 // PowerProvider::getNext() folded code
46 /* This method just constructs next unfolded ‘ut’ and creates folded ‘ft’. */
47 
48 const FRSingleTensor &
getNext(dummy<FRSingleTensor>)49 PowerProvider::getNext(dummy<FRSingleTensor>)
50 {
51   getNext<URSingleTensor>();
52   ft = std::make_unique<FRSingleTensor>(*ut);
53   return *ft;
54 }
55 
UTensorPolynomial(const FTensorPolynomial & fp)56 UTensorPolynomial::UTensorPolynomial(const FTensorPolynomial &fp)
57   : TensorPolynomial<UFSTensor, UGSTensor, URSingleTensor>(fp.nrows(), fp.nvars())
58 {
59   for (const auto &it : fp)
60     insert(std::make_unique<UFSTensor>(*(it.second)));
61 }
62 
FTensorPolynomial(const UTensorPolynomial & up)63 FTensorPolynomial::FTensorPolynomial(const UTensorPolynomial &up)
64   : TensorPolynomial<FFSTensor, FGSTensor, FRSingleTensor>(up.nrows(), up.nvars())
65 {
66   for (const auto &it : up)
67     insert(std::make_unique<FFSTensor>(*(it.second)));
68 }
69