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)
211 "Matrix<Form, Type>::operator=(const Matrix<Form, Type>&)"
212 ) << "attempted assignment to self"
213 << abort(FatalError);
216 if (n_ != a.n_ || m_ != a.m_)
227 const Type* av = a.v_[0];
230 for (register label i=0; i<nm; i++)
238 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
240 template<class Form, class Type>
241 const Type& Foam::max(const Matrix<Form, Type>& a)
243 label nm = a.n()*a.m();
248 const Type* v = a[0];
250 for (register label i=1; i<nm; i++)
252 if (v[i] > v[curMaxI])
262 FatalErrorIn("max(const Matrix<Form, Type>&)")
264 << abort(FatalError);
266 // Return in error to keep compiler happy
272 template<class Form, class Type>
273 const Type& Foam::min(const Matrix<Form, Type>& a)
275 label nm = a.n()*a.m();
280 const Type* v = a[0];
282 for (register label i=1; i<nm; i++)
284 if (v[i] < v[curMinI])
294 FatalErrorIn("min(const Matrix<Form, Type>&)")
296 << abort(FatalError);
298 // Return in error to keep compiler happy
304 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
306 template<class Form, class Type>
307 Form Foam::operator-(const Matrix<Form, Type>& a)
309 Form na(a.n(), a.m());
314 const Type* av = a[0];
316 label nm = a.n()*a.m();
317 for (register label i=0; i<nm; i++)
327 template<class Form, class Type>
328 Form Foam::operator+(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
334 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, "
335 "const Matrix<Form, Type>&)"
336 ) << "attempted add matrices with different number of rows: "
337 << a.n() << ", " << b.n()
338 << abort(FatalError);
345 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, "
346 "const Matrix<Form, Type>&)"
347 ) << "attempted add matrices with different number of columns: "
348 << a.m() << ", " << b.m()
349 << abort(FatalError);
352 Form ab(a.n(), a.m());
355 const Type* av = a[0];
356 const Type* bv = b[0];
358 label nm = a.n()*a.m();
359 for (register label i=0; i<nm; i++)
361 abv[i] = av[i] + bv[i];
368 template<class Form, class Type>
369 Form Foam::operator-(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
375 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, "
376 "const Matrix<Form, Type>&)"
377 ) << "attempted add matrices with different number of rows: "
378 << a.n() << ", " << b.n()
379 << abort(FatalError);
386 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, "
387 "const Matrix<Form, Type>&)"
388 ) << "attempted add matrices with different number of columns: "
389 << a.m() << ", " << b.m()
390 << abort(FatalError);
393 Form ab(a.n(), a.m());
396 const Type* av = a[0];
397 const Type* bv = b[0];
399 label nm = a.n()*a.m();
400 for (register label i=0; i<nm; i++)
402 abv[i] = av[i] - bv[i];
409 template<class Form, class Type>
410 Form Foam::operator*(const scalar s, const Matrix<Form, Type>& a)
412 Form sa(a.n(), a.m());
417 const Type* av = a[0];
419 label nm = a.n()*a.m();
420 for (register label i=0; i<nm; i++)
430 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
432 #include "MatrixIO.C"
434 // ************************************************************************* //