FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/bool.H>
42 #include <OpenFOAM/label.H>
43 #include <OpenFOAM/uLabel.H>
44 #include <OpenFOAM/List.H>
45 #include <OpenFOAM/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 
121  ~Matrix();
122 
123 
124  // Member Functions
125 
126  // Access
127 
128  //- Return the number of rows
129  inline label n() const;
130 
131  //- Return the number of columns
132  inline label m() const;
133 
134  //- Return the number of elements in matrix (n*m)
135  inline label size() const;
136 
137 
138  // Check
139 
140  //- Check index i is within valid range (0 ... n-1).
141  inline void checki(const label i) const;
142 
143  //- Check index j is within valid range (0 ... m-1).
144  inline void checkj(const label j) const;
145 
146 
147  // Edit
148 
149  //- Clear the Matrix, i.e. set sizes to zero.
150  void clear();
151 
152  //- Transfer the contents of the argument Matrix into this Matrix
153  // and annull the argument Matrix.
155 
156 
157  //- Return the transpose of the matrix
158  Form T() const;
159 
160 
161  // Member operators
162 
163  //- Return subscript-checked row of Matrix.
164  inline Type* operator[](const label);
165 
166  //- Return subscript-checked row of constant Matrix.
167  inline const Type* operator[](const label) const;
168 
169  //- Assignment operator. Takes linear time.
170  void operator=(const Matrix<Form, Type>&);
171 
172  //- Assignment of all entries to the given value
173  void operator=(const Type&);
174 
175 
176  // IOstream operators
177 
178  //- Read Matrix from Istream, discarding contents of existing Matrix.
179  friend Istream& operator>> <Form, Type>(Istream&, Matrix<Form, Type>&);
180 
181  // Write Matrix to Ostream.
182  friend Ostream& operator<< <Form, Type>(Ostream&, const Matrix<Form, Type>&);
183 };
184 
185 
186 // Global functions and operators
187 
188 template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
189 template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
190 
191 template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
192 
193 template<class Form, class Type> Form operator+
194 (
195  const Matrix<Form, Type>&,
196  const Matrix<Form, Type>&
197 );
198 
199 template<class Form, class Type> Form operator-
200 (
201  const Matrix<Form, Type>&,
202  const Matrix<Form, Type>&
203 );
204 
205 template<class Form, class Type> Form operator*
206 (
207  const scalar,
208  const Matrix<Form, Type>&
209 );
210 
211 
212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
213 
214 } // End namespace Foam
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 # include <OpenFOAM/MatrixI.H>
219 
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 
222 #ifdef NoRepository
223 # include <OpenFOAM/Matrix.C>
224 #endif
225 
226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 
228 #endif
229 
230 // ************************ vim: set sw=4 sts=4 et: ************************ //