1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
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)
73 "Matrix<Form, Type>::Matrix(const label n, const label m)"
74 ) << "bad n, m " << n_ << ", " << m_
82 template<class Form, class Type>
83 Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
93 "Matrix<Form, Type>::Matrix"
94 "(const label n, const label m, const T&)"
95 ) << "bad n, m " << n_ << ", " << m_
107 for (register label i=0; i<nm; i++)
115 template<class Form, class Type>
116 Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
126 const Type* av = a.v_[0];
129 for (register label i=0; i<nm; i++)
137 template<class Form, class Type>
138 void Foam::Matrix<Form, Type>::clear()
151 template<class Form, class Type>
152 void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& a)
167 template<class Form, class Type>
168 Form Foam::Matrix<Form, Type>::T() const
170 const Matrix<Form, Type>& A = *this;
173 for (register label i=0; i<n(); i++)
175 for (register label j=0; j<m(); j++)
185 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
187 template<class Form, class Type>
188 void Foam::Matrix<Form, Type>::operator=(const Type& t)
195 for (register label i=0; i<nm; i++)
203 // Assignment operator. Takes linear time.
204 template<class Form, class Type>
205 void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
209 FatalErrorIn("Matrix<Form, Type>::operator=(const Matrix<Form, Type>&)")
210 << "attempted assignment to self"
211 << abort(FatalError);
214 if (n_ != a.n_ || m_ != a.m_)
225 const Type* av = a.v_[0];
228 for (register label i=0; i<nm; i++)
236 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
238 template<class Form, class Type>
239 const Type& Foam::max(const Matrix<Form, Type>& a)
241 label nm = a.n()*a.m();
246 const Type* v = a[0];
248 for (register label i=1; i<nm; i++)
250 if (v[i] > v[curMaxI])
260 FatalErrorIn("max(const Matrix<Form, Type>&)")
262 << abort(FatalError);
264 // Return in error to keep compiler happy
270 template<class Form, class Type>
271 const Type& Foam::min(const Matrix<Form, Type>& a)
273 label nm = a.n()*a.m();
278 const Type* v = a[0];
280 for (register label i=1; i<nm; i++)
282 if (v[i] < v[curMinI])
292 FatalErrorIn("min(const Matrix<Form, Type>&)")
294 << abort(FatalError);
296 // Return in error to keep compiler happy
302 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
304 template<class Form, class Type>
305 Form Foam::operator-(const Matrix<Form, Type>& a)
307 Form na(a.n(), a.m());
312 const Type* av = a[0];
314 label nm = a.n()*a.m();
315 for (register label i=0; i<nm; i++)
325 template<class Form, class Type>
326 Form Foam::operator+(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
332 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
333 ) << "attempted add matrices with different number of rows: "
334 << a.n() << ", " << b.n()
335 << abort(FatalError);
342 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
343 ) << "attempted add matrices with different number of columns: "
344 << a.m() << ", " << b.m()
345 << abort(FatalError);
348 Form ab(a.n(), a.m());
351 const Type* av = a[0];
352 const Type* bv = b[0];
354 label nm = a.n()*a.m();
355 for (register label i=0; i<nm; i++)
357 abv[i] = av[i] + bv[i];
364 template<class Form, class Type>
365 Form Foam::operator-(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
371 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
372 ) << "attempted add matrices with different number of rows: "
373 << a.n() << ", " << b.n()
374 << abort(FatalError);
381 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
382 ) << "attempted add matrices with different number of columns: "
383 << a.m() << ", " << b.m()
384 << abort(FatalError);
387 Form ab(a.n(), a.m());
390 const Type* av = a[0];
391 const Type* bv = b[0];
393 label nm = a.n()*a.m();
394 for (register label i=0; i<nm; i++)
396 abv[i] = av[i] - bv[i];
403 template<class Form, class Type>
404 Form Foam::operator*(const scalar s, const Matrix<Form, Type>& a)
406 Form sa(a.n(), a.m());
411 const Type* av = a[0];
413 label nm = a.n()*a.m();
414 for (register label i=0; i<nm; i++)
424 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
426 #include "MatrixIO.C"
428 // ************************************************************************* //