1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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 \*---------------------------------------------------------------------------*/
29 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
31 template<class Form, class Type>
32 void Foam::Matrix<Form, Type>::allocate()
37 v_[0] = new Type[n_*m_];
39 for (register label i=1; i<n_; i++)
47 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
49 template<class Form, class Type>
50 Foam::Matrix<Form, Type>::~Matrix()
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62 template<class Form, class Type>
63 Foam::Matrix<Form, Type>::Matrix(const label n, const label m)
71 FatalErrorIn("Matrix<Form, Type>::Matrix(const label n, const label m)")
72 << "bad n, m " << n_ << ", " << m_
80 template<class Form, class Type>
81 Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
91 "Matrix<Form, Type>::Matrix(const label n, const label m, const T&)"
92 ) << "bad n, m " << n_ << ", " << m_
104 for (register label i=0; i<nm; i++)
112 template<class Form, class Type>
113 Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
123 const Type* av = a.v_[0];
126 for (register label i=0; i<nm; i++)
134 template<class Form, class Type>
135 void Foam::Matrix<Form, Type>::clear()
148 template<class Form, class Type>
149 void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& a)
164 template<class Form, class Type>
165 Form Foam::Matrix<Form, Type>::T() const
167 const Matrix<Form, Type>& A = *this;
170 for (register label i=0; i<n(); i++)
172 for (register label j=0; j<m(); j++)
182 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
184 template<class Form, class Type>
185 void Foam::Matrix<Form, Type>::operator=(const Type& t)
192 for (register label i=0; i<nm; i++)
200 // Assignment operator. Takes linear time.
201 template<class Form, class Type>
202 void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
206 FatalErrorIn("Matrix<Form, Type>::operator=(const Matrix<Form, Type>&)")
207 << "attempted assignment to self"
208 << abort(FatalError);
211 if (n_ != a.n_ || m_ != a.m_)
222 const Type* av = a.v_[0];
225 for (register label i=0; i<nm; i++)
233 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
235 template<class Form, class Type>
236 const Type& Foam::max(const Matrix<Form, Type>& a)
238 label nm = a.n_*a.m_;
243 const Type* v = a.v_[0];
245 for (register label i=1; i<nm; i++)
247 if (v[i] > v[curMaxI])
257 FatalErrorIn("max(const Matrix<Form, Type>&)")
259 << abort(FatalError);
261 // Return in error to keep compiler happy
267 template<class Form, class Type>
268 const Type& Foam::min(const Matrix<Form, Type>& a)
270 label nm = a.n_*a.m_;
275 const Type* v = a.v_[0];
277 for (register label i=1; i<nm; i++)
279 if (v[i] < v[curMinI])
289 FatalErrorIn("min(const Matrix<Form, Type>&)")
291 << abort(FatalError);
293 // Return in error to keep compiler happy
299 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
301 template<class Form, class Type>
302 Form Foam::operator-(const Matrix<Form, Type>& a)
308 Type* nav = na.v_[0];
309 const Type* av = a.v_[0];
311 label nm = a.n_*a.m_;
312 for (register label i=0; i<nm; i++)
322 template<class Form, class Type>
323 Form Foam::operator+(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
329 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
330 ) << "attempted add matrices with different number of rows: "
331 << a.n_ << ", " << b.n_
332 << abort(FatalError);
339 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
340 ) << "attempted add matrices with different number of columns: "
341 << a.m_ << ", " << b.m_
342 << abort(FatalError);
347 Type* abv = ab.v_[0];
348 const Type* av = a.v_[0];
349 const Type* bv = b.v_[0];
351 label nm = a.n_*a.m_;
352 for (register label i=0; i<nm; i++)
354 abv[i] = av[i] + bv[i];
361 template<class Form, class Type>
362 Form Foam::operator-(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
368 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
369 ) << "attempted add matrices with different number of rows: "
370 << a.n_ << ", " << b.n_
371 << abort(FatalError);
378 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
379 ) << "attempted add matrices with different number of columns: "
380 << a.m_ << ", " << b.m_
381 << abort(FatalError);
386 Type* abv = ab.v_[0];
387 const Type* av = a.v_[0];
388 const Type* bv = b.v_[0];
390 label nm = a.n_*a.m_;
391 for (register label i=0; i<nm; i++)
393 abv[i] = av[i] - bv[i];
400 template<class Form, class Type>
401 Form Foam::operator*(const scalar s, const Matrix<Form, Type>& a)
407 Type* sav = sa.v_[0];
408 const Type* av = a.v_[0];
410 label nm = a.n_*a.m_;
411 for (register label i=0; i<nm; i++)
421 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
423 #include "MatrixIO.C"
425 // ************************************************************************* //