Loading...
Searching...
No Matches
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,
53:
54 Matrix<SquareMatrix<Type>, Type>(n, n, Foam::zero{})
55{}
56
57
58template<class Type>
60(
61 const label n,
62 const Type& val
63)
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 SquareMatrix<Type>(n, Foam::zero{})
78{
79 for (label i = 0; i < n; ++i)
80 {
82 }
83}
84
85
86template<class Type>
87template<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
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());
114}
115
116
117template<class Type>
119(
120 const labelPair& dims,
122)
123:
124 Matrix<SquareMatrix<Type>, Type>(dims, Foam::zero{})
125{
126 CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
127}
128
129
130template<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
143template<class Type>
145(
146 const label m,
147 const label n,
149)
150:
151 Matrix<SquareMatrix<Type>, Type>(m, n, Foam::zero{})
154}
155
156
157template<class Type>
158template<class MatrixType>
160(
162)
163:
164 Matrix<SquareMatrix<Type>, Type>(mat)
166 // Check is square?
167}
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
183template<class Type>
185(
186 const RectangularMatrix<Type>& mat
188:
189 Matrix<SquareMatrix<Type>, Type>(mat)
190{
191 CHECK_MATRIX_IS_SQUARE(mat.m(), mat.n());
192}
193
194
195template<class Type>
198 Matrix<SquareMatrix<Type>, Type>(is)
200 CHECK_MATRIX_IS_SQUARE(this->m(), this->n());
201}
203
204template<class Type>
207{
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>
223{
225}
226
227
228template<class Type>
229inline 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);
236
238}
239
240
241template<class Type>
242inline void Foam::SquareMatrix<Type>::setSize(const label m)
243{
245}
246
247
248template<class Type>
250{
252}
253
254
255template<class Type>
256inline 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 }
267 }
268 return true;
269}
270
271
272template<class Type>
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
300template<class Type>
302{
303 this->transfer(mat);
304}
305
306
307template<class Type>
312
313
314template<class Type>
315inline void Foam::SquareMatrix<Type>::operator=(const Type& val)
316{
317 Matrix<SquareMatrix<Type>, Type>::operator=(val);
318}
319
320
321// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322
323namespace Foam
324{
325
326// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
327
328// Return the outer product of Field-Field as SquareMatrix
329template<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// ************************************************************************* //
#define CHECK_MATRIX_IS_SQUARE(a, b)
label n
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
Templated identity and dual space identity tensors derived from SphericalTensor.
Definition Identity.H:46
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A templated block of an (m x n) matrix of type <MatrixType>.
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition Matrix.H:77
const Type & operator()(const label irow, const label jcol) const
Definition MatrixI.H:559
void transfer(Matrix< SquareMatrix< Type >, Type > &mat)
Definition Matrix.C:294
const T & first() const noexcept
Access the first element.
Definition Pair.H:137
const T & second() const noexcept
Access the second element.
Definition Pair.H:147
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.
bool symmetric() const
Return true if the square matrix is effectively symmetric/Hermitian.
void resize_nocopy(const label n)
Resize the matrix without preserving existing content.
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 resize(const label m)
Resize the matrix preserving the elements.
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
Namespace for OpenFOAM.
Pair< label > labelPair
A pair of labels.
Definition Pair.H:54
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.
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:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...