SquareMatrixI.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-2022 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 \*---------------------------------------------------------------------------*/
28 
29 #define CHECK_MATRIX_IS_SQUARE(a, b) \
30  if (a != b) \
31  { \
32  FatalErrorInFunction \
33  << "Attempt to create a non-square matrix (" \
34  << a << ", " << b << ')' << nl << abort(FatalError); \
35  }
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
40 template<class Type>
41 inline Foam::SquareMatrix<Type>::SquareMatrix(const label n)
42 :
43  Matrix<SquareMatrix<Type>, Type>(n, n)
44 {}
45 
46 
47 template<class Type>
49 (
50  const label n,
51  const Foam::zero
52 )
53 :
54  Matrix<SquareMatrix<Type>, Type>(n, n, Foam::zero{})
55 {}
56 
57 
58 template<class Type>
60 (
61  const label n,
62  const Type& val
63 )
64 :
65  Matrix<SquareMatrix<Type>, Type>(n, n, val)
66 {}
67 
68 
69 template<class Type>
70 template<class AnyType>
72 (
73  const label n,
74  const Identity<AnyType>
75 )
76 :
77  SquareMatrix<Type>(n, Foam::zero{})
78 {
79  for (label i = 0; i < n; ++i)
80  {
81  this->operator()(i, i) = pTraits<Type>::one;
82  }
83 }
84 
85 
86 template<class Type>
87 template<class AnyType>
89 (
90  const labelPair& dims,
91  const Identity<AnyType>
92 )
93 :
94  Matrix<SquareMatrix<Type>, Type>(dims, Foam::zero{})
95 {
96  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
97 
98  for (label i = 0; i < dims.first(); ++i)
99  {
100  this->operator()(i, i) = pTraits<Type>::one;
101  }
102 }
103 
104 
105 template<class Type>
107 (
108  const labelPair& dims
109 )
110 :
111  Matrix<SquareMatrix<Type>, Type>(dims)
112 {
113  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
114 }
115 
116 
117 template<class Type>
119 (
120  const labelPair& dims,
121  const Foam::zero
122 )
123 :
124  Matrix<SquareMatrix<Type>, Type>(dims, Foam::zero{})
125 {
126  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
127 }
128 
129 
130 template<class Type>
132 (
133  const labelPair& dims,
134  const Type& val
135 )
136 :
137  Matrix<SquareMatrix<Type>, Type>(dims, val)
138 {
139  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
140 }
141 
142 
143 template<class Type>
145 (
146  const label m,
147  const label n,
148  const Foam::zero
149 )
150 :
151  Matrix<SquareMatrix<Type>, Type>(m, n, Foam::zero{})
152 {
154 }
155 
156 
157 template<class Type>
158 template<class MatrixType>
160 (
162 )
163 :
164  Matrix<SquareMatrix<Type>, Type>(mat)
165 {
166  // Check is square?
167 }
168 
169 
170 template<class Type>
171 template<class MatrixType>
173 (
174  const MatrixBlock<MatrixType>& mat
175 )
176 :
177  Matrix<SquareMatrix<Type>, Type>(mat)
178 {
179  // Check is square?
180 }
181 
182 
183 template<class Type>
185 (
186  const RectangularMatrix<Type>& mat
187 )
188 :
189  Matrix<SquareMatrix<Type>, Type>(mat)
190 {
191  CHECK_MATRIX_IS_SQUARE(mat.m(), mat.n());
192 }
193 
194 
195 template<class Type>
197 :
198  Matrix<SquareMatrix<Type>, Type>(is)
199 {
200  CHECK_MATRIX_IS_SQUARE(this->m(), this->n());
201 }
202 
203 
204 template<class Type>
207 {
209 }
210 
211 
212 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
213 
214 template<class Type>
215 inline void Foam::SquareMatrix<Type>::resize(const label m)
216 {
218 }
219 
220 
221 template<class Type>
222 inline void Foam::SquareMatrix<Type>::resize_nocopy(const label m)
223 {
224  Matrix<SquareMatrix<Type>, Type>::resize_nocopy(m, m);
225 }
226 
227 
228 template<class Type>
229 inline void Foam::SquareMatrix<Type>::resize(const label m, const label n)
230 {
231  if (m != n)
232  {
233  FatalErrorInFunction<< "Disallowed use of resize() for SquareMatrix"
234  << abort(FatalError);
235  }
236 
238 }
239 
240 
241 template<class Type>
242 inline void Foam::SquareMatrix<Type>::setSize(const label m)
243 {
245 }
246 
247 
248 template<class Type>
249 inline void Foam::SquareMatrix<Type>::shallowResize(const label m)
250 {
251  Matrix<SquareMatrix<Type>, Type>::shallowResize(m, m);
252 }
253 
254 
255 template<class Type>
256 inline bool Foam::SquareMatrix<Type>::symmetric() const
257 {
258  for (label n = 0; n < this->n() - 1; ++n)
259  {
260  for (label m = this->m() - 1; n < m; --m)
261  {
262  if (SMALL < mag((*this)(n, m) - (*this)(m, n)))
263  {
264  return false;
265  }
266  }
267  }
268  return true;
269 }
270 
271 
272 template<class Type>
273 inline bool Foam::SquareMatrix<Type>::tridiagonal() const
274 {
275  for (label i = 0; i < this->m(); ++i)
276  {
277  for (label j = 0; j < this->n(); ++j)
278  {
279  const Type& val = (*this)(i, j);
280 
281  if ((i == j) || (i - 1 == j) || (i + 1 == j))
282  {
283  if (mag(val) < SMALL)
284  {
285  return false;
286  }
287  }
288  else if (SMALL < mag(val))
289  {
290  return false;
291  }
292  }
293  }
294  return true;
295 }
296 
297 
298 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
299 
300 template<class Type>
302 {
303  this->transfer(mat);
304 }
305 
306 
307 template<class Type>
309 {
310  Matrix<SquareMatrix<Type>, Type>::operator=(Foam::zero{});
311 }
312 
313 
314 template<class Type>
315 inline void Foam::SquareMatrix<Type>::operator=(const Type& val)
316 {
317  Matrix<SquareMatrix<Type>, Type>::operator=(val);
318 }
319 
320 
321 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322 
323 namespace Foam
324 {
325 
326 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
327 
328 // Return the outer product of Field-Field as SquareMatrix
329 template<class Type>
331 (
332  const Field<Type>& f1,
333  const Field<Type>& f2
334 )
335 {
336  SquareMatrix<Type> f1f2T(f1.size());
337 
338  for (label i = 0; i < f1f2T.m(); ++i)
339  {
340  for (label j = 0; j < f1f2T.n(); ++j)
341  {
342  f1f2T(i, j) = f1[i]*f2[j];
343  }
344  }
345 
346  return f1f2T;
347 }
348 
349 
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
351 
352 } // End namespace Foam
353 
354 
355 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356 
357 #undef CHECK_MATRIX_IS_SQUARE
358 
359 // ************************************************************************* //
const T & first() const noexcept
Access the first element.
Definition: Pair.H:137
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
patchWriters resize(patchIds.size())
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
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 traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
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
Generic templated field type.
Definition: Field.H:62
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.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order: ...
SquareMatrix()=default
Default construct.
#define CHECK_MATRIX_IS_SQUARE(a, b)
Definition: SquareMatrixI.H:22
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:51
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.
Foam::SquareMatrix< Type > symmOuter(const Field< Type > &f1, const Field< Type > &f2)
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
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.
const T & second() const noexcept
Access the second element.
Definition: Pair.H:147
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.