diff --git a/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrix.H b/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrix.H index a947d8105f..7167a23f0c 100644 --- a/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrix.H +++ b/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrix.H @@ -39,6 +39,7 @@ SourceFiles #define RectangularMatrix_H #include "Matrix.H" +#include "SquareMatrix.H" #include "Identity.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -67,7 +68,12 @@ public: inline RectangularMatrix(const label m, const label n); //- Construct from a block of another matrix - inline RectangularMatrix(const typename RectangularMatrix::Block&); + template + inline RectangularMatrix(const ConstMatrixBlock&); + + //- Construct from a block of another matrix + template + inline RectangularMatrix(const MatrixBlock&); //- Construct with given number of rows and columns // initializing all elements to zero @@ -81,6 +87,9 @@ public: // and value for all elements. inline RectangularMatrix(const label m, const label n, const Type&); + //- Construct as copy of a square matrix + inline RectangularMatrix(const SquareMatrix&); + //- Construct from Istream. inline RectangularMatrix(Istream&); @@ -97,6 +106,31 @@ public: // Global functions and operators +template +class typeOfInnerProduct, RectangularMatrix> +{ +public: + + typedef RectangularMatrix type; +}; + +template +class typeOfInnerProduct, SquareMatrix> +{ +public: + + typedef RectangularMatrix type; +}; + +template +class typeOfInnerProduct, RectangularMatrix> +{ +public: + + typedef RectangularMatrix type; +}; + + template RectangularMatrix outer(const Field& f1, const Field& f2); diff --git a/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrixI.H b/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrixI.H index 631cc8b93c..f7a26784dc 100644 --- a/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrixI.H +++ b/src/OpenFOAM/matrices/RectangularMatrix/RectangularMatrixI.H @@ -44,9 +44,21 @@ inline Foam::RectangularMatrix::RectangularMatrix template +template inline Foam::RectangularMatrix::RectangularMatrix ( - const typename RectangularMatrix::Block& block + const ConstMatrixBlock& block +) +: + Matrix, Type>(block) +{} + + +template +template +inline Foam::RectangularMatrix::RectangularMatrix +( + const MatrixBlock& block ) : Matrix, Type>(block) @@ -74,7 +86,7 @@ inline Foam::RectangularMatrix::RectangularMatrix : Matrix, Type>(n, n, Zero) { - for (label i = 0; i < n; ++i) + for (label i = 0; i < n; i++) { this->operator()(i, i) = I; } @@ -93,6 +105,16 @@ inline Foam::RectangularMatrix::RectangularMatrix {} +template +inline Foam::RectangularMatrix::RectangularMatrix +( + const SquareMatrix& SM +) +: + Matrix, Type>(SM) +{} + + template inline Foam::RectangularMatrix::RectangularMatrix(Istream& is) : @@ -136,9 +158,9 @@ inline Foam::RectangularMatrix outer { RectangularMatrix f1f2T(f1.size(), f2.size()); - for (label i=0; i::one; - for (label i = 0; i < matrix.m(); ++i) + for (label i = 0; i < matrix.m(); i++) { diagProduct *= matrix(i, i); } diff --git a/src/OpenFOAM/matrices/SquareMatrix/SquareMatrix.H b/src/OpenFOAM/matrices/SquareMatrix/SquareMatrix.H index 39403194d5..2af8a5d542 100644 --- a/src/OpenFOAM/matrices/SquareMatrix/SquareMatrix.H +++ b/src/OpenFOAM/matrices/SquareMatrix/SquareMatrix.H @@ -45,6 +45,12 @@ SourceFiles namespace Foam { +// Forward declaration of friend functions and operators + +template +class RectangularMatrix; + + /*---------------------------------------------------------------------------*\ Class Matrix Declaration \*---------------------------------------------------------------------------*/ @@ -65,10 +71,22 @@ public: //- Construct given number of rows/columns. inline SquareMatrix(const label n); + //- Construct from a block of another matrix + template + inline SquareMatrix(const ConstMatrixBlock&); + + //- Construct from a block of another matrix + template + inline SquareMatrix(const MatrixBlock&); + //- Construct given number of rows/columns // initializing all elements to zero inline SquareMatrix(const label n, const zero); + //- Construct given number of rows and columns (checked to be equal) + // initializing all elements to zero + inline SquareMatrix(const label m, const label n, const zero); + //- Construct given number of rows/columns // Initializing to the identity matrix inline SquareMatrix(const label n, const Identity); @@ -77,15 +95,25 @@ public: // initializing all elements to the given value inline SquareMatrix(const label n, const Type&); + //- Construct as copy of a RectangularMatrix + // which is checked to be square + inline explicit SquareMatrix(const RectangularMatrix&); + //- Construct from Istream. inline SquareMatrix(Istream&); //- Clone inline autoPtr> clone() const; + + + // Member operators + + //- Assignment of all entries to zero + void operator=(const zero); }; -// Global functions +// Global functions and operators //- Return the LU decomposed SquareMatrix det template @@ -99,6 +127,14 @@ scalar det(const SquareMatrix&); template scalar det(SquareMatrix&); +template +class typeOfInnerProduct, SquareMatrix> +{ +public: + + typedef SquareMatrix type; +}; + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -106,7 +142,7 @@ scalar det(SquareMatrix&); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #include "SquareMatrixI.H" +#include "SquareMatrixI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/matrices/SquareMatrix/SquareMatrixI.H b/src/OpenFOAM/matrices/SquareMatrix/SquareMatrixI.H index ffa595d51d..873259380e 100644 --- a/src/OpenFOAM/matrices/SquareMatrix/SquareMatrixI.H +++ b/src/OpenFOAM/matrices/SquareMatrix/SquareMatrixI.H @@ -39,6 +39,28 @@ inline Foam::SquareMatrix::SquareMatrix(const label n) {} +template +template +inline Foam::SquareMatrix::SquareMatrix +( + const ConstMatrixBlock& block +) +: + Matrix, Type>(block) +{} + + +template +template +inline Foam::SquareMatrix::SquareMatrix +( + const MatrixBlock& block +) +: + Matrix, Type>(block) +{} + + template inline Foam::SquareMatrix::SquareMatrix ( @@ -50,6 +72,26 @@ inline Foam::SquareMatrix::SquareMatrix {} +template +inline Foam::SquareMatrix::SquareMatrix +( + const label m, + const label n, + const zero +) +: + Matrix, Type>(m, n, Zero) +{ + if (m != n) + { + FatalErrorInFunction + << "Attempt to construct a square matrix " + << m << " x " << n << nl + << abort(FatalError); + } +} + + template inline Foam::SquareMatrix::SquareMatrix ( @@ -59,7 +101,7 @@ inline Foam::SquareMatrix::SquareMatrix : Matrix, Type>(n, n, Zero) { - for (label i = 0; i < n; ++i) + for (label i = 0; i < n; i++) { this->operator()(i, i) = I; } @@ -77,6 +119,24 @@ inline Foam::SquareMatrix::SquareMatrix {} +template +inline Foam::SquareMatrix::SquareMatrix +( + const RectangularMatrix& RM +) +: + Matrix, Type>(RM) +{ + if (this->m() != this->n()) + { + FatalErrorInFunction + << "Attempt to construct a square matrix from a rectangular matrix " + << this->m() << " x " << this->n() << nl + << abort(FatalError); + } +} + + template inline Foam::SquareMatrix::SquareMatrix(Istream& is) : @@ -92,4 +152,45 @@ Foam::SquareMatrix::clone() const } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +template +void Foam::SquareMatrix::operator=(const zero) +{ + Matrix, Type>::operator=(Zero); +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * // + +template +inline Foam::SquareMatrix symmOuter +( + const Field& f1, + const Field& f2 +) +{ + SquareMatrix f1f2T(f1.size()); + + for (label i=0; i Foam::invDecomposed SymmetricSquareMatrix inv(n, Zero); - for (label i = 0; i < n; ++i) + for (label i = 0; i < n; i++) { inv(i, i) = 1.0/matrix(i, i); - for (label j = 0; j < i; ++j) + for (label j = 0; j < i; j++) { Type sum = Zero; @@ -89,7 +89,7 @@ Type Foam::detDecomposed(const SymmetricSquareMatrix& matrix) { Type diagProduct = pTraits::one; - for (label i = 0; i < matrix.m(); ++i) + for (label i = 0; i < matrix.m(); i++) { diagProduct *= matrix(i, i); }