1 //                                               -*- C++ -*-
2 /**
3  *  @brief Matrix implements the classical mathematical matrix
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 #ifndef OPENTURNS_MATRIX_HXX
22 #define OPENTURNS_MATRIX_HXX
23 
24 #include "openturns/MatrixImplementation.hxx"
25 #include "openturns/TypedInterfaceObject.hxx"
26 
27 BEGIN_NAMESPACE_OPENTURNS
28 
29 
30 class CovarianceMatrix;
31 class IdentityMatrix;
32 class SquareMatrix;
33 class SymmetricMatrix;
34 class Sample;
35 
36 /**
37  * @class Matrix
38  *
39  * Matrix implements the classical mathematical matrix
40  */
41 
42 class OT_API Matrix :
43   public TypedInterfaceObject<MatrixImplementation>
44 {
45   CLASSNAME
46 
47 #ifndef SWIG
48   /** Declaration of friend operators */
49   friend Matrix operator * (const Scalar s,
50                             const Matrix & m);
51 #endif
52 
53 
54 public:
55 
56   typedef Collection<Scalar>       ScalarCollection;
57   typedef Collection<Complex>      ComplexCollection;
58 
59   /** Default constructor */
60   Matrix();
61 
62   /** Constructor with implementation */
63   Matrix(const Implementation & i);
64 
65   /** Constructor with implementation */
66   Matrix(const MatrixImplementation & i);
67 
68   /** Constructor with size (rowDim and colDim) */
69   Matrix(const UnsignedInteger rowDim,
70          const UnsignedInteger colDim);
71 
72   /** Constructor from range of external collection */
73   template <class InputIterator>
74   Matrix(const UnsignedInteger rowDim,
75          const UnsignedInteger colDim,
76          const InputIterator first,
77          const InputIterator last);
78 
79   /** Constructor from external collection */
80   /** If the dimensions of the matrix and of the collection */
81   /** do not correspond, either the collection is truncated */
82   /** or the rest of the matrix is filled with zeros */
83   Matrix(const UnsignedInteger rowDim,
84          const UnsignedInteger colDim,
85          const ScalarCollection & elementsValues);
86 
87   /** Constructor from symmetric matrix */
88   Matrix(const SymmetricMatrix & symmetric);
89 
90   /** Set small elements to zero */
91   virtual Matrix clean(const Scalar threshold) const;
92 
93   /** String converter */
94   String __repr__() const override;
95   String __str__(const String & offset = "") const override;
96 
97 #ifndef SWIG
98   /** Operator () gives access to the elements of the matrix (to modify these elements) */
99   /** The element of the matrix is designated by its row number i and its column number j */
100   Scalar & operator () (const UnsignedInteger i,
101                         const UnsignedInteger j);
102 
103   /** Operator () gives access to the elements of the matrix (read only) */
104   /** The element of the matrix is designated by its row number i and its column number j */
105   const Scalar & operator () (const UnsignedInteger i,
106                               const UnsignedInteger j) const;
107 #endif
108 
109   /** Get the dimensions of the matrix */
110   /** Number of rows */
111   UnsignedInteger getNbRows() const;
112   /** Number of columns */
113   UnsignedInteger getNbColumns() const;
114 
115   /** Matrix transpose */
116   Matrix transpose() const;
117 
118   /** Matrix reshape */
119   Matrix reshape(const UnsignedInteger newRowDim,
120                  const UnsignedInteger newColDim) const;
121   void reshapeInPlace(const UnsignedInteger newRowDim,
122                       const UnsignedInteger newColDim);
123 
124   /** Row extraction */
125   const Matrix getRow(const UnsignedInteger rowIndex) const;
126   /** Column extration */
127   const Matrix getColumn(const UnsignedInteger columnIndex) const;
128 
129   /** Matrix additions (must have the same dimensions) */
130   Matrix operator + (const Matrix & m) const;
131   Matrix operator + (const SymmetricMatrix & m) const;
132 
133   /** Matrix subtractions (must have the same dimensions) */
134   Matrix operator - (const Matrix & m) const;
135   Matrix operator - (const SymmetricMatrix & m) const;
136 
137   /** Matrix multiplications (must have consistent dimensions) */
138   Matrix operator * (const Matrix & m) const;
139   Matrix operator * (const SymmetricMatrix & m) const;
140   Matrix operator * (const IdentityMatrix & m) const;
141 
142   /** Multiplication with a Sample (must have consistent dimensions) */
143   Sample operator * (const Sample & sample) const;
144 
145   /** Multiplication with a Point (must have consistent dimensions) */
146   Point operator * (const Point & pt) const;
147 
148   /** Multiplication with a Scalar */
149   Matrix operator * (const Scalar s) const;
150 
151   /** Division by a Scalar*/
152   Matrix operator / (const Scalar s) const;
153 
154   /** Resolution of a linear system */
155   Point solveLinearSystem(const Point & b,
156                           const Bool keepIntact = true);
157   Matrix solveLinearSystem(const Matrix & b,
158                            const Bool keepIntact = true);
159 
160   /** Compute singular values */
161   Point computeSingularValues(const Bool keepIntact = true);
162 
163   Point computeSVD(Matrix & uOut,
164                    Matrix & vTOut,
165                    const Bool fullSVD = false,
166                    const Bool keepIntact = true);
167 
168   /** Build the QR factorization of the matrix */
169   virtual Matrix computeQR(Matrix & ROut,
170                            const Bool fullQR = false,
171                            const Bool keepIntact = true);
172 
173   /** Compute the associated Gram matrix */
174   virtual CovarianceMatrix computeGram(const Bool transpose = true) const;
175 
176   /** Comparison operators */
177   Bool operator == (const Matrix & rhs) const;
178 
179   /** Empty returns true if there is no element in the matrix */
180   Bool isEmpty() const;
181 
182   /** Low-level data access */
183   const Scalar * data () const;
184   UnsignedInteger elementSize() const;
185   UnsignedInteger stride(const UnsignedInteger dim) const;
186 
187 }; /* class Matrix */
188 
189 /** Declaration of friend operators */
operator *(const Scalar s,const Matrix & m)190 inline Matrix operator * (const Scalar s,
191                           const Matrix & m)
192 {
193   return m.operator * (s);
194 }
195 
196 
197 /** Constructor from range of external collection */
198 template <class InputIterator>
Matrix(const UnsignedInteger rowDim,const UnsignedInteger colDim,const InputIterator first,const InputIterator last)199 Matrix::Matrix(const UnsignedInteger rowDim,
200                const UnsignedInteger colDim,
201                const InputIterator first,
202                const InputIterator last)
203   : TypedInterfaceObject<MatrixImplementation>(new MatrixImplementation(rowDim, colDim, first, last))
204 {
205   // Nothing to do
206 }
207 
208 END_NAMESPACE_OPENTURNS
209 
210 #endif /* OPENTURNS_MATRIX_HXX */
211