initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / matrices / Matrix / Matrix.H
blobfd623fdb206dc0746c78aaebadbd6e10ff9567a9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::Matrix
28 Description
29     A templated 2D matrix of objects of \<T\>, where the n x m matrix
30     dimensions are known and used for subscript bounds checking, etc.
32 SourceFiles
33     Matrix.C
34     MatrixI.H
35     MatrixIO.C
37 \*---------------------------------------------------------------------------*/
39 #ifndef Matrix_H
40 #define Matrix_H
42 #include "bool.H"
43 #include "label.H"
44 #include "uLabel.H"
45 #include "List.H"
46 #include "autoPtr.H"
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 namespace Foam
53 // Forward declaration of friend functions and operators
55 template<class Form, class Type> class Matrix;
57 template<class Form, class Type> Istream& operator>>
59     Istream&,
60     Matrix<Form, Type>&
63 template<class Form, class Type> Ostream& operator<<
65     Ostream&,
66     const Matrix<Form, Type>&
70 /*---------------------------------------------------------------------------*\
71                            Class Matrix Declaration
72 \*---------------------------------------------------------------------------*/
74 template<class Form, class Type>
75 class Matrix
77     // Private data
79         //- Number of rows and columns in Matrix.
80         label n_, m_;
82         //- Row pointers
83         Type** __restrict__ v_;
85         //- Allocate the storage for the row-pointers and the data
86         //  and set the row pointers
87         void allocate();
90 public:
92     // Static Member Functions
94         //- Return a null Matrix
95         inline static const Matrix<Form, Type>& null();
98     // Constructors
100         //- Null constructor.
101         inline Matrix();
103         //- Construct given number of rows and columns.
104         Matrix(const label n, const label m);
106         //- Construct with given number of rows and columns
107         //  and value for all elements.
108         Matrix(const label n, const label m, const Type&);
110         //- Copy constructor.
111         Matrix(const Matrix<Form, Type>&);
113         //- Construct from Istream.
114         Matrix(Istream&);
116         //- Clone
117         inline autoPtr<Matrix<Form, Type> > clone() const;
120     // Destructor
122         ~Matrix();
125     // Member Functions
127         // Access
129             //- Return the number of rows
130             inline label n() const;
132             //- Return the number of columns
133             inline label m() const;
135             //- Return the number of elements in matrix (n*m)
136             inline label size() const;
139         // Check
141             //- Check index i is within valid range (0 ... n-1).
142             inline void checki(const label i) const;
144             //- Check index j is within valid range (0 ... m-1).
145             inline void checkj(const label j) const;
148         // Edit
150             //- Clear the Matrix, i.e. set sizes to zero.
151             void clear();
153             //- Transfer the contents of the argument Matrix into this Matrix
154             //  and annull the argument Matrix.
155             void transfer(Matrix<Form, Type>&);
158         //- Return the transpose of the matrix
159         Form T() const;
162     // Member operators
164         //- Return subscript-checked element of Matrix.
165         inline Type* operator[](const label);
167         //- Return subscript-checked element of constant Matrix.
168         inline const Type* operator[](const label) const;
170         //- Assignment operator. Takes linear time.
171         void operator=(const Matrix<Form, Type>&);
173         //- Assignment of all entries to the given value
174         void operator=(const Type&);
177     // IOstream operators
179         //- Read Matrix from Istream, discarding contents of existing Matrix.
180         friend Istream& operator>> <Form, Type>(Istream&, Matrix<Form, Type>&);
182         // Write Matrix to Ostream.
183         friend Ostream& operator<< <Form, Type>(Ostream&, const Matrix<Form, Type>&);
187 // Global functions and operators
189 template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
190 template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
192 template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
194 template<class Form, class Type> Form operator+
196     const Matrix<Form, Type>&,
197     const Matrix<Form, Type>&
200 template<class Form, class Type> Form operator-
202     const Matrix<Form, Type>&,
203     const Matrix<Form, Type>&
206 template<class Form, class Type> Form operator*
208     const scalar,
209     const Matrix<Form, Type>&
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 } // End namespace Foam
217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 #   include "MatrixI.H"
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 #ifdef NoRepository
224 #   include "Matrix.C"
225 #endif
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 #endif
231 // ************************************************************************* //