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-2013 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 2D matrix of objects of <T>, where the n x m matrix
29  dimensions are known and used for subscript bounds checking, etc.
30 
31 SourceFiles
32  Matrix.C
33  MatrixI.H
34  MatrixIO.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Matrix_H
39 #define Matrix_H
40 
41 #include "bool.H"
42 #include "label.H"
43 #include "uLabel.H"
44 #include "List.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 
69 /*---------------------------------------------------------------------------*\
70  Class Matrix Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class Form, class Type>
74 class Matrix
75 {
76  // Private data
77 
78  //- Number of rows and columns in Matrix.
79  label n_, m_;
80 
81  //- Row pointers
82  Type** __restrict__ v_;
83 
84  //- Allocate the storage for the row-pointers and the data
85  // and set the row pointers
86  void allocate();
87 
88 
89 public:
90 
91  // Static Member Functions
92 
93  //- Return a null Matrix
94  inline static const Matrix<Form, Type>& null();
95 
96 
97  // Constructors
98 
99  //- Null constructor.
100  inline Matrix();
101 
102  //- Construct given number of rows and columns.
103  Matrix(const label n, const label m);
104 
105  //- Construct with given number of rows and columns
106  // and value for all elements.
107  Matrix(const label n, const label m, const Type&);
108 
109  //- Copy constructor.
110  Matrix(const Matrix<Form, Type>&);
111 
112  //- Construct from Istream.
113  Matrix(Istream&);
114 
115  //- Clone
116  inline autoPtr<Matrix<Form, Type> > clone() const;
117 
118 
119  //- Destructor
120  ~Matrix();
121 
122 
123  // Member Functions
124 
125  // Access
126 
127  //- Return the number of rows
128  inline label n() const;
129 
130  //- Return the number of columns
131  inline label m() const;
132 
133  //- Return the number of elements in matrix (n*m)
134  inline label size() const;
135 
136 
137  // Check
138 
139  //- Check index i is within valid range (0 ... n-1).
140  inline void checki(const label i) const;
141 
142  //- Check index j is within valid range (0 ... m-1).
143  inline void checkj(const label j) const;
144 
145 
146  // Edit
147 
148  //- Clear the Matrix, i.e. set sizes to zero.
149  void clear();
150 
151  //- Transfer the contents of the argument Matrix into this Matrix
152  // and annul the argument Matrix.
154 
155 
156  //- Return the transpose of the matrix
157  Form T() const;
158 
159 
160  // Member operators
161 
162  //- Return subscript-checked row of Matrix.
163  inline Type* operator[](const label);
164 
165  //- Return subscript-checked row of constant Matrix.
166  inline const Type* operator[](const label) const;
167 
168  //- Assignment operator. Takes linear time.
169  void operator=(const Matrix<Form, Type>&);
170 
171  //- Assignment of all entries to the given value
172  void operator=(const Type&);
173 
174 
175  // IOstream operators
176 
177  //- Read Matrix from Istream, discarding contents of existing Matrix.
178  friend Istream& operator>> <Form, Type>
179  (
180  Istream&,
182  );
183 
184  // Write Matrix to Ostream.
185  friend Ostream& operator<< <Form, Type>
186  (
187  Ostream&,
188  const Matrix<Form, Type>&
189  );
190 };
191 
192 
193 // Global functions and operators
194 
195 template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
196 template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
197 
198 template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
199 
200 template<class Form, class Type> Form operator+
201 (
202  const Matrix<Form, Type>&,
203  const Matrix<Form, Type>&
204 );
205 
206 template<class Form, class Type> Form operator-
207 (
208  const Matrix<Form, Type>&,
209  const Matrix<Form, Type>&
210 );
211 
212 template<class Form, class Type> Form operator*
213 (
214  const scalar,
215  const Matrix<Form, Type>&
216 );
217 
218 template<class Form, class Type> Form operator*
219 (
220  const Matrix<Form, Type>&,
221  const Matrix<Form, Type>&
222 );
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 } // End namespace Foam
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230 # include "MatrixI.H"
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 #ifdef NoRepository
235 # include "Matrix.C"
236 #endif
237 
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 
240 #endif
241 
242 // ************************************************************************* //
Foam::Matrix::m
label m() const
Return the number of columns.
Definition: MatrixI.H:63
List.H
Foam::Matrix::checkj
void checkj(const label j) const
Check index j is within valid range (0 ... m-1).
Definition: MatrixI.H:95
Foam::Matrix::clone
autoPtr< Matrix< Form, Type > > clone() const
Clone.
Definition: MatrixI.H:39
Foam::Matrix::~Matrix
~Matrix()
Destructor.
Definition: Matrix.C:49
Foam::Matrix::allocate
void allocate()
Allocate the storage for the row-pointers and the data.
Definition: Matrix.C:31
Foam::Matrix::n
label n() const
Return the number of rows.
Definition: MatrixI.H:56
Foam::Matrix
A templated 2D matrix of objects of <T>, where the n x m matrix dimensions are known and used for sub...
Definition: DiagonalMatrix.H:47
Foam::operator-
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
Foam::label
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
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Matrix::operator=
void operator=(const Matrix< Form, Type > &)
Assignment operator. Takes linear time.
bool.H
System bool.
Foam::Matrix::clear
void clear()
Clear the Matrix, i.e. set sizes to zero.
Definition: Matrix.C:132
Foam::Matrix::size
label size() const
Return the number of elements in matrix (n*m)
Definition: MatrixI.H:70
MatrixI.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Matrix::T
Form T() const
Return the transpose of the matrix.
Definition: Matrix.C:162
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::Matrix::operator[]
Type * operator[](const label)
Return subscript-checked row of Matrix.
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::Matrix::n_
label n_
Number of rows and columns in Matrix.
Definition: Matrix.H:78
label.H
Matrix.C
Foam::Matrix::checki
void checki(const label i) const
Check index i is within valid range (0 ... n-1).
Definition: MatrixI.H:77
Foam::Matrix::v_
Type **__restrict__ v_
Row pointers.
Definition: Matrix.H:81
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::Matrix::Matrix
Matrix()
Null constructor.
Definition: MatrixI.H:29
uLabel.H
Foam::Matrix::m_
label m_
Definition: Matrix.H:78
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
autoPtr.H
Foam::Matrix::transfer
void transfer(Matrix< Form, Type > &)
Transfer the contents of the argument Matrix into this Matrix.
Definition: Matrix.C:146