SquareMatrix.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::SquareMatrix
29 
30 Description
31  A templated (N x N) square matrix of objects of <Type>,
32  containing N*N elements, derived from Matrix.
33 
34 See also
35  Test-SquareMatrix.C
36 
37 SourceFiles
38  SquareMatrixI.H
39  SquareMatrix.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef Foam_SquareMatrix_H
44 #define Foam_SquareMatrix_H
45 
46 #include "Matrix.H"
47 #include "Identity.H"
48 #include <numeric>
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 template<class Type> class RectangularMatrix;
57 
58 
59 /*---------------------------------------------------------------------------*\
60  Class SquareMatrix Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 template<class Type>
64 class SquareMatrix
65 :
66  public Matrix<SquareMatrix<Type>, Type>
67 {
68 public:
69 
70  // Generated Methods
71 
72  //- Default construct
73  SquareMatrix() = default;
74 
75  //- Copy construct
76  SquareMatrix(const SquareMatrix&) = default;
77 
78  //- Copy assignment
79  SquareMatrix& operator=(const SquareMatrix&) = default;
80 
81 
82  // Constructors
83 
84  //- Construct for given size (rows == cols)
85  inline explicit SquareMatrix(const label n);
86 
87  //- Construct for given size (rows == cols)
88  //- initializing all elements to zero
89  inline SquareMatrix(const label n, const Foam::zero);
90 
91  //- Construct for given size (rows == cols)
92  //- initializing all elements to the given value
93  inline SquareMatrix(const label n, const Type& val);
94 
95  //- Construct for given size (rows == cols)
96  //- initializing to the identity matrix
97  template<class AnyType>
98  inline SquareMatrix(const label n, const Identity<AnyType>);
99 
100  //- Construct for given size (rows == cols) by using a labelPair
101  //- initializing to the identity matrix
102  template<class AnyType>
103  inline SquareMatrix(const labelPair& dims, const Identity<AnyType>);
104 
105  //- Construct given number of rows/columns
106  //- by using a labelPair (checked to be equal)
107  // For constructor consistency in rectangular matrices
108  inline explicit SquareMatrix(const labelPair& dims);
109 
110  //- Construct given number of rows/columns
111  //- by using a labelPair (checked to be equal)
112  //- and initializing all elements to zero
113  // For constructor consistency with rectangular matrices
114  inline SquareMatrix(const labelPair& dims, const Foam::zero);
115 
116  //- Construct given number of rows/columns
117  //- by using a labelPair (checked to be equal)
118  //- and initializing all elements to the given value
119  // For constructor consistency with rectangular matrices
120  inline SquareMatrix(const labelPair& dims, const Type& val);
121 
122  //- Construct given number of rows/columns (checked to be equal)
123  //- initializing all elements to zero
124  inline SquareMatrix(const label m, const label n, const Foam::zero);
125 
126  //- Construct from const sub-matrix block
127  template<class MatrixType>
128  inline SquareMatrix(const ConstMatrixBlock<MatrixType>& mat);
129 
130  //- Construct from sub-matrix block
131  template<class MatrixType>
132  inline SquareMatrix(const MatrixBlock<MatrixType>& mat);
133 
134  //- Construct as copy of a RectangularMatrix
135  //- which is checked to be square
136  inline explicit SquareMatrix(const RectangularMatrix<Type>& mat);
137 
138  //- Construct from Istream
139  inline explicit SquareMatrix(Istream& is);
140 
141  //- Clone
142  inline autoPtr<SquareMatrix<Type>> clone() const;
143 
144 
145  // Member Functions
146 
147  // Edit
148 
149  //- Resize the matrix preserving the elements
150  inline void resize(const label m);
151 
152  //- Resize the matrix \em without preserving existing content
153  inline void resize_nocopy(const label n);
154 
155  //- Resize the matrix preserving the elements (compatibility)
156  inline void resize(const label m, const label n);
157 
158  //- Resize the matrix preserving the elements
159  inline void setSize(const label m);
160 
161  //- Resize the matrix without reallocating storage (unsafe)
162  inline void shallowResize(const label m);
163 
164  // Check
165 
166  //- Return true if the square matrix is effectively symmetric/Hermitian
167  inline bool symmetric() const;
168 
169  //- Return true if the square matrix is reduced tridiagonal
170  inline bool tridiagonal() const;
171 
172  // Sort
173 
174  //- Return a sort permutation using the given comparison operator
175  //- on the diagonal entries
176  template<class CompOp>
177  labelList sortPermutation(CompOp& compare) const;
178 
179  //- Column-reorder this Matrix according to the given permutation
180  void applyPermutation(const labelUList& p);
181 
182 
183  // Member Operators
184 
185  //- Move assignment
186  inline void operator=(SquareMatrix<Type>&& mat);
187 
188  //- Assign all elements to zero
189  inline void operator=(const Foam::zero);
190 
191  //- Assign all elements to value
192  inline void operator=(const Type& val);
193 
194  //- Set to identity matrix
195  template<class AnyType>
196  void operator=(const Identity<AnyType>);
197 };
198 
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 } // End namespace Foam
203 
204 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
205 
206 #include "SquareMatrixI.H"
207 
208 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
209 
210 #ifdef NoRepository
211  #include "SquareMatrix.C"
212 #endif
213 
214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 
216 #endif
217 
218 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
autoPtr< SquareMatrix< Type > > clone() const
Clone.
A templated block of an (m x n) matrix of type <MatrixType>.
Definition: Matrix.H:62
bool symmetric() const
Return true if the square matrix is effectively symmetric/Hermitian.
void shallowResize(const label m)
Resize the matrix without reallocating storage (unsafe)
label n() const noexcept
The number of columns.
Definition: Matrix.H:258
void resize_nocopy(const label n)
Resize the matrix without preserving existing content.
A templated (M x N) rectangular matrix of objects of <Type>, containing M*N elements, derived from Matrix.
labelList sortPermutation(CompOp &compare) const
Return a sort permutation using the given comparison operator on the diagonal entries.
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order: ...
SquareMatrix()=default
Default construct.
void setSize(const label m)
Resize the matrix preserving the elements.
label m() const noexcept
The number of rows.
Definition: Matrix.H:248
void resize(const label m)
Resize the matrix preserving the elements.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Templated identity and dual space identity tensors derived from SphericalTensor.
Definition: Identity.H:43
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
volScalarField & p
A templated (N x N) square matrix of objects of <Type>, containing N*N elements, derived from Matrix...
Definition: SquareMatrix.H:59
bool tridiagonal() const
Return true if the square matrix is reduced tridiagonal.
Namespace for OpenFOAM.
void applyPermutation(const labelUList &p)
Column-reorder this Matrix according to the given permutation.
Definition: SquareMatrix.C:47