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 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>
42 :
43  Matrix<SquareMatrix<Type>, Type>()
44 {}
45 
46 
47 template<class Type>
49 :
50  Matrix<SquareMatrix<Type>, Type>(n, n)
51 {}
52 
53 
54 template<class Type>
56 (
57  const label n,
58  const zero
59 )
60 :
62 {}
63 
64 
65 template<class Type>
67 (
68  const label n,
69  const Type& val
70 )
71 :
73 {}
74 
75 
76 template<class Type>
77 template<class AnyType>
79 (
80  const label n,
81  const Identity<AnyType>
82 )
83 :
85 {
86  for (label i=0; i < n; ++i)
87  {
88  this->operator()(i, i) = pTraits<Type>::one;
89  }
90 }
91 
92 
93 template<class Type>
94 template<class AnyType>
96 (
97  const labelPair& dims,
98  const Identity<AnyType>
99 )
100 :
101  Matrix<SquareMatrix<Type>, Type>(dims, Zero)
102 {
103  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
104 
105  for (label i = 0; i < dims.first(); ++i)
106  {
107  this->operator()(i, i) = pTraits<Type>::one;
108  }
109 }
110 
111 
112 template<class Type>
114 (
115  const labelPair& dims
116 )
117 :
118  Matrix<SquareMatrix<Type>, Type>(dims)
119 {
120  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
121 }
122 
123 
124 template<class Type>
126 (
127  const labelPair& dims,
128  const zero
129 )
130 :
131  Matrix<SquareMatrix<Type>, Type>(dims, Zero)
132 {
133  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
134 }
135 
136 
137 template<class Type>
139 (
140  const labelPair& dims,
141  const Type& val
142 )
143 :
144  Matrix<SquareMatrix<Type>, Type>(dims, val)
145 {
146  CHECK_MATRIX_IS_SQUARE(dims.first(), dims.second());
147 }
148 
149 
150 template<class Type>
152 (
153  const label m,
154  const label n,
155  const zero
156 )
157 :
158  Matrix<SquareMatrix<Type>, Type>(m, n, Zero)
159 {
161 }
162 
163 
164 template<class Type>
165 template<class MatrixType>
167 (
169 )
170 :
171  Matrix<SquareMatrix<Type>, Type>(mat)
172 {
173  // Check is square?
174 }
175 
176 
177 template<class Type>
178 template<class MatrixType>
180 (
181  const MatrixBlock<MatrixType>& mat
182 )
183 :
184  Matrix<SquareMatrix<Type>, Type>(mat)
185 {
186  // Check is square?
187 }
188 
189 
190 template<class Type>
192 (
193  const RectangularMatrix<Type>& mat
194 )
195 :
196  Matrix<SquareMatrix<Type>, Type>(mat)
197 {
198  CHECK_MATRIX_IS_SQUARE(mat.m(), mat.n());
199 }
200 
201 
202 template<class Type>
204 :
205  Matrix<SquareMatrix<Type>, Type>(is)
206 {
207  CHECK_MATRIX_IS_SQUARE(this->m(), this->n());
208 }
209 
210 
211 template<class Type>
214 {
215  return autoPtr<SquareMatrix<Type>>::New(*this);
216 }
217 
218 
219 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
220 
221 template<class Type>
223 {
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>
243 {
245 }
246 
247 
248 template<class Type>
250 {
251  Matrix<SquareMatrix<Type>, Type>::shallowResize(m, m);
252 }
253 
254 
255 template<class Type>
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>
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  Matrix<SquareMatrix<Type>, Type>::operator=(Zero);
304 }
305 
306 
307 template<class Type>
308 inline void Foam::SquareMatrix<Type>::operator=(const Type& val)
309 {
310  Matrix<SquareMatrix<Type>, Type>::operator=(val);
311 }
312 
313 
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 
316 namespace Foam
317 {
318 
319 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
320 
321 template<class Type>
323 (
324  const Field<Type>& f1,
325  const Field<Type>& f2
326 )
327 {
328  SquareMatrix<Type> f1f2T(f1.size());
329 
330  for (label i = 0; i < f1f2T.m(); ++i)
331  {
332  for (label j = 0; j < f1f2T.n(); ++j)
333  {
334  f1f2T(i, j) = f1[i]*f2[j];
335  }
336  }
337 
338  return f1f2T;
339 }
340 
341 
342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343 
344 } // End namespace Foam
345 
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 #undef CHECK_MATRIX_IS_SQUARE
350 
351 // ************************************************************************* //
Foam::symmOuter
Foam::SquareMatrix< Type > symmOuter(const Field< Type > &f1, const Field< Type > &f2)
Definition: SquareMatrixI.H:323
Foam::Pair::second
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:122
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::SquareMatrix::SquareMatrix
SquareMatrix()
Construct null.
Definition: SquareMatrixI.H:41
Foam::SquareMatrix::clone
autoPtr< SquareMatrix< Type > > clone() const
Clone.
Definition: SquareMatrixI.H:213
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::SquareMatrix::operator=
void operator=(const zero)
Assign all elements to zero.
Definition: SquareMatrixI.H:301
Foam::Matrix< RectangularMatrix< Type >, Type >::n
ConstMatrixBlock< mType > const label n
Definition: Matrix.H:525
Foam::SquareMatrix::resize
void resize(const label m)
Resize the matrix preserving the elements.
Definition: SquareMatrixI.H:222
Foam::Matrix
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: DiagonalMatrix.H:48
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
resize
patchWriters resize(patchIds.size())
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Identity
Templated identity and dual space identity tensors derived from SphericalTensor.
Definition: Identity.H:49
Foam::MatrixBlock
A templated block of an (m x n) matrix of type <MatrixType>.
Definition: Matrix.H:66
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::SquareMatrix::symmetric
bool symmetric() const
Return true if the square matrix is effectively symmetric/Hermitian.
Definition: SquareMatrixI.H:256
Foam::RectangularMatrix
A templated 2D rectangular m x n matrix of objects of <Type>.
Definition: RectangularMatrix.H:56
Foam::FatalError
error FatalError
Foam::SquareMatrix::tridiagonal
bool tridiagonal() const
Return true if the square matrix is reduced tridiagonal.
Definition: SquareMatrixI.H:273
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::SquareMatrix
A templated 2D square matrix of objects of <T>, where the n x n matrix dimension is known and used fo...
Definition: SquareMatrix.H:59
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::Matrix< RectangularMatrix< Type >, Type >::m
label m() const noexcept
The number of rows.
Definition: MatrixI.H:95
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
CHECK_MATRIX_IS_SQUARE
#define CHECK_MATRIX_IS_SQUARE(a, b)
Definition: SquareMatrixI.H:29
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::SquareMatrix::setSize
void setSize(const label m)
Resize the matrix preserving the elements.
Definition: SquareMatrixI.H:242
Foam::ConstMatrixBlock
Definition: Matrix.H:65
Foam::SquareMatrix::shallowResize
void shallowResize(const label m)
Resize the matrix without reallocating storage (unsafe)
Definition: SquareMatrixI.H:249
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61