1 //                                               -*- C++ -*-
2 /**
3  *  @brief ARMACoefficients class enables to stock coefficients of an ARMA process
4  *
5  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6  *
7  *  This library is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser 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  *  This library 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 Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #include "openturns/PersistentObjectFactory.hxx"
22 #include "openturns/ARMACoefficients.hxx"
23 #include "openturns/Exception.hxx"
24 #include "openturns/Os.hxx"
25 
26 BEGIN_NAMESPACE_OPENTURNS
27 
28 TEMPLATE_CLASSNAMEINIT(PersistentCollection< ARMACoefficients >)
29 TEMPLATE_CLASSNAMEINIT(PersistentCollection< SquareMatrix >)
30 static const Factory<PersistentCollection<SquareMatrix> > Factory_PersistentCollection_SquareMatrix;
31 
32 CLASSNAMEINIT(ARMACoefficients)
33 static const Factory<ARMACoefficients> Factory_ARMACoefficients;
34 
35 /* Default constructor */
ARMACoefficients(const UnsignedInteger & size,const UnsignedInteger & dimension)36 ARMACoefficients::ARMACoefficients(const UnsignedInteger & size,
37                                    const UnsignedInteger & dimension)
38   : PersistentCollection<SquareMatrix>(size, SquareMatrix(dimension) )
39   , dimension_(dimension)
40 {
41   if (dimension == 0) throw InvalidArgumentException(HERE) << "Error: dimension should be at least 1";
42 }
43 
44 
45 /* Standard constructor */
ARMACoefficients(const SquareMatrixCollection & collection)46 ARMACoefficients::ARMACoefficients(const SquareMatrixCollection & collection)
47   : PersistentCollection<SquareMatrix>(0)
48 {
49   // Adding elements one by one and checking coherance of dimension
50   const UnsignedInteger collectionSize = collection.getSize();
51   if (collectionSize == 0) throw InvalidArgumentException(HERE) << "Error: cannot build an ARMACoefficients object based on an empty collection of matrices.";
52   dimension_ = collection[0].getDimension();
53   add(collection[0]);
54   for (UnsignedInteger i = 1; i < collectionSize; ++i)
55     if (collection[i].getDimension() == dimension_)
56       add(collection[i]);
57 }
58 
59 /* Default constructor */
ARMACoefficients(const Point & scalarCoefficients)60 ARMACoefficients::ARMACoefficients(const Point & scalarCoefficients)
61   : PersistentCollection<SquareMatrix>(scalarCoefficients.getSize(), SquareMatrix(1))
62 {
63   dimension_ = 1;
64   for (UnsignedInteger i = 0 ; i < getSize() ; ++i ) (*this)[i](0, 0) = scalarCoefficients[i];
65 }
66 
67 /* constructor using polynomial */
ARMACoefficients(const UniVariatePolynomial & polynomial)68 ARMACoefficients::ARMACoefficients(const UniVariatePolynomial & polynomial)
69   : PersistentCollection<SquareMatrix>(polynomial.getDegree() + 1, SquareMatrix(1))
70 {
71   dimension_ = 1;
72   const Point coefficients(polynomial.getCoefficients());
73   for (UnsignedInteger i = 0 ; i < getSize() ; ++i ) (*this)[i](0, 0) = coefficients[i];
74 }
75 
76 /* Virtual constructor  - clone*/
clone() const77 ARMACoefficients * ARMACoefficients::clone() const
78 {
79   return new ARMACoefficients(*this);
80 }
81 
82 /* String converter */
__repr__() const83 String ARMACoefficients::__repr__() const
84 {
85   OSS oss(true);
86   oss << "class=" << ARMACoefficients::GetClassName();
87   for (UnsignedInteger i = 0; i < getSize(); ++i) oss << ", shift=" << i << ", value=" << (*this)[i];
88   return oss;
89 }
90 
__str__(const String & offset) const91 String ARMACoefficients::__str__(const String & offset) const
92 {
93   OSS oss(false);
94   for (UnsignedInteger i = 0; i < getSize(); ++i) oss << "shift = " << i << Os::GetEndOfLine() << offset << (*this)[i].__str__(offset) << Os::GetEndOfLine() << offset;
95   return oss;
96 }
97 
98 /* Dimension accessor */
getDimension() const99 UnsignedInteger  ARMACoefficients::getDimension() const
100 {
101   return dimension_;
102 }
103 
104 /** Redefinition of add method : control of SquareMatrix sizes */
add(const SquareMatrix & matrix)105 void ARMACoefficients::add(const SquareMatrix & matrix)
106 {
107   if (matrix.getDimension() != dimension_)
108     throw InvalidArgumentException(HERE) << "Could not add the coefficient. Incompatible dimension with the elements of collection";
109   PersistentCollection<SquareMatrix>::add(matrix);
110 }
111 
add(const Scalar scalar)112 void ARMACoefficients::add(const Scalar scalar)
113 {
114   if (dimension_ != 1)
115     throw InvalidArgumentException(HERE) << "Could not add the coefficient. The dimension is greater than 1.";
116   PersistentCollection<SquareMatrix>::add(SquareMatrix(1, Point(1, scalar)));
117 }
118 
119 
120 /* Method save() stores the object through the StorageManager */
save(Advocate & adv) const121 void ARMACoefficients::save(Advocate & adv) const
122 {
123   PersistentCollection<SquareMatrix>::save( adv );
124 }
125 
126 /* Method load() reloads the object from the StorageManager */
load(Advocate & adv)127 void ARMACoefficients::load(Advocate & adv)
128 {
129   PersistentCollection<SquareMatrix>::load( adv );
130 }
131 
132 END_NAMESPACE_OPENTURNS
133 
134