/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 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" #include "MatrixSpace.H" // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // template void Foam::Matrix::allocate() { if (mRows_ && nCols_) { v_ = new Type[size()]; } } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template Foam::Matrix::Matrix(const label m, const label n) : mRows_(m), nCols_(n), v_(nullptr) { if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } allocate(); } template Foam::Matrix::Matrix(const label m, const label n, const zero) : mRows_(m), nCols_(n), v_(nullptr) { if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } allocate(); if (v_) { const label mn = size(); for (label i=0; i Foam::Matrix::Matrix(const label m, const label n, const Type& s) : mRows_(m), nCols_(n), v_(nullptr) { if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } allocate(); if (v_) { const label mn = size(); for (label i=0; i template Foam::Matrix::Matrix ( const label m, const label n, InputIterator first, InputIterator last ) : mRows_(m), nCols_(n), v_(nullptr) { if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } if (std::distance(first, last) != mRows_*nCols_) { FatalErrorInFunction << "Number of values provided " << std::distance(first, last) << " is not the same as the number of matrix elements " << mRows_*nCols_ << abort(FatalError); } allocate(); if (v_) { const label mn = size(); InputIterator iter = first; for (label i=0; i Foam::Matrix::Matrix ( const label m, const label n, std::initializer_list lst ) : Matrix(m, n, lst.begin(), lst.end()) {} template Foam::Matrix::Matrix ( std::initializer_list> lstLst ) : mRows_(lstLst.size()), nCols_(lstLst.begin()->size()) { allocate(); label rowi = 0; label i = 0; for (const std::initializer_list& lst : lstLst) { if (label(lst.size()) != nCols_) { FatalErrorInFunction << "Number of columns in row " << rowi << " is not equal to " << nCols_ << abort(FatalError); } for (const Type& v : lst) { v_[i++] = v; } rowi++; } } template Foam::Matrix::Matrix(const Matrix& M) : mRows_(M.mRows_), nCols_(M.nCols_), v_(nullptr) { if (M.v_) { allocate(); const label mn = size(); for (label i=0; i template Foam::Matrix::Matrix(const Matrix& M) : mRows_(M.m()), nCols_(M.n()), v_(nullptr) { if (M.v()) { allocate(); const label mn = size(); for (label i=0; i template Foam::Matrix::Matrix ( const ConstMatrixBlock& Mb ) : mRows_(Mb.m()), nCols_(Mb.n()) { allocate(); for (label i=0; i template Foam::Matrix::Matrix ( const MatrixBlock& Mb ) : mRows_(Mb.m()), nCols_(Mb.n()) { allocate(); for (label i=0; i template Foam::Matrix::Matrix ( const MatrixSpace& Ms ) : mRows_(Mrows), nCols_(Ncols) { allocate(); for (label i=0; i Foam::Matrix::~Matrix() { if (v_) { delete[] v_; } } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template void Foam::Matrix::clear() { if (v_) { delete[] v_; v_ = nullptr; } mRows_ = 0; nCols_ = 0; } template void Foam::Matrix::transfer(Matrix& M) { clear(); mRows_ = M.mRows_; M.mRows_ = 0; nCols_ = M.nCols_; M.nCols_ = 0; v_ = M.v_; M.v_ = nullptr; } template void Foam::Matrix::setSize(const label m, const label n) { mType newMatrix(m, n, Zero); label minM = min(m, mRows_); label minN = min(n, nCols_); for (label i=0; i Form Foam::Matrix::T() const { const Matrix& A = *this; Form At(n(), m()); for (label i=0; i void Foam::Matrix::operator=(const Matrix& M) { if (this == &M) { FatalErrorInFunction << "Attempted assignment to self" << abort(FatalError); } if (mRows_ != M.mRows_ || nCols_ != M.nCols_) { clear(); mRows_ = M.mRows_; nCols_ = M.nCols_; allocate(); } if (v_) { const label mn = size(); for (label i=0; i template void Foam::Matrix::operator= ( const ConstMatrixBlock& Mb ) { for (label i=0; i template void Foam::Matrix::operator= ( const MatrixBlock& Mb ) { for (label i=0; i void Foam::Matrix::operator=(const Type& s) { if (v_) { const label mn = size(); for (label i=0; i void Foam::Matrix::operator=(const zero) { if (v_) { const label mn = size(); for (label i=0; i const Type& Foam::max(const Matrix& M) { const label mn = M.size(); if (mn) { label curMaxI = 0; const Type* Mv = M.v(); for (label i=1; i Mv[curMaxI]) { curMaxI = i; } } return Mv[curMaxI]; } else { FatalErrorInFunction << "Matrix is empty" << abort(FatalError); // Return in error to keep compiler happy return M(0, 0); } } template const Type& Foam::min(const Matrix& M) { const label mn = M.size(); if (mn) { label curMinI = 0; const Type* Mv = M.v(); for (label i=1; i Form Foam::operator-(const Matrix& M) { Form nM(M.m(), M.n()); if (M.m() && M.n()) { Type* nMv = nM.v(); const Type* Mv = M.v(); const label mn = M.size(); for (label i=0; i Form Foam::operator+(const Matrix& A, const Matrix& B) { if (A.m() != B.m()) { FatalErrorInFunction << "Attempt to add matrices with different numbers of rows: " << A.m() << ", " << B.m() << abort(FatalError); } if (A.n() != B.n()) { FatalErrorInFunction << "Attempt to add matrices with different numbers of columns: " << A.n() << ", " << B.n() << abort(FatalError); } Form AB(A.m(), A.n()); Type* ABv = AB.v(); const Type* Av = A.v(); const Type* Bv = B.v(); const label mn = A.size(); for (label i=0; i Form Foam::operator-(const Matrix& A, const Matrix& B) { if (A.m() != B.m()) { FatalErrorInFunction << "Attempt to add matrices with different numbers of rows: " << A.m() << ", " << B.m() << abort(FatalError); } if (A.n() != B.n()) { FatalErrorInFunction << "Attempt to add matrices with different numbers of columns: " << A.n() << ", " << B.n() << abort(FatalError); } Form AB(A.m(), A.n()); Type* ABv = AB.v(); const Type* Av = A.v(); const Type* Bv = B.v(); const label mn = A.size(); for (label i=0; i Form Foam::operator*(const scalar s, const Matrix& M) { Form sM(M.m(), M.n()); if (M.m() && M.n()) { Type* sMv = sM.v(); const Type* Mv = M.v(); const label mn = M.size(); for (label i=0; i Form Foam::operator*(const Matrix& M, const scalar s) { Form sM(M.m(), M.n()); if (M.m() && M.n()) { Type* sMv = sM.v(); const Type* Mv = M.v(); const label mn = M.size(); for (label i=0; i Form Foam::operator/(const Matrix& M, const scalar s) { Form sM(M.m(), M.n()); if (M.m() && M.n()) { Type* sMv = sM.v(); const Type* Mv = M.v(); const label mn = M.size(); for (label i=0; i typename Foam::typeOfInnerProduct::type Foam::operator* ( const Matrix& A, const Matrix& B ) { if (A.n() != B.m()) { FatalErrorInFunction << "Attempt to multiply incompatible matrices:" << nl << "Matrix A : " << A.m() << " x " << A.n() << nl << "Matrix B : " << B.m() << " x " << B.n() << nl << "In order to multiply matrices, columns of A must equal " << "rows of B" << abort(FatalError); } typename typeOfInnerProduct::type AB ( A.m(), B.n(), Zero ); for (label i=0; i inline Foam::tmp> Foam::operator* ( const Matrix& M, const Field& f ) { if (M.n() != f.size()) { FatalErrorInFunction << "Attempt to multiply incompatible matrix and field:" << nl << "Matrix : " << M.m() << " x " << M.n() << nl << "Field : " << f.size() << " rows" << nl << "In order to multiply a matrix M and field f, " "columns of M must equal rows of f" << abort(FatalError); } tmp> tMf(new Field(M.m(), Zero)); Field& Mf = tMf.ref(); for (label i=0; i