FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

Matrix.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::Matrix
00026 
00027 Description
00028     A templated 2D matrix of objects of <T>, where the n x m matrix
00029     dimensions are known and used for subscript bounds checking, etc.
00030 
00031 SourceFiles
00032     Matrix.C
00033     MatrixI.H
00034     MatrixIO.C
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #ifndef Matrix_H
00039 #define Matrix_H
00040 
00041 #include <OpenFOAM/bool.H>
00042 #include <OpenFOAM/label.H>
00043 #include <OpenFOAM/uLabel.H>
00044 #include <OpenFOAM/List.H>
00045 #include <OpenFOAM/autoPtr.H>
00046 
00047 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00048 
00049 namespace Foam
00050 {
00051 
00052 // Forward declaration of friend functions and operators
00053 
00054 template<class Form, class Type> class Matrix;
00055 
00056 template<class Form, class Type> Istream& operator>>
00057 (
00058     Istream&,
00059     Matrix<Form, Type>&
00060 );
00061 
00062 template<class Form, class Type> Ostream& operator<<
00063 (
00064     Ostream&,
00065     const Matrix<Form, Type>&
00066 );
00067 
00068 
00069 /*---------------------------------------------------------------------------*\
00070                            Class Matrix Declaration
00071 \*---------------------------------------------------------------------------*/
00072 
00073 template<class Form, class Type>
00074 class Matrix
00075 {
00076     // Private data
00077 
00078         //- Number of rows and columns in Matrix.
00079         label n_, m_;
00080 
00081         //- Row pointers
00082         Type** __restrict__ v_;
00083 
00084         //- Allocate the storage for the row-pointers and the data
00085         //  and set the row pointers
00086         void allocate();
00087 
00088 
00089 public:
00090 
00091     // Static Member Functions
00092 
00093         //- Return a null Matrix
00094         inline static const Matrix<Form, Type>& null();
00095 
00096 
00097     // Constructors
00098 
00099         //- Null constructor.
00100         inline Matrix();
00101 
00102         //- Construct given number of rows and columns.
00103         Matrix(const label n, const label m);
00104 
00105         //- Construct with given number of rows and columns
00106         //  and value for all elements.
00107         Matrix(const label n, const label m, const Type&);
00108 
00109         //- Copy constructor.
00110         Matrix(const Matrix<Form, Type>&);
00111 
00112         //- Construct from Istream.
00113         Matrix(Istream&);
00114 
00115         //- Clone
00116         inline autoPtr<Matrix<Form, Type> > clone() const;
00117 
00118 
00119     // Destructor
00120 
00121         ~Matrix();
00122 
00123 
00124     // Member Functions
00125 
00126         // Access
00127 
00128             //- Return the number of rows
00129             inline label n() const;
00130 
00131             //- Return the number of columns
00132             inline label m() const;
00133 
00134             //- Return the number of elements in matrix (n*m)
00135             inline label size() const;
00136 
00137 
00138         // Check
00139 
00140             //- Check index i is within valid range (0 ... n-1).
00141             inline void checki(const label i) const;
00142 
00143             //- Check index j is within valid range (0 ... m-1).
00144             inline void checkj(const label j) const;
00145 
00146 
00147         // Edit
00148 
00149             //- Clear the Matrix, i.e. set sizes to zero.
00150             void clear();
00151 
00152             //- Transfer the contents of the argument Matrix into this Matrix
00153             //  and annull the argument Matrix.
00154             void transfer(Matrix<Form, Type>&);
00155 
00156 
00157         //- Return the transpose of the matrix
00158         Form T() const;
00159 
00160 
00161     // Member operators
00162 
00163         //- Return subscript-checked row of Matrix.
00164         inline Type* operator[](const label);
00165 
00166         //- Return subscript-checked row of constant Matrix.
00167         inline const Type* operator[](const label) const;
00168 
00169         //- Assignment operator. Takes linear time.
00170         void operator=(const Matrix<Form, Type>&);
00171 
00172         //- Assignment of all entries to the given value
00173         void operator=(const Type&);
00174 
00175 
00176     // IOstream operators
00177 
00178         //- Read Matrix from Istream, discarding contents of existing Matrix.
00179         friend Istream& operator>> <Form, Type>(Istream&, Matrix<Form, Type>&);
00180 
00181         // Write Matrix to Ostream.
00182         friend Ostream& operator<< <Form, Type>(Ostream&, const Matrix<Form, Type>&);
00183 };
00184 
00185 
00186 // Global functions and operators
00187 
00188 template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
00189 template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
00190 
00191 template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
00192 
00193 template<class Form, class Type> Form operator+
00194 (
00195     const Matrix<Form, Type>&,
00196     const Matrix<Form, Type>&
00197 );
00198 
00199 template<class Form, class Type> Form operator-
00200 (
00201     const Matrix<Form, Type>&,
00202     const Matrix<Form, Type>&
00203 );
00204 
00205 template<class Form, class Type> Form operator*
00206 (
00207     const scalar,
00208     const Matrix<Form, Type>&
00209 );
00210 
00211 
00212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00213 
00214 } // End namespace Foam
00215 
00216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00217 
00218 #   include <OpenFOAM/MatrixI.H>
00219 
00220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00221 
00222 #ifdef NoRepository
00223 #   include <OpenFOAM/Matrix.C>
00224 #endif
00225 
00226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00227 
00228 #endif
00229 
00230 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines