All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2021 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 
75 /*---------------------------------------------------------------------------*\
76  Class Matrix Declaration
77 \*---------------------------------------------------------------------------*/
78 
79 template<class Form, class Type>
80 class Matrix
81 {
82  // Private Data
83 
84  //- Number of rows and columns in Matrix.
85  label mRows_, nCols_;
86 
87  //- Row pointers
88  Type* __restrict__ v_;
89 
90  //- Allocate the storage for the element vector
91  void allocate();
92 
93 
94 public:
95 
96  //- Matrix type
97  typedef Matrix<Form, Type> mType;
98 
99  //- Component type
100  typedef Type cmptType;
101 
102 
103  // Static Member Functions
104 
105  //- Return a null Matrix
106  inline static const mType& null();
107 
108 
109  // Constructors
110 
111  //- Null constructor.
112  inline Matrix();
113 
114  //- Construct given number of rows and columns.
115  Matrix(const label m, const label n);
116 
117  //- Construct with given number of rows and columns
118  // initialising all elements to zero
119  Matrix(const label m, const label n, const zero);
120 
121  //- Construct with given number of rows and columns
122  // initialising all elements to the given value
123  Matrix(const label m, const label n, const Type&);
124 
125  //- Copy constructor.
126  Matrix(const mType&);
127 
128  //- Copy constructor from matrix of a different form
129  template<class Form2>
130  explicit Matrix(const Matrix<Form2, Type>&);
131 
132  //- Construct from a block of another matrix
133  template<class MatrixType>
135 
136  //- Construct from a block of another matrix
137  template<class MatrixType>
139 
140  //- Construct from Istream.
141  Matrix(Istream&);
142 
143  //- Clone
144  inline autoPtr<mType> clone() const;
145 
146 
147  //- Destructor
148  ~Matrix();
149 
150 
151  // Member Functions
152 
153  // Access
154 
155  //- Return the number of rows
156  inline label m() const;
157 
158  //- Return the number of columns
159  inline label n() const;
160 
161  //- Return the number of elements in matrix (m*n)
162  inline label size() const;
163 
164  //- Return element vector of the constant Matrix
165  inline const Type* v() const;
166 
167  //- Return element vector of the Matrix
168  inline Type* v();
169 
170 
171  // Block access
172 
174  (
175  const label m,
176  const label n,
177  const label mStart,
178  const label nStart
179  ) const;
180 
181  template<class VectorSpace>
183  (
184  const label mStart,
185  const label nStart
186  ) const;
187 
189  (
190  const label m,
191  const label rowStart
192  ) const;
193 
195  (
196  const label m,
197  const label mStart,
198  const label nStart
199  ) const;
200 
201 
203  (
204  const label m,
205  const label n,
206  const label mStart,
207  const label nStart
208  );
209 
210  template<class VectorSpace>
212  (
213  const label mStart,
214  const label nStart
215  );
216 
217  inline MatrixBlock<mType> col
218  (
219  const label m,
220  const label rowStart
221  );
222 
223  inline MatrixBlock<mType> col
224  (
225  const label m,
226  const label mStart,
227  const label nStart
228  );
229 
230 
231  // Check
232 
233  //- Check index i is within valid range (0 ... m-1).
234  inline void checki(const label i) const;
235 
236  //- Check index j is within valid range (0 ... n-1).
237  inline void checkj(const label j) const;
238 
239 
240  // Edit
241 
242  //- Clear the Matrix, i.e. set sizes to zero.
243  void clear();
244 
245  //- Transfer the contents of the argument Matrix into this Matrix
246  // and annul the argument Matrix.
247  void transfer(mType&);
248 
249  //- Resize the matrix preserving the elements
250  void setSize(const label m, const label n);
251 
252  //- Resize the matrix without reallocating storage (unsafe)
253  inline void shallowResize(const label m, const label n);
254 
255 
256  //- Return the transpose of the matrix
257  Form T() const;
258 
259 
260  // Member Operators
261 
262  //- Return subscript-checked row of Matrix.
263  inline Type* operator[](const label);
264 
265  //- Return subscript-checked row of constant Matrix.
266  inline const Type* operator[](const label) const;
267 
268  //- (i, j) const element access operator
269  inline const Type& operator()(const label i, const label j) const;
270 
271  //- (i, j) element access operator
272  inline Type& operator()(const label i, const label j);
273 
274  //- Assignment operator. Takes linear time.
275  void operator=(const mType&);
276 
277  //- Assignment to a block of another matrix
278  template<class MatrixType>
280 
281  //- Assignment to a block of another matrix
282  template<class MatrixType>
283  void operator=(const MatrixBlock<MatrixType>&);
284 
285  //- Assignment of all elements to zero
286  void operator=(const zero);
287 
288  //- Assignment of all elements to the given value
289  void operator=(const Type&);
290 
291 
292  // IOstream Operators
293 
294  //- Read Matrix from Istream, discarding contents of existing Matrix.
295  friend Istream& operator>> <Form, Type>
296  (
297  Istream&,
298  mType&
299  );
300 
301  //- Write Matrix to Ostream.
302  friend Ostream& operator<< <Form, Type>
303  (
304  Ostream&,
305  const mType&
306  );
307 };
308 
309 
310 // Global functions and operators
311 
312 template<class Form, class Type>
313 void writeEntry(Ostream&, const Matrix<Form, Type>&);
314 
315 template<class Form, class Type>
316 const Type& max(const Matrix<Form, Type>&);
317 
318 template<class Form, class Type>
319 const Type& min(const Matrix<Form, Type>&);
320 
321 template<class Form, class Type>
322 Form operator-(const Matrix<Form, Type>&);
323 
324 template<class Form, class Type>
325 Form operator+
326 (
327  const Matrix<Form, Type>&,
328  const Matrix<Form, Type>&
329 );
330 
331 template<class Form, class Type>
332 Form operator-
333 (
334  const Matrix<Form, Type>&,
335  const Matrix<Form, Type>&
336 );
337 
338 template<class Form, class Type>
339 Form operator*
340 (
341  const scalar,
342  const Matrix<Form, Type>&
343 );
344 
345 template<class Form, class Type>
346 Form operator*
347 (
348  const Matrix<Form, Type>&,
349  const scalar
350 );
351 
352 template<class Form, class Type>
353 Form operator/
354 (
355  const Matrix<Form, Type>&,
356  const scalar
357 );
358 
359 template<class Form1, class Form2, class Type>
361 operator*
362 (
363  const Matrix<Form1, Type>& a,
364  const Matrix<Form2, Type>& b
365 );
366 
367 template<class Form, class Type>
368 tmp<Field<Type>> operator*
369 (
370  const Matrix<Form, Type>&,
371  const Field<Type>&
372 );
373 
374 
375 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
376 
377 } // End namespace Foam
378 
379 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 
381 #include "MatrixI.H"
382 
383 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
384 
385 #ifdef NoRepository
386  #include "Matrix.C"
387 #endif
388 
389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
390 
391 #endif
392 
393 // ************************************************************************* //
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
A templated (m x n) matrix of objects of <T>.
Definition: Matrix.H:80
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:202
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:99
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:284
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:228
Form T() const
Return the transpose of the matrix.
Definition: Matrix.C:264
void setSize(const label m, const label n)
Resize the matrix preserving the elements.
Definition: Matrix.C:244
void clear()
Clear the Matrix, i.e. set sizes to zero.
Definition: Matrix.C:214
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:96
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:25
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
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 > &)