Matrix.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Matrix
26 
27 Description
28  A templated (m x n) matrix of objects of <T>.
29 
30 SourceFiles
31  Matrix.C
32  MatrixI.H
33  MatrixIO.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Matrix_H
38 #define Matrix_H
39 
40 #include "bool.H"
41 #include "label.H"
42 #include "uLabel.H"
43 #include "Field.H"
44 #include "zero.H"
45 #include "autoPtr.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward declaration of friend functions and operators
53 
54 template<class Form, class Type> class Matrix;
55 
56 template<class Form, class Type> Istream& operator>>
57 (
58  Istream&,
60 );
61 
62 template<class Form, class Type> Ostream& operator<<
63 (
64  Ostream&,
65  const Matrix<Form, Type>&
66 );
67 
68 template<class MatrixType>
69 class ConstMatrixBlock;
70 
71 template<class MatrixType>
72 class MatrixBlock;
73 
74 template<class Form, class Cmpt, direction Mrows, direction Ncols>
75 class MatrixSpace;
76 
77 
78 /*---------------------------------------------------------------------------*\
79  Class Matrix Declaration
80 \*---------------------------------------------------------------------------*/
81 
82 template<class Form, class Type>
83 class Matrix
84 {
85  // Private Data
86 
87  //- Number of rows and columns in Matrix.
88  label mRows_, nCols_;
89 
90  //- Row pointers
91  Type* __restrict__ v_;
92 
93  //- Allocate the storage for the element vector
94  void allocate();
95 
96 
97 public:
98 
99  //- Matrix type
100  typedef Matrix<Form, Type> mType;
101 
102  //- Component type
103  typedef Type cmptType;
104 
105 
106  // Static Member Functions
107 
108  //- Return a null Matrix
109  inline static const mType& null();
110 
111 
112  // Constructors
113 
114  //- Null constructor.
115  inline Matrix();
116 
117  //- Construct given number of rows and columns.
118  Matrix(const label m, const label n);
119 
120  //- Construct with given number of rows and columns
121  // initialising all elements to zero
122  Matrix(const label m, const label n, const zero);
123 
124  //- Construct with given number of rows and columns
125  // initialising all elements to the given value
126  Matrix(const label m, const label n, const Type&);
127 
128  //- Construct with given number of rows and columns
129  // and start and end iterators
130  template<class InputIterator>
131  Matrix
132  (
133  const label m,
134  const label n,
135  InputIterator first,
136  InputIterator last
137  );
138 
139  //- Construct with given number of rows and columns and initialiser list
140  Matrix(const label m, const label n, std::initializer_list<Type>);
141 
142  //- Construct from initialiser list list
143  Matrix(std::initializer_list<std::initializer_list<Type>>);
144 
145  //- Copy constructor.
146  Matrix(const mType&);
147 
148  //- Copy constructor from matrix of a different form
149  template<class Form2>
150  explicit Matrix(const Matrix<Form2, Type>&);
151 
152  //- Construct from a block of another matrix
153  template<class MatrixType>
155 
156  //- Construct from a block of another matrix
157  template<class MatrixType>
159 
160  template<class MSForm, direction Mrows, direction Ncols>
162 
163  //- Construct from Istream.
164  Matrix(Istream&);
165 
166  //- Clone
167  inline autoPtr<mType> clone() const;
168 
169 
170  //- Destructor
171  ~Matrix();
172 
173 
174  // Member Functions
175 
176  // Access
177 
178  //- Return the number of rows
179  inline label m() const;
180 
181  //- Return the number of columns
182  inline label n() const;
183 
184  //- Return the number of elements in matrix (m*n)
185  inline label size() const;
186 
187  //- Return element vector of the constant Matrix
188  inline const Type* v() const;
189 
190  //- Return element vector of the Matrix
191  inline Type* v();
192 
193 
194  // Block access
195 
197  (
198  const label m,
199  const label n,
200  const label mStart,
201  const label nStart
202  ) const;
203 
204  template<class VectorSpace>
206  (
207  const label mStart,
208  const label nStart
209  ) const;
210 
212  (
213  const label m,
214  const label rowStart
215  ) const;
216 
218  (
219  const label m,
220  const label mStart,
221  const label nStart
222  ) const;
223 
224 
226  (
227  const label m,
228  const label n,
229  const label mStart,
230  const label nStart
231  );
232 
233  template<class VectorSpace>
235  (
236  const label mStart,
237  const label nStart
238  );
239 
240  inline MatrixBlock<mType> col
241  (
242  const label m,
243  const label rowStart
244  );
245 
246  inline MatrixBlock<mType> col
247  (
248  const label m,
249  const label mStart,
250  const label nStart
251  );
252 
253 
254  // Check
255 
256  //- Check index i is within valid range (0 ... m-1).
257  inline void checki(const label i) const;
258 
259  //- Check index j is within valid range (0 ... n-1).
260  inline void checkj(const label j) const;
261 
262 
263  // Edit
264 
265  //- Clear the Matrix, i.e. set sizes to zero.
266  void clear();
267 
268  //- Transfer the contents of the argument Matrix into this Matrix
269  // and annul the argument Matrix.
270  void transfer(mType&);
271 
272  //- Resize the matrix preserving the elements
273  void setSize(const label m, const label n);
274 
275  //- Resize the matrix without reallocating storage (unsafe)
276  inline void shallowResize(const label m, const label n);
277 
278 
279  //- Return the transpose of the matrix
280  Form T() const;
281 
282 
283  // Member Operators
284 
285  //- Return subscript-checked row of Matrix.
286  inline Type* operator[](const label);
287 
288  //- Return subscript-checked row of constant Matrix.
289  inline const Type* operator[](const label) const;
290 
291  //- (i, j) const element access operator
292  inline const Type& operator()(const label i, const label j) const;
293 
294  //- (i, j) element access operator
295  inline Type& operator()(const label i, const label j);
296 
297  //- Assignment operator. Takes linear time.
298  void operator=(const mType&);
299 
300  //- Assignment to a block of another matrix
301  template<class MatrixType>
303 
304  //- Assignment to a block of another matrix
305  template<class MatrixType>
306  void operator=(const MatrixBlock<MatrixType>&);
307 
308  //- Assignment of all elements to zero
309  void operator=(const zero);
310 
311  //- Assignment of all elements to the given value
312  void operator=(const Type&);
313 
314 
315  // IOstream Operators
316 
317  //- Read Matrix from Istream, discarding contents of existing Matrix.
318  friend Istream& operator>> <Form, Type>
319  (
320  Istream&,
321  mType&
322  );
323 
324  //- Write Matrix to Ostream.
325  friend Ostream& operator<< <Form, Type>
326  (
327  Ostream&,
328  const mType&
329  );
330 };
331 
332 
333 // Global functions and operators
334 
335 template<class Form, class Type>
336 void writeEntry(Ostream&, const Matrix<Form, Type>&);
337 
338 template<class Form, class Type>
339 const Type& max(const Matrix<Form, Type>&);
340 
341 template<class Form, class Type>
342 const Type& min(const Matrix<Form, Type>&);
343 
344 template<class Form, class Type>
345 Form operator-(const Matrix<Form, Type>&);
346 
347 template<class Form, class Type>
348 Form operator+
349 (
350  const Matrix<Form, Type>&,
351  const Matrix<Form, Type>&
352 );
353 
354 template<class Form, class Type>
355 Form operator-
356 (
357  const Matrix<Form, Type>&,
358  const Matrix<Form, Type>&
359 );
360 
361 template<class Form, class Type>
362 Form operator*
363 (
364  const scalar,
365  const Matrix<Form, Type>&
366 );
367 
368 template<class Form, class Type>
369 Form operator*
370 (
371  const Matrix<Form, Type>&,
372  const scalar
373 );
374 
375 template<class Form, class Type>
376 Form operator/
377 (
378  const Matrix<Form, Type>&,
379  const scalar
380 );
381 
382 template<class Form1, class Form2, class Type>
384 operator*
385 (
386  const Matrix<Form1, Type>& a,
387  const Matrix<Form2, Type>& b
388 );
389 
390 template<class Form, class Type>
391 tmp<Field<Type>> operator*
392 (
393  const Matrix<Form, Type>&,
394  const Field<Type>&
395 );
396 
397 
398 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
399 
400 } // End namespace Foam
401 
402 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
403 
404 #include "MatrixI.H"
405 
406 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407 
408 #ifdef NoRepository
409  #include "Matrix.C"
410 #endif
411 
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
414 #endif
415 
416 // ************************************************************************* //
System bool.
Pre-declare SubField and related Field type.
Definition: Field.H:83
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>.
Definition: MatrixBlock.H:116
Templated matrix space.
Definition: MatrixSpace.H:58
A templated (m x n) matrix of objects of <T>.
Definition: Matrix.H:83
void checkj(const label j) const
Check index j is within valid range (0 ... n-1).
Definition: MatrixI.H:98
const Type & operator()(const label i, const label j) const
(i, j) const element access operator
Definition: MatrixI.H:295
~Matrix()
Destructor.
Definition: Matrix.C:325
void checki(const label i) const
Check index i is within valid range (0 ... m-1).
Definition: MatrixI.H:78
Matrix()
Null constructor.
Definition: MatrixI.H:31
Type cmptType
Component type.
Definition: Matrix.H:102
label size() const
Return the number of elements in matrix (m*n)
Definition: MatrixI.H:71
ConstMatrixBlock< mType > col(const label m, const label rowStart) const
Definition: MatrixI.H:175
autoPtr< mType > clone() const
Clone.
Definition: MatrixI.H:41
void shallowResize(const label m, const label n)
Resize the matrix without reallocating storage (unsafe)
Definition: MatrixI.H:284
const Type * v() const
Return element vector of the constant Matrix.
Definition: MatrixI.H:118
label n() const
Return the number of columns.
Definition: MatrixI.H:64
void operator=(const mType &)
Assignment operator. Takes linear time.
Definition: Matrix.C:407
label m() const
Return the number of rows.
Definition: MatrixI.H:57
void transfer(mType &)
Transfer the contents of the argument Matrix into this Matrix.
Definition: Matrix.C:351
Form T() const
Return the transpose of the matrix.
Definition: Matrix.C:387
void setSize(const label m, const label n)
Resize the matrix preserving the elements.
Definition: Matrix.C:367
void clear()
Clear the Matrix, i.e. set sizes to zero.
Definition: Matrix.C:337
Type * operator[](const label)
Return subscript-checked row of Matrix.
Definition: MatrixI.H:328
ConstMatrixBlock< mType > block(const label m, const label n, const label mStart, const label nStart) const
Definition: MatrixI.H:134
Matrix< Form, Type > mType
Matrix type.
Definition: Matrix.H:99
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A class for managing temporary objects.
Definition: tmp.H:55
Abstract template class to provide the form resulting from.
Definition: products.H:48
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
volScalarField & b
Definition: createFields.H:27
Namespace for OpenFOAM.
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:59
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)