Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / matrices / Matrix / Matrix.H
blob0fef235b8bee0b12c923ea166e02b1c1704edbf5
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Class
25     Foam::Matrix
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.
31 SourceFiles
32     Matrix.C
33     MatrixI.H
34     MatrixIO.C
36 \*---------------------------------------------------------------------------*/
38 #ifndef Matrix_H
39 #define Matrix_H
41 #include "List.H"
42 #include "label.H"
43 #include "bool.H"
44 #include "autoPtr.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 namespace Foam
51 // Forward declaration of friend functions and operators
53 template<class Form, class Type> class Matrix;
55 template<class Form, class Type> Istream& operator>>
57     Istream&,
58     Matrix<Form, Type>&
61 template<class Form, class Type> Ostream& operator<<
63     Ostream&,
64     const Matrix<Form, Type>&
68 /*---------------------------------------------------------------------------*\
69                            Class Matrix Declaration
70 \*---------------------------------------------------------------------------*/
72 template<class Form, class Type>
73 class Matrix
75     // Private data
77         //- Row pointers
78         Type** __restrict__ v_;
80         //- Number of rows and columns in Matrix.
81         label n_, m_;
83         //- Allocate the storage for the row-pointers and the data
84         //  and set the row pointers
85         void allocate();
88 public:
90     // Static data members
92         //- Empty matrix
93         static const Matrix<Form, Type> zero;
96     // Static Member Functions
98         //- Return a null Matrix
99         inline static const Matrix<Form, Type>& null();
102     // Constructors
104         //- Null constructor.
105         inline Matrix();
107         //- Construct given number of rows and columns.
108         Matrix(const label n, const label m);
110         //- Construct with given number of rows and columns
111         //  and value for all elements.
112         Matrix(const label n, const label m, const Type&);
114         //- Copy constructor.
115     Matrix(const Matrix<Form, Type>&);
117         //- Construct from Istream.
118         Matrix(Istream&);
120         //- Clone
121         inline autoPtr<Matrix<Form, Type> > clone() const;
124     // Destructor
126         ~Matrix();
129     // Member functions
131         // Access
133             //- Return the number of rows
134             inline label n() const;
136             //- Return the number of columns
137             inline label m() const;
139             //- Return the number of elements in matrix (n*m)
140             inline label size() const;
143         // Check
145             //- Check index i is within valid range (0 ... n-1).
146             inline void checki(const label i) const;
148             //- Check index j is within valid range (0 ... m-1).
149             inline void checkj(const label j) const;
152         // Edit
154             //- Clear the Matrix, i.e. set sizes to zero.
155             void clear();
157             //- Transfer the contents of the argument Matrix into this Matrix
158             //  and annull the argument Matrix.
159             void transfer(Matrix<Form, Type>&);
162         //- Return the transpose of the matrix
163         Form T() const;
166     // Member operators
168         //- Return subscript-checked element of Matrix.
169         inline Type* operator[](const label);
171         //- Return subscript-checked element of constant Matrix.
172         inline const Type* operator[](const label) const;
174         //- Assignment operator. Takes linear time.
175         void operator=(const Matrix<Form, Type>&);
177         //- Assignment of all entries to the given value
178         void operator=(const Type&);
181     // IOstream operators
183         //- Read Matrix from Istream, discarding contents of existing Matrix.
184         friend Istream& operator>> <Form, Type>
185         (
186             Istream&,
187             Matrix<Form, Type>&
188         );
190         // Write Matrix to Ostream.
191         friend Ostream& operator<< <Form, Type>
192         (
193             Ostream&,
194             const Matrix<Form, Type>&
195         );
199 // Global functions and operators
201 template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
202 template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
204 template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
206 template<class Form, class Type> Form operator+
208     const Matrix<Form, Type>&,
209     const Matrix<Form, Type>&
212 template<class Form, class Type> Form operator-
214     const Matrix<Form, Type>&,
215     const Matrix<Form, Type>&
218 template<class Form, class Type> Form operator*
220     const scalar,
221     const Matrix<Form, Type>&
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 } // End namespace Foam
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 #   include "MatrixI.H"
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 #ifdef NoRepository
236 #   include "Matrix.C"
237 #endif
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 #endif
243 // ************************************************************************* //