/*---------------------------------------------------------------------------*\ ========= | \\ / 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 . Class Foam::Matrix Description A templated (m x n) matrix of objects of \. SourceFiles Matrix.C MatrixI.H MatrixIO.C \*---------------------------------------------------------------------------*/ #ifndef Matrix_H #define Matrix_H #include "bool.H" #include "label.H" #include "uLabel.H" #include "Field.H" #include "zero.H" #include "autoPtr.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { // Forward declaration of friend functions and operators template class Matrix; template Istream& operator>> ( Istream&, Matrix& ); template Ostream& operator<< ( Ostream&, const Matrix& ); template class ConstMatrixBlock; template class MatrixBlock; template class MatrixSpace; /*---------------------------------------------------------------------------*\ Class Matrix Declaration \*---------------------------------------------------------------------------*/ template class Matrix { // Private Data //- Number of rows and columns in Matrix. label mRows_, nCols_; //- Row pointers Type* __restrict__ v_; //- Allocate the storage for the element vector void allocate(); public: //- Matrix type typedef Matrix mType; //- Component type typedef Type cmptType; // Static Member Functions //- Return a null Matrix inline static const mType& null(); // Constructors //- Null constructor. inline Matrix(); //- Construct given number of rows and columns. Matrix(const label m, const label n); //- Construct with given number of rows and columns // initialising all elements to zero Matrix(const label m, const label n, const zero); //- Construct with given number of rows and columns // initialising all elements to the given value Matrix(const label m, const label n, const Type&); //- Construct with given number of rows and columns // and start and end iterators template Matrix ( const label m, const label n, InputIterator first, InputIterator last ); //- Construct with given number of rows and columns and initialiser list Matrix(const label m, const label n, std::initializer_list); //- Construct from initialiser list list Matrix(std::initializer_list>); //- Copy constructor. Matrix(const mType&); //- Copy constructor from matrix of a different form template explicit Matrix(const Matrix&); //- Construct from a block of another matrix template Matrix(const ConstMatrixBlock&); //- Construct from a block of another matrix template Matrix(const MatrixBlock&); template Matrix(const MatrixSpace&); //- Construct from Istream. Matrix(Istream&); //- Clone inline autoPtr clone() const; //- Destructor ~Matrix(); // Member Functions // Access //- Return the number of rows inline label m() const; //- Return the number of columns inline label n() const; //- Return the number of elements in matrix (m*n) inline label size() const; //- Return element vector of the constant Matrix inline const Type* v() const; //- Return element vector of the Matrix inline Type* v(); // Block access inline ConstMatrixBlock block ( const label m, const label n, const label mStart, const label nStart ) const; template inline ConstMatrixBlock block ( const label mStart, const label nStart ) const; inline ConstMatrixBlock col ( const label m, const label rowStart ) const; inline ConstMatrixBlock col ( const label m, const label mStart, const label nStart ) const; inline MatrixBlock block ( const label m, const label n, const label mStart, const label nStart ); template inline MatrixBlock block ( const label mStart, const label nStart ); inline MatrixBlock col ( const label m, const label rowStart ); inline MatrixBlock col ( const label m, const label mStart, const label nStart ); // Check //- Check index i is within valid range (0 ... m-1). inline void checki(const label i) const; //- Check index j is within valid range (0 ... n-1). inline void checkj(const label j) const; // Edit //- Clear the Matrix, i.e. set sizes to zero. void clear(); //- Transfer the contents of the argument Matrix into this Matrix // and annul the argument Matrix. void transfer(mType&); //- Resize the matrix preserving the elements void setSize(const label m, const label n); //- Resize the matrix without reallocating storage (unsafe) inline void shallowResize(const label m, const label n); //- Return the transpose of the matrix Form T() const; // Member Operators //- Return subscript-checked row of Matrix. inline Type* operator[](const label); //- Return subscript-checked row of constant Matrix. inline const Type* operator[](const label) const; //- (i, j) const element access operator inline const Type& operator()(const label i, const label j) const; //- (i, j) element access operator inline Type& operator()(const label i, const label j); //- Assignment operator. Takes linear time. void operator=(const mType&); //- Assignment to a block of another matrix template void operator=(const ConstMatrixBlock&); //- Assignment to a block of another matrix template void operator=(const MatrixBlock&); //- Assignment of all elements to zero void operator=(const zero); //- Assignment of all elements to the given value void operator=(const Type&); // IOstream Operators //- Read Matrix from Istream, discarding contents of existing Matrix. friend Istream& operator>> ( Istream&, mType& ); //- Write Matrix to Ostream. friend Ostream& operator<< ( Ostream&, const mType& ); }; namespace expression { template struct Expressionable> : public notExpressionable {}; } // Global functions and operators template void writeEntry(Ostream&, const Matrix&); template const Type& max(const Matrix&); template const Type& min(const Matrix&); template Form operator-(const Matrix&); template Form operator+ ( const Matrix&, const Matrix& ); template Form operator- ( const Matrix&, const Matrix& ); template Form operator* ( const scalar, const Matrix& ); template Form operator* ( const Matrix&, const scalar ); template Form operator/ ( const Matrix&, const scalar ); template typename typeOfInnerProduct::type operator* ( const Matrix& a, const Matrix& b ); template tmp> operator* ( const Matrix&, const Field& ); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #include "MatrixI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository #include "Matrix.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif // ************************************************************************* //