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-2020 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>(n, n)
44 {}
45 
46 
47 template<class Type>
49 (
50  const label n,
51  const Foam::zero
52 )
53 :
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 :
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, 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, 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, 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 {
208  return autoPtr<SquareMatrix<Type>>::New(*this);
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(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 
234 template<class Type>
235 inline void Foam::SquareMatrix<Type>::setSize(const label m)
236 {
238 }
239 
240 
241 template<class Type>
242 inline void Foam::SquareMatrix<Type>::shallowResize(const label m)
243 {
244  Matrix<SquareMatrix<Type>, Type>::shallowResize(m, m);
245 }
246 
247 
248 template<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 
265 template<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 
293 template<class Type>
295 {
296  Matrix<SquareMatrix<Type>, Type>::operator=(Zero);
297 }
298 
299 
300 template<class Type>
301 inline void Foam::SquareMatrix<Type>::operator=(const Type& val)
302 {
303  Matrix<SquareMatrix<Type>, Type>::operator=(val);
304 }
305 
306 
307 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 
309 namespace Foam
310 {
311 
312 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
313 
314 // Return the outer product of Field-Field as SquareMatrix
315 template<class Type>
317 (
318  const Field<Type>& f1,
319  const Field<Type>& f2
320 )
321 {
322  SquareMatrix<Type> f1f2T(f1.size());
323 
324  for (label i = 0; i < f1f2T.m(); ++i)
325  {
326  for (label j = 0; j < f1f2T.n(); ++j)
327  {
328  f1f2T(i, j) = f1[i]*f2[j];
329  }
330  }
331 
332  return f1f2T;
333 }
334 
335 
336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
337 
338 } // End namespace Foam
339 
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 #undef CHECK_MATRIX_IS_SQUARE
344 
345 // ************************************************************************* //
Foam::symmOuter
Foam::SquareMatrix< Type > symmOuter(const Field< Type > &f1, const Field< Type > &f2)
Definition: SquareMatrixI.H:317
Foam::Pair::second
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:122
Foam::SquareMatrix::clone
autoPtr< SquareMatrix< Type > > clone() const
Clone.
Definition: SquareMatrixI.H:206
Foam::Matrix< RectangularMatrix< Type >, Type >::n
label n() const noexcept
The number of columns.
Definition: MatrixI.H:103
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SquareMatrix::resize
void resize(const label m)
Resize the matrix preserving the elements.
Definition: SquareMatrixI.H:215
Foam::SquareMatrix::SquareMatrix
SquareMatrix()=default
Default construct.
Foam::Matrix
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: DiagonalMatrix.H:53
n
label n
Definition: TABSMDCalcMethod2.H:31
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:249
Foam::RectangularMatrix
A templated (M x N) rectangular matrix of objects of <Type>, containing M*N elements,...
Definition: RectangularMatrix.H:57
Foam::FatalError
error FatalError
Foam::SquareMatrix::tridiagonal
bool tridiagonal() const
Return true if the square matrix is reduced tridiagonal.
Definition: SquareMatrixI.H:266
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::SquareMatrix::operator=
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
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 (N x N) square matrix of objects of <Type>, containing N*N elements, derived from Matrix.
Definition: SquareMatrix.H:63
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::Matrix< RectangularMatrix< Type >, Type >::m
label m() const noexcept
The number of rows.
Definition: MatrixI.H:96
Foam::Pair< label >
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
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:235
Foam::ConstMatrixBlock
Definition: Matrix.H:65
Foam::SquareMatrix::shallowResize
void shallowResize(const label m)
Resize the matrix without reallocating storage (unsafe)
Definition: SquareMatrixI.H:242
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62