/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . \*---------------------------------------------------------------------------*/ #include "Matrix.H" // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // template void Foam::Matrix::allocate() { if (n_ && m_) { v_ = new Type*[n_]; v_[0] = new Type[n_*m_]; for (label i=1; i Foam::Matrix::~Matrix() { if (v_) { delete[] (v_[0]); delete[] v_; } } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template Foam::Matrix::Matrix(const label n, const label m) : n_(n), m_(m), v_(NULL) { if (n_ < 0 || m_ < 0) { FatalErrorInFunction << "bad n, m " << n_ << ", " << m_ << abort(FatalError); } allocate(); } template Foam::Matrix::Matrix(const label n, const label m, const Type& a) : n_(n), m_(m), v_(NULL) { if (n_ < 0 || m_ < 0) { FatalErrorInFunction << "bad n, m " << n_ << ", " << m_ << abort(FatalError); } allocate(); if (v_) { Type* v = v_[0]; label nm = n_*m_; for (label i=0; i Foam::Matrix::Matrix(const Matrix& a) : n_(a.n_), m_(a.m_), v_(NULL) { if (a.v_) { allocate(); Type* v = v_[0]; const Type* av = a.v_[0]; label nm = n_*m_; for (label i=0; i void Foam::Matrix::clear() { if (v_) { delete[] (v_[0]); delete[] v_; } n_ = 0; m_ = 0; v_ = NULL; } template void Foam::Matrix::transfer(Matrix& a) { clear(); n_ = a.n_; a.n_ = 0; m_ = a.m_; a.m_ = 0; v_ = a.v_; a.v_ = NULL; } template Form Foam::Matrix::T() const { const Matrix& A = *this; Form At(m(), n()); for (label i=0; i void Foam::Matrix::operator=(const Type& t) { if (v_) { Type* v = v_[0]; label nm = n_*m_; for (label i=0; i void Foam::Matrix::operator=(const Matrix& a) { if (this == &a) { FatalErrorInFunction << "attempted assignment to self" << abort(FatalError); } if (n_ != a.n_ || m_ != a.m_) { clear(); n_ = a.n_; m_ = a.m_; allocate(); } if (v_) { Type* v = v_[0]; const Type* av = a.v_[0]; label nm = n_*m_; for (label i=0; i const Type& Foam::max(const Matrix& a) { label nm = a.n()*a.m(); if (nm) { label curMaxI = 0; const Type* v = a[0]; for (label i=1; i v[curMaxI]) { curMaxI = i; } } return v[curMaxI]; } else { FatalErrorInFunction << "matrix is empty" << abort(FatalError); // Return in error to keep compiler happy return a[0][0]; } } template const Type& Foam::min(const Matrix& a) { label nm = a.n()*a.m(); if (nm) { label curMinI = 0; const Type* v = a[0]; for (label i=1; i Form Foam::operator-(const Matrix& a) { Form na(a.n(), a.m()); if (a.n() && a.m()) { Type* nav = na[0]; const Type* av = a[0]; label nm = a.n()*a.m(); for (label i=0; i Form Foam::operator+(const Matrix& a, const Matrix& b) { if (a.n() != b.n()) { FatalErrorInFunction << "attempted add matrices with different number of rows: " << a.n() << ", " << b.n() << abort(FatalError); } if (a.m() != b.m()) { FatalErrorInFunction << "attempted add matrices with different number of columns: " << a.m() << ", " << b.m() << abort(FatalError); } Form ab(a.n(), a.m()); Type* abv = ab[0]; const Type* av = a[0]; const Type* bv = b[0]; label nm = a.n()*a.m(); for (label i=0; i Form Foam::operator-(const Matrix& a, const Matrix& b) { if (a.n() != b.n()) { FatalErrorInFunction << "attempted add matrices with different number of rows: " << a.n() << ", " << b.n() << abort(FatalError); } if (a.m() != b.m()) { FatalErrorInFunction << "attempted add matrices with different number of columns: " << a.m() << ", " << b.m() << abort(FatalError); } Form ab(a.n(), a.m()); Type* abv = ab[0]; const Type* av = a[0]; const Type* bv = b[0]; label nm = a.n()*a.m(); for (label i=0; i Form Foam::operator*(const scalar s, const Matrix& a) { Form sa(a.n(), a.m()); if (a.n() && a.m()) { Type* sav = sa[0]; const Type* av = a[0]; label nm = a.n()*a.m(); for (label i=0; i Form Foam::operator*(const Matrix& a, const Matrix& b) { if (a.m() != b.n()) { FatalErrorInFunction << "attempted to multiply incompatible matrices:" << nl << "Matrix A : " << a.n() << " rows, " << a.m() << " columns" << nl << "Matrix B : " << b.n() << " rows, " << b.m() << " columns" << nl << "In order to multiply matrices, columns of A must equal " << "rows of B" << abort(FatalError); } Form ab(a.n(), b.m(), scalar(0)); for (label i = 0; i < ab.n(); i++) { for (label j = 0; j < ab.m(); j++) { for (label l = 0; l < b.n(); l++) { ab[i][j] += a[i][l]*b[l][j]; } } } return ab; } // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * // #include "MatrixIO.C" // ************************************************************************* //