OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::Matrix
00027 
00028 Description
00029     A templated 2D matrix of objects of <T>, where the n x m matrix
00030     dimensions are known and used for subscript bounds checking, etc.
00031 
00032 SourceFiles
00033     Matrix.C
00034     MatrixI.H
00035     MatrixIO.C
00036 
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifndef Matrix_H
00040 #define Matrix_H
00041 
00042 #include "bool.H"
00043 #include "label.H"
00044 #include "uLabel.H"
00045 #include "List.H"
00046 #include "autoPtr.H"
00047 
00048 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00049 
00050 namespace Foam
00051 {
00052 
00053 // Forward declaration of friend functions and operators
00054 
00055 template<class Form, class Type> class Matrix;
00056 
00057 template<class Form, class Type> Istream& operator>>
00058 (
00059     Istream&,
00060     Matrix<Form, Type>&
00061 );
00062 
00063 template<class Form, class Type> Ostream& operator<<
00064 (
00065     Ostream&,
00066     const Matrix<Form, Type>&
00067 );
00068 
00069 
00070 /*---------------------------------------------------------------------------*\
00071                            Class Matrix Declaration
00072 \*---------------------------------------------------------------------------*/
00073 
00074 template<class Form, class Type>
00075 class Matrix
00076 {
00077     // Private data
00078 
00079         //- Number of rows and columns in Matrix.
00080         label n_, m_;
00081 
00082         //- Row pointers
00083         Type** __restrict__ v_;
00084 
00085         //- Allocate the storage for the row-pointers and the data
00086         //  and set the row pointers
00087         void allocate();
00088 
00089 
00090 public:
00091 
00092     // Static Member Functions
00093 
00094         //- Return a null Matrix
00095         inline static const Matrix<Form, Type>& null();
00096 
00097 
00098     // Constructors
00099 
00100         //- Null constructor.
00101         inline Matrix();
00102 
00103         //- Construct given number of rows and columns.
00104         Matrix(const label n, const label m);
00105 
00106         //- Construct with given number of rows and columns
00107         //  and value for all elements.
00108         Matrix(const label n, const label m, const Type&);
00109 
00110         //- Copy constructor.
00111         Matrix(const Matrix<Form, Type>&);
00112 
00113         //- Construct from Istream.
00114         Matrix(Istream&);
00115 
00116         //- Clone
00117         inline autoPtr<Matrix<Form, Type> > clone() const;
00118 
00119 
00120     // Destructor
00121 
00122         ~Matrix();
00123 
00124 
00125     // Member Functions
00126 
00127         // Access
00128 
00129             //- Return the number of rows
00130             inline label n() const;
00131 
00132             //- Return the number of columns
00133             inline label m() const;
00134 
00135             //- Return the number of elements in matrix (n*m)
00136             inline label size() const;
00137 
00138 
00139         // Check
00140 
00141             //- Check index i is within valid range (0 ... n-1).
00142             inline void checki(const label i) const;
00143 
00144             //- Check index j is within valid range (0 ... m-1).
00145             inline void checkj(const label j) const;
00146 
00147 
00148         // Edit
00149 
00150             //- Clear the Matrix, i.e. set sizes to zero.
00151             void clear();
00152 
00153             //- Transfer the contents of the argument Matrix into this Matrix
00154             //  and annull the argument Matrix.
00155             void transfer(Matrix<Form, Type>&);
00156 
00157 
00158         //- Return the transpose of the matrix
00159         Form T() const;
00160 
00161 
00162     // Member operators
00163 
00164         //- Return subscript-checked element of Matrix.
00165         inline Type* operator[](const label);
00166 
00167         //- Return subscript-checked element of constant Matrix.
00168         inline const Type* operator[](const label) const;
00169 
00170         //- Assignment operator. Takes linear time.
00171         void operator=(const Matrix<Form, Type>&);
00172 
00173         //- Assignment of all entries to the given value
00174         void operator=(const Type&);
00175 
00176 
00177     // IOstream operators
00178 
00179         //- Read Matrix from Istream, discarding contents of existing Matrix.
00180         friend Istream& operator>> <Form, Type>(Istream&, Matrix<Form, Type>&);
00181 
00182         // Write Matrix to Ostream.
00183         friend Ostream& operator<< <Form, Type>(Ostream&, const Matrix<Form, Type>&);
00184 };
00185 
00186 
00187 // Global functions and operators
00188 
00189 template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
00190 template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
00191 
00192 template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
00193 
00194 template<class Form, class Type> Form operator+
00195 (
00196     const Matrix<Form, Type>&,
00197     const Matrix<Form, Type>&
00198 );
00199 
00200 template<class Form, class Type> Form operator-
00201 (
00202     const Matrix<Form, Type>&,
00203     const Matrix<Form, Type>&
00204 );
00205 
00206 template<class Form, class Type> Form operator*
00207 (
00208     const scalar,
00209     const Matrix<Form, Type>&
00210 );
00211 
00212 
00213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00214 
00215 } // End namespace Foam
00216 
00217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00218 
00219 #   include "MatrixI.H"
00220 
00221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00222 
00223 #ifdef NoRepository
00224 #   include "Matrix.C"
00225 #endif
00226 
00227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00228 
00229 #endif
00230 
00231 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd