1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
28 // * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
30 template<class Form, class Type>
31 const Foam::Matrix<Form, Type> Foam::Matrix<Form, Type>::zero;
34 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
36 template<class Form, class Type>
37 void Foam::Matrix<Form, Type>::allocate()
42 v_[0] = new Type[n_*m_];
44 for (register label i=1; i<n_; i++)
52 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
54 template<class Form, class Type>
55 Foam::Matrix<Form, Type>::~Matrix()
65 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
67 template<class Form, class Type>
68 Foam::Matrix<Form, Type>::Matrix(const label n, const label m)
78 "Matrix<Form, Type>::Matrix(const label n, const label m)"
79 ) << "bad n, m " << n_ << ", " << m_
87 template<class Form, class Type>
88 Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
98 "Matrix<Form, Type>::Matrix"
99 "(const label n, const label m, const T&)"
100 ) << "bad n, m " << n_ << ", " << m_
101 << abort(FatalError);
112 for (register label i=0; i<nm; i++)
120 template<class Form, class Type>
121 Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
131 const Type* av = a.v_[0];
134 for (register label i=0; i<nm; i++)
142 template<class Form, class Type>
143 void Foam::Matrix<Form, Type>::clear()
156 template<class Form, class Type>
157 void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& a)
172 template<class Form, class Type>
173 Form Foam::Matrix<Form, Type>::T() const
175 const Matrix<Form, Type>& A = *this;
178 for (register label i=0; i<n(); i++)
180 for (register label j=0; j<m(); j++)
190 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
192 template<class Form, class Type>
193 void Foam::Matrix<Form, Type>::operator=(const Type& t)
200 for (register label i=0; i<nm; i++)
208 // Assignment operator. Takes linear time.
209 template<class Form, class Type>
210 void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
216 "Matrix<Form, Type>::operator=(const Matrix<Form, Type>&)"
217 ) << "attempted assignment to self"
218 << abort(FatalError);
221 if (n_ != a.n_ || m_ != a.m_)
232 const Type* av = a.v_[0];
235 for (register label i=0; i<nm; i++)
243 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
245 template<class Form, class Type>
246 const Type& Foam::max(const Matrix<Form, Type>& a)
248 label nm = a.n()*a.m();
253 const Type* v = a[0];
255 for (register label i=1; i<nm; i++)
257 if (v[i] > v[curMaxI])
267 FatalErrorIn("max(const Matrix<Form, Type>&)")
269 << abort(FatalError);
271 // Return in error to keep compiler happy
277 template<class Form, class Type>
278 const Type& Foam::min(const Matrix<Form, Type>& a)
280 label nm = a.n()*a.m();
285 const Type* v = a[0];
287 for (register label i=1; i<nm; i++)
289 if (v[i] < v[curMinI])
299 FatalErrorIn("min(const Matrix<Form, Type>&)")
301 << abort(FatalError);
303 // Return in error to keep compiler happy
309 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
311 template<class Form, class Type>
312 Form Foam::operator-(const Matrix<Form, Type>& a)
314 Form na(a.n(), a.m());
319 const Type* av = a[0];
321 label nm = a.n()*a.m();
322 for (register label i=0; i<nm; i++)
332 template<class Form, class Type>
333 Form Foam::operator+(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
339 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, "
340 "const Matrix<Form, Type>&)"
341 ) << "attempted add matrices with different number of rows: "
342 << a.n() << ", " << b.n()
343 << abort(FatalError);
350 "Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, "
351 "const Matrix<Form, Type>&)"
352 ) << "attempted add matrices with different number of columns: "
353 << a.m() << ", " << b.m()
354 << abort(FatalError);
357 Form ab(a.n(), a.m());
360 const Type* av = a[0];
361 const Type* bv = b[0];
363 label nm = a.n()*a.m();
364 for (register label i=0; i<nm; i++)
366 abv[i] = av[i] + bv[i];
373 template<class Form, class Type>
374 Form Foam::operator-(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
380 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, "
381 "const Matrix<Form, Type>&)"
382 ) << "attempted add matrices with different number of rows: "
383 << a.n() << ", " << b.n()
384 << abort(FatalError);
391 "Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, "
392 "const Matrix<Form, Type>&)"
393 ) << "attempted add matrices with different number of columns: "
394 << a.m() << ", " << b.m()
395 << abort(FatalError);
398 Form ab(a.n(), a.m());
401 const Type* av = a[0];
402 const Type* bv = b[0];
404 label nm = a.n()*a.m();
405 for (register label i=0; i<nm; i++)
407 abv[i] = av[i] - bv[i];
414 template<class Form, class Type>
415 Form Foam::operator*(const scalar s, const Matrix<Form, Type>& a)
417 Form sa(a.n(), a.m());
422 const Type* av = a[0];
424 label nm = a.n()*a.m();
425 for (register label i=0; i<nm; i++)
435 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
437 #include "MatrixIO.C"
439 // ************************************************************************* //