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 
253  //- Return the transpose of the matrix
254  Form T() const;
255 
256 
257  // Member operators
258 
259  //- Return subscript-checked row of Matrix.
260  inline Type* operator[](const label);
261 
262  //- Return subscript-checked row of constant Matrix.
263  inline const Type* operator[](const label) const;
264 
265  //- (i, j) const element access operator
266  inline const Type& operator()(const label i, const label j) const;
267 
268  //- (i, j) element access operator
269  inline Type& operator()(const label i, const label j);
270 
271  //- Assignment operator. Takes linear time.
272  void operator=(const mType&);
273 
274  //- Assignment to a block of another matrix
275  template<class MatrixType>
277 
278  //- Assignment to a block of another matrix
279  template<class MatrixType>
280  void operator=(const MatrixBlock<MatrixType>&);
281 
282  //- Assignment of all elements to zero
283  void operator=(const zero);
284 
285  //- Assignment of all elements to the given value
286  void operator=(const Type&);
287 
288 
289  // IOstream operators
290 
291  //- Read Matrix from Istream, discarding contents of existing Matrix.
292  friend Istream& operator>> <Form, Type>
293  (
294  Istream&,
295  mType&
296  );
297 
298  // Write Matrix to Ostream.
299  friend Ostream& operator<< <Form, Type>
300  (
301  Ostream&,
302  const mType&
303  );
304 };
305 
306 
307 // Global functions and operators
308 
309 template<class Form, class Type>
310 const Type& max(const Matrix<Form, Type>&);
311 
312 template<class Form, class Type>
313 const Type& min(const Matrix<Form, Type>&);
314 
315 template<class Form, class Type>
316 Form operator-(const Matrix<Form, Type>&);
317 
318 template<class Form, class Type>
319 Form operator+
320 (
321  const Matrix<Form, Type>&,
322  const Matrix<Form, Type>&
323 );
324 
325 template<class Form, class Type>
326 Form operator-
327 (
328  const Matrix<Form, Type>&,
329  const Matrix<Form, Type>&
330 );
331 
332 template<class Form, class Type>
333 Form operator*
334 (
335  const scalar,
336  const Matrix<Form, Type>&
337 );
338 
339 template<class Form, class Type>
340 Form operator*
341 (
342  const Matrix<Form, Type>&,
343  const scalar
344 );
345 
346 template<class Form, class Type>
347 Form operator/
348 (
349  const Matrix<Form, Type>&,
350  const scalar
351 );
352 
353 template<class Form1, class Form2, class Type>
355 operator*
356 (
357  const Matrix<Form1, Type>& a,
358  const Matrix<Form2, Type>& b
359 );
360 
361 template<class Form, class Type>
362 tmp<Field<Type>> operator*
363 (
364  const Matrix<Form, Type>&,
365  const Field<Type>&
366 );
367 
368 
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 } // End namespace Foam
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 #include "MatrixI.H"
376 
377 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
378 
379 #ifdef NoRepository
380  #include "Matrix.C"
381 #endif
382 
383 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
384 
385 #endif
386 
387 // ************************************************************************* //
void checkj(const label j) const
Check index j is within valid range (0 ... n-1).
Definition: MatrixI.H:98
Abstract template class to provide the form resulting from.
Definition: products.H:47
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
Form T() const
Return the transpose of the matrix.
Definition: Matrix.C:264
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
A templated block of an (m x n) matrix of type <MatrixType>.
Definition: Matrix.H:71
label m() const
Return the number of rows.
Definition: MatrixI.H:57
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
label n() const
Return the number of columns.
Definition: MatrixI.H:64
ConstMatrixBlock< mType > col(const label m, const label rowStart) const
Definition: MatrixI.H:175
static const mType & null()
Return a null Matrix.
Definition: MatrixI.H:50
ConstMatrixBlock< mType > block(const label m, const label n, const label mStart, const label nStart) const
Definition: MatrixI.H:134
autoPtr< mType > clone() const
Clone.
Definition: MatrixI.H:41
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
const Type & operator()(const label i, const label j) const
(i, j) const element access operator
Definition: MatrixI.H:287
const Type * v() const
Return element vector of the constant Matrix.
Definition: MatrixI.H:118
A templated (m x n) matrix of objects of <T>.
Type * operator[](const label)
Return subscript-checked row of Matrix.
Definition: MatrixI.H:320
Type cmptType
Component type.
Definition: Matrix.H:99
label size() const
Return the number of elements in matrix (m*n)
Definition: MatrixI.H:71
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
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:53
A class for managing temporary objects.
Definition: PtrList.H:54
void checki(const label i) const
Check index i is within valid range (0 ... m-1).
Definition: MatrixI.H:78
void transfer(mType &)
Transfer the contents of the argument Matrix into this Matrix.
Definition: Matrix.C:228
System bool.
Namespace for OpenFOAM.