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

Matrix.C

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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Matrix.H"
00028 
00029 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00030 
00031 template<class Form, class Type>
00032 void Foam::Matrix<Form, Type>::allocate()
00033 {
00034     if (n_ && m_)
00035     {
00036         v_ = new Type*[n_];
00037         v_[0] = new Type[n_*m_];
00038 
00039         for (register label i=1; i<n_; i++)
00040         {
00041             v_[i] = v_[i-1] + m_;
00042         }
00043     }
00044 }
00045 
00046 
00047 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
00048 
00049 template<class Form, class Type>
00050 Foam::Matrix<Form, Type>::~Matrix()
00051 {
00052     if (v_)
00053     {
00054         delete[] (v_[0]);
00055         delete[] v_;
00056     }
00057 }
00058 
00059 
00060 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00061 
00062 template<class Form, class Type>
00063 Foam::Matrix<Form, Type>::Matrix(const label n, const label m)
00064 :
00065     n_(n),
00066     m_(m),
00067     v_(NULL)
00068 {
00069     if (n_ < 0 || m_ < 0)
00070     {
00071         FatalErrorIn("Matrix<Form, Type>::Matrix(const label n, const label m)")
00072             << "bad n, m " << n_ << ", " << m_
00073             << abort(FatalError);
00074     }
00075 
00076     allocate();
00077 }
00078 
00079 
00080 template<class Form, class Type>
00081 Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
00082 :
00083     n_(n),
00084     m_(m),
00085     v_(NULL)
00086 {
00087     if (n_ < 0 || m_ < 0)
00088     {
00089         FatalErrorIn
00090         (
00091             "Matrix<Form, Type>::Matrix(const label n, const label m, const T&)"
00092         )   << "bad n, m " << n_ << ", " << m_
00093             << abort(FatalError);
00094     }
00095 
00096     allocate();
00097 
00098     if (v_)
00099     {
00100         Type* v = v_[0];
00101 
00102         label nm = n_*m_;
00103 
00104         for (register label i=0; i<nm; i++)
00105         {
00106             v[i] = a;
00107         }
00108     }
00109 }
00110 
00111 
00112 template<class Form, class Type>
00113 Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
00114 :
00115     n_(a.n_),
00116     m_(a.m_),
00117     v_(NULL)
00118 {
00119     if (a.v_)
00120     {
00121         allocate();
00122         Type* v = v_[0];
00123         const Type* av = a.v_[0];
00124 
00125         label nm = n_*m_;
00126         for (register label i=0; i<nm; i++)
00127         {
00128             v[i] = av[i];
00129         }
00130     }
00131 }
00132 
00133 
00134 template<class Form, class Type>
00135 void Foam::Matrix<Form, Type>::clear()
00136 {
00137     if (v_)
00138     {
00139         delete[] (v_[0]);
00140         delete[] v_;
00141     }
00142     n_ = 0;
00143     m_ = 0;
00144     v_ = NULL;
00145 }
00146 
00147 
00148 template<class Form, class Type>
00149 void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& a)
00150 {
00151     clear();
00152 
00153     n_ = a.n_;
00154     a.n_ = 0;
00155 
00156     m_ = a.m_;
00157     a.m_ = 0;
00158 
00159     v_ = a.v_;
00160     a.v_ = NULL;
00161 }
00162 
00163 
00164 template<class Form, class Type>
00165 Form Foam::Matrix<Form, Type>::T() const
00166 {
00167     const Matrix<Form, Type>& A = *this;
00168     Form At(m(), n());
00169 
00170     for (register label i=0; i<n(); i++)
00171     {
00172         for (register label j=0; j<m(); j++)
00173         {
00174             At[j][i] = A[i][j];
00175         }
00176     }
00177 
00178     return At;
00179 }
00180 
00181 
00182 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00183 
00184 template<class Form, class Type>
00185 void Foam::Matrix<Form, Type>::operator=(const Type& t)
00186 {
00187     if (v_)
00188     {
00189         Type* v = v_[0];
00190 
00191         label nm = n_*m_;
00192         for (register label i=0; i<nm; i++)
00193         {
00194             v[i] = t;
00195         }
00196     }
00197 }
00198 
00199 
00200 // Assignment operator. Takes linear time.
00201 template<class Form, class Type>
00202 void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
00203 {
00204     if (this == &a)
00205     {
00206         FatalErrorIn("Matrix<Form, Type>::operator=(const Matrix<Form, Type>&)")
00207             << "attempted assignment to self"
00208             << abort(FatalError);
00209     }
00210 
00211     if (n_ != a.n_ || m_ != a.m_)
00212     {
00213         clear();
00214         n_ = a.n_;
00215         m_ = a.m_;
00216         allocate();
00217     }
00218 
00219     if (v_)
00220     {
00221         Type* v = v_[0];
00222         const Type* av = a.v_[0];
00223 
00224         label nm = n_*m_;
00225         for (register label i=0; i<nm; i++)
00226         {
00227             v[i] = av[i];
00228         }
00229     }
00230 }
00231 
00232 
00233 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00234 
00235 template<class Form, class Type>
00236 const Type& Foam::max(const Matrix<Form, Type>& a)
00237 {
00238     label nm = a.n_*a.m_;
00239 
00240     if (nm)
00241     {
00242         label curMaxI = 0;
00243         const Type* v = a.v_[0];
00244 
00245         for (register label i=1; i<nm; i++)
00246         {
00247             if (v[i] > v[curMaxI])
00248             {
00249                 curMaxI = i;
00250             }
00251         }
00252 
00253         return v[curMaxI];
00254     }
00255     else
00256     {
00257         FatalErrorIn("max(const Matrix<Form, Type>&)")
00258             << "matrix is empty"
00259             << abort(FatalError);
00260 
00261         // Return in error to keep compiler happy
00262         return a[0][0];
00263     }
00264 }
00265 
00266 
00267 template<class Form, class Type>
00268 const Type& Foam::min(const Matrix<Form, Type>& a)
00269 {
00270     label nm = a.n_*a.m_;
00271 
00272     if (nm)
00273     {
00274         label curMinI = 0;
00275         const Type* v = a.v_[0];
00276 
00277         for (register label i=1; i<nm; i++)
00278         {
00279             if (v[i] < v[curMinI])
00280             {
00281                 curMinI = i;
00282             }
00283         }
00284 
00285         return v[curMinI];
00286     }
00287     else
00288     {
00289         FatalErrorIn("min(const Matrix<Form, Type>&)")
00290             << "matrix is empty"
00291             << abort(FatalError);
00292 
00293         // Return in error to keep compiler happy
00294         return a[0][0];
00295     }
00296 }
00297 
00298 
00299 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00300 
00301 template<class Form, class Type>
00302 Form Foam::operator-(const Matrix<Form, Type>& a)
00303 {
00304     Form na(a.n_, a.m_);
00305 
00306     if (a.n_ && a.m_)
00307     {
00308         Type* nav = na.v_[0];
00309         const Type* av = a.v_[0];
00310 
00311         label nm = a.n_*a.m_;
00312         for (register label i=0; i<nm; i++)
00313         {
00314             nav[i] = -av[i];
00315         }
00316     }
00317 
00318     return na;
00319 }
00320 
00321 
00322 template<class Form, class Type>
00323 Form Foam::operator+(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
00324 {
00325     if (a.n_ != b.n_)
00326     {
00327         FatalErrorIn
00328         (
00329             "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
00330         )   << "attempted add matrices with different number of rows: "
00331             << a.n_ << ", " << b.n_
00332             << abort(FatalError);
00333     }
00334 
00335     if (a.m_ != b.m_)
00336     {
00337         FatalErrorIn
00338         (
00339             "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
00340         )   << "attempted add matrices with different number of columns: "
00341             << a.m_ << ", " << b.m_
00342             << abort(FatalError);
00343     }
00344 
00345     Form ab(a.n_, a.m_);
00346 
00347     Type* abv = ab.v_[0];
00348     const Type* av = a.v_[0];
00349     const Type* bv = b.v_[0];
00350 
00351     label nm = a.n_*a.m_;
00352     for (register label i=0; i<nm; i++)
00353     {
00354         abv[i] = av[i] + bv[i];
00355     }
00356 
00357     return ab;
00358 }
00359 
00360 
00361 template<class Form, class Type>
00362 Form Foam::operator-(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
00363 {
00364     if (a.n_ != b.n_)
00365     {
00366         FatalErrorIn
00367         (
00368             "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
00369         )   << "attempted add matrices with different number of rows: "
00370             << a.n_ << ", " << b.n_
00371             << abort(FatalError);
00372     }
00373 
00374     if (a.m_ != b.m_)
00375     {
00376         FatalErrorIn
00377         (
00378             "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
00379         )   << "attempted add matrices with different number of columns: "
00380             << a.m_ << ", " << b.m_
00381             << abort(FatalError);
00382     }
00383 
00384     Form ab(a.n_, a.m_);
00385 
00386     Type* abv = ab.v_[0];
00387     const Type* av = a.v_[0];
00388     const Type* bv = b.v_[0];
00389 
00390     label nm = a.n_*a.m_;
00391     for (register label i=0; i<nm; i++)
00392     {
00393         abv[i] = av[i] - bv[i];
00394     }
00395 
00396     return ab;
00397 }
00398 
00399 
00400 template<class Form, class Type>
00401 Form Foam::operator*(const scalar s, const Matrix<Form, Type>& a)
00402 {
00403     Form sa(a.n_, a.m_);
00404 
00405     if (a.n_ && a.m_)
00406     {
00407         Type* sav = sa.v_[0];
00408         const Type* av = a.v_[0];
00409 
00410         label nm = a.n_*a.m_;
00411         for (register label i=0; i<nm; i++)
00412         {
00413             sav[i] = s*av[i];
00414         }
00415     }
00416 
00417     return sa;
00418 }
00419 
00420 
00421 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
00422 
00423 #include "MatrixIO.C"
00424 
00425 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd