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-------------------------------------------------------------------------------
11License
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
40template<class Type>
42:
43 Matrix<SquareMatrix<Type>, Type>(n, n)
44{}
45
46
47template<class Type>
49(
50 const label n,
51 const Foam::zero
52)
53:
54 Matrix<SquareMatrix<Type>, Type>(n, n, Zero)
55{}
56
57
58template<class Type>
60(
61 const label n,
62 const Type& val
63)
64:
65 Matrix<SquareMatrix<Type>, Type>(n, n, val)
66{}
67
68
69template<class Type>
70template<class AnyType>
72(
73 const label n,
75)
76:
77 Matrix<SquareMatrix<Type>, Type>(n, n, Zero)
78{
79 for (label i = 0; i < n; ++i)
80 {
81 this->operator()(i, i) = pTraits<Type>::one;
82 }
83}
84
86template<class Type>
87template<class AnyType>
90 const labelPair& dims,
92)
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}
104
105template<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());
115
116
117template<class Type>
119(
120 const labelPair& dims,
121 const Foam::zero
122)
123:
124 Matrix<SquareMatrix<Type>, Type>(dims, Zero)
125{
126 CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
127}
129
130template<class Type>
133 const labelPair& dims,
134 const Type& val
135)
137 Matrix<SquareMatrix<Type>, Type>(dims, val)
138{
140}
141
143template<class Type>
145(
146 const label m,
147 const label n,
148 const Foam::zero
149)
151 Matrix<SquareMatrix<Type>, Type>(m, n, Zero)
152{
154}
155
157template<class Type>
158template<class MatrixType>
160(
162)
163:
164 Matrix<SquareMatrix<Type>, Type>(mat)
165{
166 // Check is square?
168
169
170template<class Type>
171template<class MatrixType>
173(
174 const MatrixBlock<MatrixType>& mat
175)
176:
177 Matrix<SquareMatrix<Type>, Type>(mat)
178{
179 // Check is square?
180}
181
182
183template<class Type>
185(
186 const RectangularMatrix<Type>& mat
188:
189 Matrix<SquareMatrix<Type>, Type>(mat)
191 CHECK_MATRIX_IS_SQUARE(mat.m(), mat.n());
192}
193
194
195template<class Type>
197:
198 Matrix<SquareMatrix<Type>, Type>(is)
199{
200 CHECK_MATRIX_IS_SQUARE(this->m(), this->n());
201}
202
203
204template<class Type>
207{
208 return autoPtr<SquareMatrix<Type>>::New(*this);
209}
210
211
212// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
213
214template<class Type>
215inline void Foam::SquareMatrix<Type>::resize(const label m)
216{
218}
219
220
221template<class Type>
222inline void Foam::SquareMatrix<Type>::resize(const label m, const label n)
223{
224 if (m != n)
225 {
226 FatalErrorInFunction<< "Disallowed use of resize() for SquareMatrix"
227 << abort(FatalError);
228 }
229
231}
232
233
234template<class Type>
235inline void Foam::SquareMatrix<Type>::setSize(const label m)
236{
238}
239
240
241template<class Type>
243{
244 Matrix<SquareMatrix<Type>, Type>::shallowResize(m, m);
245}
246
247
248template<class Type>
250{
251 for (label n = 0; n < this->n() - 1; ++n)
252 {
253 for (label m = this->m() - 1; n < m; --m)
254 {
255 if (SMALL < mag((*this)(n, m) - (*this)(m, n)))
256 {
257 return false;
258 }
259 }
260 }
261 return true;
262}
263
264
265template<class Type>
267{
268 for (label i = 0; i < this->m(); ++i)
269 {
270 for (label j = 0; j < this->n(); ++j)
271 {
272 const Type& val = (*this)(i, j);
273
274 if ((i == j) || (i - 1 == j) || (i + 1 == j))
275 {
276 if (mag(val) < SMALL)
277 {
278 return false;
279 }
280 }
281 else if (SMALL < mag(val))
282 {
283 return false;
284 }
285 }
286 }
287 return true;
288}
289
290
291// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
292
293template<class Type>
295{
296 this->transfer(mat);
297}
298
299
300template<class Type>
302{
303 Matrix<SquareMatrix<Type>, Type>::operator=(Zero);
304}
305
306
307template<class Type>
308inline void Foam::SquareMatrix<Type>::operator=(const Type& val)
309{
310 Matrix<SquareMatrix<Type>, Type>::operator=(val);
311}
312
313
314// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315
316namespace Foam
317{
318
319// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
320
321// Return the outer product of Field-Field as SquareMatrix
322template<class Type>
324(
325 const Field<Type>& f1,
326 const Field<Type>& f2
327)
328{
329 SquareMatrix<Type> f1f2T(f1.size());
330
331 for (label i = 0; i < f1f2T.m(); ++i)
332 {
333 for (label j = 0; j < f1f2T.n(); ++j)
334 {
335 f1f2T(i, j) = f1[i]*f2[j];
336 }
337 }
338
339 return f1f2T;
340}
341
342
343// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344
345} // End namespace Foam
346
347
348// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
349
350#undef CHECK_MATRIX_IS_SQUARE
351
352// ************************************************************************* //
#define CHECK_MATRIX_IS_SQUARE(a, b)
Definition: SquareMatrixI.H:29
label n
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
Generic templated field type.
Definition: Field.H:82
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:207
Templated identity and dual space identity tensors derived from SphericalTensor.
Definition: Identity.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A templated block of an (m x n) matrix of type <MatrixType>.
Definition: MatrixBlock.H:132
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: Matrix.H:81
label n() const noexcept
The number of columns.
Definition: MatrixI.H:103
const Type & operator()(const label irow, const label jcol) const
(i, j) const element access operator
Definition: MatrixI.H:585
label m() const noexcept
The number of rows.
Definition: MatrixI.H:96
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:120
A templated (M x N) rectangular matrix of objects of <Type>, containing M*N elements,...
A templated (N x N) square matrix of objects of <Type>, containing N*N elements, derived from Matrix.
Definition: SquareMatrix.H:66
bool symmetric() const
Return true if the square matrix is effectively symmetric/Hermitian.
SquareMatrix()=default
Default construct.
autoPtr< SquareMatrix< Type > > clone() const
Clone.
bool tridiagonal() const
Return true if the square matrix is reduced tridiagonal.
void setSize(const label m)
Resize the matrix preserving the elements.
void shallowResize(const label m)
Resize the matrix without reallocating storage (unsafe)
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
Foam::SquareMatrix< Type > symmOuter(const Field< Type > &f1, const Field< Type > &f2)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
error FatalError