1 //                                               -*- C++ -*-
2 /**
3  *  @brief SquareMatrix implements the classical mathematical square 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_SQUAREMATRIX_HXX
22 #define OPENTURNS_SQUAREMATRIX_HXX
23 
24 #include "openturns/OTprivate.hxx"
25 #include "openturns/Matrix.hxx"
26 
27 BEGIN_NAMESPACE_OPENTURNS
28 
29 class IdentityMatrix;
30 
31 class SymmetricMatrix;
32 
33 class SquareComplexMatrix;
34 
35 /**
36  * @class SquareMatrix
37  *
38  * SquareMatrix implements the classical mathematical square matrix
39  */
40 
41 class OT_API SquareMatrix :
42   public Matrix
43 {
44   CLASSNAME
45 
46 #ifndef SWIG
47   friend SquareMatrix operator * (const Scalar s,
48                                   const SquareMatrix & m);
49 #endif
50 
51 public:
52 
53   /** Default constructor */
54   SquareMatrix();
55 
56   /** Constructor with implementation */
57   SquareMatrix(const Implementation & i);
58 
59   /** Constructor with implementation */
60   SquareMatrix(const MatrixImplementation & i);
61 
62   /** Constructor with size (dim, which is the same for nbRows_ and nbColumns_) */
63   explicit SquareMatrix(const UnsignedInteger dim);
64 #if 0
65   /** Constructor from range of external collection */
66   template <class InputIterator>
67   SquareMatrix(const UnsignedInteger dim,
68                InputIterator first,
69                InputIterator last);
70 #endif
71   /** Constructor from external collection */
72   /** If the dimensions of the matrix and of the collection */
73   /** do not correspond, either the collection is truncated */
74   /** or the rest of the matrix is filled with zeros */
75   SquareMatrix(const UnsignedInteger dim,
76                const ScalarCollection & elementsValues);
77 
78   /** Constructor from symmetric matrix */
79   SquareMatrix(const SymmetricMatrix & symmetric);
80 
81   /** String converter */
82   String __repr__() const override;
83 
84   /** Get the dimension of the matrix */
85   UnsignedInteger getDimension() const;
86 
87   /** SquareMatrix transpose */
88   SquareMatrix transpose () const;
89 
90   /** SquareMatrix additions (must have the same dimensions) */
91   SquareMatrix operator + (const SquareMatrix & m) const;
92   SquareMatrix operator + (const SymmetricMatrix & m) const;
93 
94   /** SquareMatrix subtractions (must have the same dimensions) */
95   SquareMatrix operator - (const SquareMatrix & m) const;
96   SquareMatrix operator - (const SymmetricMatrix & m) const;
97 
98   /** SquareMatrix multiplications (must have consistent dimensions) */
99 #ifdef _MSC_VER   // VS2010 does not like 'using' being called after overloads
100   using Matrix::operator *;
101 #endif
102   SquareMatrix operator * (const SquareMatrix & m) const;
103   SquareMatrix operator * (const SymmetricMatrix & m) const;
104   SquareMatrix operator * (const IdentityMatrix & m) const;
105 
106   /** SquareMatrix integer power */
107   SquareMatrix power(const UnsignedInteger n) const;
108 
109   /** Multiplication with a Point (must have consistent dimensions) */
110   Point operator * (const Point & p) const;
111 
112   /** Multiplication with a Scalar */
113   SquareMatrix operator * (const Scalar s) const;
114 
115   // We import the definitions from the upper class (for Matrix multiplication)
116 #ifndef _MSC_VER   // VS2010 does not like 'using' being called after overloads
117   using Matrix::operator *;
118 #endif
119 
120   /** Division by a Scalar*/
121   SquareMatrix operator / (const Scalar s) const;
122 
123   /** Resolution of a linear system */
124   Point solveLinearSystem(const Point & b,
125                           const Bool keepIntact = true);
126 
127   Matrix solveLinearSystem(const Matrix & b,
128                            const Bool keepIntact = true);
129 
130   /** Compute determinant */
131   Scalar computeLogAbsoluteDeterminant(Scalar & signOut,
132                                        const Bool keepIntact = true);
133   Scalar computeDeterminant(const Bool keepIntact = true);
134 
135   /** Compute trace */
136   Scalar computeTrace() const;
137 
138   /** Compute eigenvalues */
139   ComplexCollection computeEigenValues(const Bool keepIntact = true);
140   ComplexCollection computeEV(SquareComplexMatrix & vOut,
141                               const Bool keepIntact = true);
142   /** Compute the largest eigenvalue module using power iterations */
143   virtual Scalar computeLargestEigenValueModule(const UnsignedInteger maximumIterations = ResourceMap::GetAsUnsignedInteger("Matrix-LargestEigenValueIterations"),
144       const Scalar epsilon = ResourceMap::GetAsScalar("Matrix-LargestEigenValueRelativeError")) const;
145 
146   /** Check if it is diagonal */
147   Bool isDiagonal() const;
148 
149 protected:
150 
151 private:
152 
153 }; /* class SquareMatrix */
154 
155 
operator *(const Scalar s,const SquareMatrix & m)156 inline SquareMatrix operator * (const Scalar s,
157                                 const SquareMatrix & m)
158 {
159   return m.operator * (s);
160 }
161 
162 
163 END_NAMESPACE_OPENTURNS
164 
165 #endif /* OPENTURNS_MATRIX_HXX */
166