Matrix.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 | Copyright (C) 2011-2016 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&,
59  Matrix<Form, Type>&
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  // initializing 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  // initializing 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 const Type& max(const Matrix<Form, Type>&);
314 
315 template<class Form, class Type>
316 const Type& min(const Matrix<Form, Type>&);
317 
318 template<class Form, class Type>
319 Form operator-(const Matrix<Form, Type>&);
320 
321 template<class Form, class Type>
322 Form operator+
323 (
324  const Matrix<Form, Type>&,
325  const Matrix<Form, Type>&
326 );
327 
328 template<class Form, class Type>
329 Form operator-
330 (
331  const Matrix<Form, Type>&,
332  const Matrix<Form, Type>&
333 );
334 
335 template<class Form, class Type>
336 Form operator*
337 (
338  const scalar,
339  const Matrix<Form, Type>&
340 );
341 
342 template<class Form, class Type>
343 Form operator*
344 (
345  const Matrix<Form, Type>&,
346  const scalar
347 );
348 
349 template<class Form, class Type>
350 Form operator/
351 (
352  const Matrix<Form, Type>&,
353  const scalar
354 );
355 
356 template<class Form1, class Form2, class Type>
358 operator*
359 (
360  const Matrix<Form1, Type>& a,
361  const Matrix<Form2, Type>& b
362 );
363 
364 template<class Form, class Type>
365 tmp<Field<Type>> operator*
366 (
367  const Matrix<Form, Type>&,
368  const Field<Type>&
369 );
370 
371 
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373 
374 } // End namespace Foam
375 
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
377 
378 #include "MatrixI.H"
379 
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 #ifdef NoRepository
383  #include "Matrix.C"
384 #endif
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 #endif
389 
390 // ************************************************************************* //
label n() const
Return the number of columns.
Definition: MatrixI.H:64
Abstract template class to provide the form resulting from.
Definition: products.H:47
void shallowResize(const label m, const label n)
Resize the matrix without reallocating storage (unsafe)
Definition: MatrixI.H:284
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
void checki(const label i) const
Check index i is within valid range (0 ... m-1).
Definition: MatrixI.H:78
autoPtr< mType > clone() const
Clone.
Definition: MatrixI.H:41
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Form T() const
Return the transpose of the matrix.
Definition: Matrix.C:264
A templated block of an (m x n) matrix of type <MatrixType>.
Definition: Matrix.H:71
void setSize(const label m, const label n)
Resize the matrix preserving the elements.
Definition: Matrix.C:244
Matrix()
Null constructor.
Definition: MatrixI.H:31
void checkj(const label j) const
Check index j is within valid range (0 ... n-1).
Definition: MatrixI.H:98
static const mType & null()
Return a null Matrix.
Definition: MatrixI.H:50
label size() const
Return the number of elements in matrix (m*n)
Definition: MatrixI.H:71
Pre-declare SubField and related Field type.
Definition: Field.H:57
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
void clear()
Clear the Matrix, i.e. set sizes to zero.
Definition: Matrix.C:214
A templated (m x n) matrix of objects of <T>.
Type * operator[](const label)
Return subscript-checked row of Matrix.
Definition: MatrixI.H:328
Type cmptType
Component type.
Definition: Matrix.H:99
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
void operator=(const mType &)
Assignment operator. Takes linear time.
Definition: Matrix.C:284
Matrix< Form, Type > mType
Matrix type.
Definition: Matrix.H:96
~Matrix()
Destructor.
Definition: Matrix.C:202
ConstMatrixBlock< mType > block(const label m, const label n, const label mStart, const label nStart) const
Definition: MatrixI.H:134
label m() const
Return the number of rows.
Definition: MatrixI.H:57
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:49
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
A class for managing temporary objects.
Definition: PtrList.H:53
const Type * v() const
Return element vector of the constant Matrix.
Definition: MatrixI.H:118
const Type & operator()(const label i, const label j) const
(i, j) const element access operator
Definition: MatrixI.H:295
void transfer(mType &)
Transfer the contents of the argument Matrix into this Matrix.
Definition: Matrix.C:228
ConstMatrixBlock< mType > col(const label m, const label rowStart) const
Definition: MatrixI.H:175
System bool.
Namespace for OpenFOAM.