ENH: improve Matrix classes and tests

This commit is contained in:
Kutalmis Bercin
2020-03-02 14:24:44 +00:00
committed by Andrew Heather
parent b3e5620d2a
commit af22163492
33 changed files with 3100 additions and 363 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,14 +28,7 @@ License
#include "DiagonalMatrix.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
inline Foam::DiagonalMatrix<Type>::DiagonalMatrix()
:
List<Type>()
{}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::DiagonalMatrix<Type>::DiagonalMatrix(const label n)
@ -74,8 +67,10 @@ Foam::DiagonalMatrix<Type>::DiagonalMatrix(const Matrix<Form, Type>& mat)
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Foam::DiagonalMatrix<Type>& Foam::DiagonalMatrix<Type>::invert()
void Foam::DiagonalMatrix<Type>::invert()
{
for (Type& val : *this)
{
@ -88,13 +83,75 @@ Foam::DiagonalMatrix<Type>& Foam::DiagonalMatrix<Type>::invert()
val = Type(1)/val;
}
}
return this;
}
template<class Type>
Foam::DiagonalMatrix<Type> Foam::inv(const DiagonalMatrix<Type>& mat)
template<class CompOp>
Foam::List<Foam::label> Foam::DiagonalMatrix<Type>::sortPermutation
(
CompOp& compare
) const
{
List<label> p(this->size());
std::iota(p.begin(), p.end(), 0);
std::sort
(
p.begin(),
p.end(),
[&](label i, label j){ return compare((*this)[i], (*this)[j]); }
);
return p;
}
template<class Type>
void Foam::DiagonalMatrix<Type>::applyPermutation(const List<label>& p)
{
#ifdef FULLDEBUG
if (this->size() != p.size())
{
FatalErrorInFunction
<< "Attempt to column-reorder according to an uneven list: " << nl
<< "DiagonalMatrix diagonal size = " << this->size() << nl
<< "Permutation list size = " << p.size() << nl
<< abort(FatalError);
}
#endif
List<bool> pass(p.size(), false);
for (label i = 0; i < p.size(); ++i)
{
if (pass[i])
{
continue;
}
pass[i] = true;
label prev = i;
label j = p[i];
while (i != j)
{
Swap((*this)[prev], (*this)[j]);
pass[j] = true;
prev = j;
j = p[j];
}
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- Return the matrix inverse as a DiagonalMatrix if no elem is equal to zero
template<class Type>
DiagonalMatrix<Type> inv(const DiagonalMatrix<Type>& mat)
{
DiagonalMatrix<Type> Ainv(mat.size());
@ -118,4 +175,41 @@ Foam::DiagonalMatrix<Type> Foam::inv(const DiagonalMatrix<Type>& mat)
}
//- Return Matrix column-reordered according to
//- a given permutation labelList
template<class Type>
DiagonalMatrix<Type> applyPermutation
(
const DiagonalMatrix<Type>& mat,
const List<label>& p
)
{
#ifdef FULLDEBUG
if (mat.size() != p.size())
{
FatalErrorInFunction
<< "Attempt to column-reorder according to an uneven list: " << nl
<< "DiagonalMatrix diagonal size = " << mat.size() << nl
<< "Permutation list size = " << p.size() << nl
<< abort(FatalError);
}
#endif
DiagonalMatrix<Type> reordered(mat.size());
label j = 0;
for (const label i : p)
{
reordered[j] = mat[i];
++j;
}
return reordered;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,7 +28,11 @@ Class
Foam::DiagonalMatrix
Description
A 2D diagonal matrix of objects of type \<Type\>, size (N x N)
A templated (N x N) diagonal matrix of objects of \<Type\>, effectively
containing N elements, derived from List.
See also
Test-DiagonalMatrix.C
SourceFiles
DiagonalMatrix.C
@ -39,6 +43,7 @@ SourceFiles
#define DiagonalMatrix_H
#include "List.H"
#include <numeric>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -59,19 +64,27 @@ class DiagonalMatrix
{
public:
// Constructors
// Generated Methods
//- Construct null.
DiagonalMatrix<Type>();
//- Default construct
DiagonalMatrix() = default;
//- Copy construct
DiagonalMatrix(const DiagonalMatrix&) = default;
//- Copy assignment
DiagonalMatrix& operator=(const DiagonalMatrix&) = default;
// Constructors
//- Construct empty from size
explicit DiagonalMatrix<Type>(const label n);
//- Construct from size
//- initializing all elements to the given value
//- Construct from size and initialise all elems to zero
DiagonalMatrix<Type>(const label n, const zero);
//- Construct from size and a value
//- Construct from size and initialise all elems to value
DiagonalMatrix<Type>(const label n, const Type& val);
//- Construct from the diagonal of a Matrix
@ -81,19 +94,20 @@ public:
// Member Functions
//- Invert the diagonal matrix and return itself
DiagonalMatrix<Type>& invert();
//- Return the matrix inverse into itself if no elem is equal to zero
void invert();
//- Return a sort permutation labelList according to
//- a given comparison on the diagonal entries
template<class CompOp>
List<label> sortPermutation(CompOp& compare) const;
//- Column-reorder this Matrix according to
//- a given permutation labelList
void applyPermutation(const List<label>& p);
};
// Global functions
//- Return the diagonal Matrix inverse
template<class Type>
DiagonalMatrix<Type> inv(const DiagonalMatrix<Type>& mat);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -644,10 +644,16 @@ void Foam::Matrix<Form, Type>::operator/=(const Type& s)
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
//- Find max value in Matrix
template<class Form, class Type>
const Type& Foam::max(const Matrix<Form, Type>& mat)
const Type& max(const Matrix<Form, Type>& mat)
{
if (mat.empty())
{
@ -659,8 +665,9 @@ const Type& Foam::max(const Matrix<Form, Type>& mat)
}
//- Find min value in Matrix
template<class Form, class Type>
const Type& Foam::min(const Matrix<Form, Type>& mat)
const Type& min(const Matrix<Form, Type>& mat)
{
if (mat.empty())
{
@ -672,8 +679,9 @@ const Type& Foam::min(const Matrix<Form, Type>& mat)
}
//- Find the min/max values of Matrix
template<class Form, class Type>
Foam::MinMax<Type> Foam::minMax(const Matrix<Form, Type>& mat)
MinMax<Type> minMax(const Matrix<Form, Type>& mat)
{
MinMax<Type> result;
@ -686,10 +694,12 @@ Foam::MinMax<Type> Foam::minMax(const Matrix<Form, Type>& mat)
}
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
//- Matrix negation
template<class Form, class Type>
Form Foam::operator-(const Matrix<Form, Type>& mat)
Form operator-(const Matrix<Form, Type>& mat)
{
Form result(mat.sizes());
@ -705,8 +715,9 @@ Form Foam::operator-(const Matrix<Form, Type>& mat)
}
//- Matrix addition. Returns Matrix of the same form as the first parameter.
template<class Form1, class Form2, class Type>
Form1 Foam::operator+
Form1 operator+
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& B
@ -738,8 +749,9 @@ Form1 Foam::operator+
}
//- Matrix subtraction. Returns Matrix of the same form as the first parameter.
template<class Form1, class Form2, class Type>
Form1 Foam::operator-
Form1 operator-
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& B
@ -771,8 +783,9 @@ Form1 Foam::operator-
}
//- Scalar multiplication of Matrix
template<class Form, class Type>
Form Foam::operator*(const Type& s, const Matrix<Form, Type>& mat)
Form operator*(const Type& s, const Matrix<Form, Type>& mat)
{
Form result(mat.sizes());
@ -788,8 +801,17 @@ Form Foam::operator*(const Type& s, const Matrix<Form, Type>& mat)
}
//- Scalar multiplication of Matrix
template<class Form, class Type>
Form Foam::operator+(const Type& s, const Matrix<Form, Type>& mat)
Form operator*(const Matrix<Form, Type>& mat, const Type& s)
{
return s*mat;
}
//- Scalar addition of Matrix
template<class Form, class Type>
Form operator+(const Type& s, const Matrix<Form, Type>& mat)
{
Form result(mat.sizes());
@ -805,8 +827,17 @@ Form Foam::operator+(const Type& s, const Matrix<Form, Type>& mat)
}
//- Scalar addition of Matrix
template<class Form, class Type>
Form Foam::operator-(const Type& s, const Matrix<Form, Type>& mat)
Form operator+(const Matrix<Form, Type>& mat, const Type& s)
{
return s + mat;
}
//- Scalar subtraction of Matrix
template<class Form, class Type>
Form operator-(const Type& s, const Matrix<Form, Type>& mat)
{
Form result(mat.sizes());
@ -822,22 +853,9 @@ Form Foam::operator-(const Type& s, const Matrix<Form, Type>& mat)
}
//- Scalar subtraction of Matrix
template<class Form, class Type>
Form Foam::operator*(const Matrix<Form, Type>& mat, const Type& s)
{
return s*mat;
}
template<class Form, class Type>
Form Foam::operator+(const Matrix<Form, Type>& mat, const Type& s)
{
return s + mat;
}
template<class Form, class Type>
Form Foam::operator-(const Matrix<Form, Type>& mat, const Type& s)
Form operator-(const Matrix<Form, Type>& mat, const Type& s)
{
Form result(mat.sizes());
@ -853,8 +871,9 @@ Form Foam::operator-(const Matrix<Form, Type>& mat, const Type& s)
}
//- Scalar division of Matrix
template<class Form, class Type>
Form Foam::operator/(const Matrix<Form, Type>& mat, const Type& s)
Form operator/(const Matrix<Form, Type>& mat, const Type& s)
{
Form result(mat.sizes());
@ -870,9 +889,10 @@ Form Foam::operator/(const Matrix<Form, Type>& mat, const Type& s)
}
//- Matrix-Matrix multiplication using ikj-algorithm
template<class Form1, class Form2, class Type>
typename Foam::typeOfInnerProduct<Type, Form1, Form2>::type
Foam::operator*
typename typeOfInnerProduct<Type, Form1, Form2>::type
operator*
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& B
@ -912,9 +932,10 @@ Foam::operator*
}
//- Implicit inner product of Matrix-Matrix, equivalent to A.T()*B
template<class Form1, class Form2, class Type>
typename Foam::typeOfInnerProduct<Type, Form1, Form2>::type
Foam::operator&
typename typeOfInnerProduct<Type, Form1, Form2>::type
operator&
(
const Matrix<Form1, Type>& AT,
const Matrix<Form2, Type>& B
@ -954,9 +975,10 @@ Foam::operator&
}
//- Implicit outer product of Matrix-Matrix, equivalent to A*B.T()
template<class Form1, class Form2, class Type>
typename Foam::typeOfInnerProduct<Type, Form1, Form2>::type
Foam::operator^
typename typeOfInnerProduct<Type, Form1, Form2>::type
operator^
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& BT
@ -996,7 +1018,11 @@ Foam::operator^
}
// * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
#include "MatrixIO.C"

View File

@ -220,7 +220,6 @@ public:
// Subscript checking only with FULLDEBUG
inline Type& at(const label idx);
// Block Access (const)
//- Return const column or column's subset of Matrix
@ -265,7 +264,6 @@ public:
const label colIndex
) const;
// Block Access (non-const)
//- Return column or column's subset of Matrix
@ -301,7 +299,6 @@ public:
const label colIndex
);
// Check
//- Check index i is within valid range [0, m)
@ -316,7 +313,6 @@ public:
//- True if all entries have identical values, and Matrix is non-empty
inline bool uniform() const;
// Edit
//- Clear Matrix, i.e. set sizes to zero
@ -342,10 +338,9 @@ public:
//- Resize Matrix without reallocating storage (unsafe)
inline void shallowResize(const label m, const label n);
//- Round to zero elements with magnitude smaller than tol (SMALL)
//- Round elements with magnitude smaller than tol (SMALL) to zero
void round(const scalar tol = SMALL);
// Operations
//- Return (conjugate) transpose of Matrix
@ -386,7 +381,6 @@ public:
//- Return Frobenius norm of Matrix
// Optional without sqrt for parallel usage.
// https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm
scalar norm(const bool noSqrt=false) const;
@ -596,159 +590,6 @@ template<class Form, class Type>
Ostream& operator<<(Ostream& os, const Matrix<Form, Type>& mat);
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
//- Find max value in Matrix
template<class Form, class Type>
const Type& max(const Matrix<Form, Type>& mat);
//- Find min value in Matrix
template<class Form, class Type>
const Type& min(const Matrix<Form, Type>& mat);
//- Find the min/max values of Matrix
template<class Form, class Type>
MinMax<Type> minMax(const Matrix<Form, Type>& mat);
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
//- Matrix negation
template<class Form, class Type>
Form operator-(const Matrix<Form, Type>& mat);
//- Matrix addition. Returns Matrix of the same form as the first parameter.
template<class Form1, class Form2, class Type>
Form1 operator+
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& B
);
//- Matrix subtraction. Returns Matrix of the same form as the first parameter.
template<class Form1, class Form2, class Type>
Form1 operator-
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& B
);
//- Scalar multiplication of Matrix
template<class Form, class Type>
Form operator*
(
const Type& s,
const Matrix<Form, Type>& mat
);
//- Scalar addition of Matrix
template<class Form, class Type>
Form operator+
(
const Type& s,
const Matrix<Form, Type>& mat
);
//- Scalar subtraction of Matrix
template<class Form, class Type>
Form operator-
(
const Type& s,
const Matrix<Form, Type>& mat
);
//- Scalar multiplication of Matrix
template<class Form, class Type>
Form operator*
(
const Matrix<Form, Type>& mat,
const Type& s
);
//- Scalar addition of Matrix
template<class Form, class Type>
Form operator+
(
const Matrix<Form, Type>& mat,
const Type& s
);
//- Scalar subtraction of Matrix
template<class Form, class Type>
Form operator-
(
const Matrix<Form, Type>& mat,
const Type& s
);
//- Scalar division of Matrix
template<class Form, class Type>
Form operator/
(
const Matrix<Form, Type>& mat,
const Type& s
);
//- Matrix-Matrix multiplication using ikj-algorithm
template<class Form1, class Form2, class Type>
typename typeOfInnerProduct<Type, Form1, Form2>::type
operator*
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& B
);
//- Matrix-vector multiplication (A * x), where x is a column vector
template<class Form, class Type>
inline tmp<Field<Type>> operator*
(
const Matrix<Form, Type>& mat,
const UList<Type>& x
);
//- Matrix-vector multiplication (A * x), where x is a column vector
template<class Form, class Type, class Addr>
inline tmp<Field<Type>> operator*
(
const Matrix<Form, Type>& mat,
const IndirectListBase<Type, Addr>& x
);
//- Vector-Matrix multiplication (x * A), where x is a row vector
template<class Form, class Type>
inline tmp<Field<Type>> operator*
(
const UList<Type>& x,
const Matrix<Form, Type>& mat
);
//- Vector-Matrix multiplication (x * A), where x is a row vector
template<class Form, class Type, class Addr>
inline tmp<Field<Type>> operator*
(
const IndirectListBase<Type, Addr>& x,
const Matrix<Form, Type>& mat
);
//- Implicit inner product of Matrix-Matrix, equivalent to A.T()*B
template<class Form1, class Form2, class Type>
typename typeOfInnerProduct<Type, Form1, Form2>::type
operator&
(
const Matrix<Form1, Type>& ATranspose,
const Matrix<Form2, Type>& B
);
//- Implicit outer product of Matrix-Matrix, equivalent to A*B.T()
template<class Form1, class Form2, class Type>
typename typeOfInnerProduct<Type, Form1, Form2>::type
operator^
(
const Matrix<Form1, Type>& A,
const Matrix<Form2, Type>& BTranspose
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -242,7 +242,7 @@ inline const Type& Foam::Matrix<Form, Type>::at(const label idx) const
<< abort(FatalError);
}
#endif
return (v_ + idx);
return *(v_ + idx);
}
@ -257,7 +257,7 @@ inline Type& Foam::Matrix<Form, Type>::at(const label idx)
<< abort(FatalError);
}
#endif
return (v_ + idx);
return *(v_ + idx);
}
@ -608,8 +608,16 @@ inline Type* Foam::Matrix<Form, Type>::operator[](const label irow)
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
//- Matrix-vector multiplication (A * x), where x is a column vector
template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
inline tmp<Field<Type>> operator*
(
const Matrix<Form, Type>& mat,
const UList<Type>& x
@ -619,8 +627,9 @@ inline Foam::tmp<Foam::Field<Type>> Foam::operator*
}
//- Matrix-vector multiplication (A * x), where x is a column vector
template<class Form, class Type, class Addr>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
inline tmp<Field<Type>> operator*
(
const Matrix<Form, Type>& mat,
const IndirectListBase<Type, Addr>& x
@ -630,8 +639,9 @@ inline Foam::tmp<Foam::Field<Type>> Foam::operator*
}
//- Vector-Matrix multiplication (x * A), where x is a row vector
template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
inline tmp<Field<Type>> operator*
(
const UList<Type>& x,
const Matrix<Form, Type>& mat
@ -641,8 +651,9 @@ inline Foam::tmp<Foam::Field<Type>> Foam::operator*
}
//- Vector-Matrix multiplication (x * A), where x is a row vector
template<class Form, class Type, class Addr>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
inline tmp<Field<Type>> operator*
(
const IndirectListBase<Type, Addr>& x,
const Matrix<Form, Type>& mat
@ -651,5 +662,8 @@ inline Foam::tmp<Foam::Field<Type>> Foam::operator*
return mat.Tmul(x);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //

View File

@ -176,7 +176,7 @@ void Foam::MatrixBlock<MatrixType>::operator=
const ConstMatrixBlock<MatrixType>& Mb
)
{
if (this != &Mb)
if (reinterpret_cast<const ConstMatrixBlock<MatrixType>*>(this) != &Mb)
{
if (mRows_ != Mb.m() || nCols_ != Mb.n())
{
@ -233,7 +233,7 @@ void Foam::MatrixBlock<MatrixType>::operator=
const ConstMatrixBlock<MatrixType2>& Mb
)
{
if (this != &Mb)
if (reinterpret_cast<const ConstMatrixBlock<MatrixType2>*>(this) != &Mb)
{
if (mRows_ != Mb.m() || nCols_ != Mb.n())
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,13 +28,14 @@ Class
Foam::RectangularMatrix
Description
A templated 2D rectangular m x n matrix of objects of \<Type\>.
A templated (M x N) rectangular matrix of objects of \<Type\>,
containing M*N elements, derived from Matrix.
The matrix dimensions are used for subscript bounds checking etc.
See also
Test-RectangularMatrix.C
SourceFiles
RectangularMatrixI.H
RectangularMatrix.C
\*---------------------------------------------------------------------------*/
@ -61,10 +62,19 @@ class RectangularMatrix
public:
// Constructors
// Generated Methods
//- Construct null
inline RectangularMatrix();
//- Default construct
RectangularMatrix() = default;
//- Copy construct
RectangularMatrix(const RectangularMatrix&) = default;
//- Copy assignment
RectangularMatrix& operator=(const RectangularMatrix&) = default;
// Constructors
//- Construct a square matrix (rows == columns)
inline explicit RectangularMatrix(const label n);
@ -85,15 +95,15 @@ public:
template<class AnyType>
inline RectangularMatrix(const labelPair& dims, const Identity<AnyType>);
//- Construct given number of rows/columns
//- Construct given number of rows/columns by using a label pair
inline explicit RectangularMatrix(const labelPair& dims);
//- Construct given number of rows/columns
//- initializing all elements to zero
//- Construct given number of rows/columns by using a label pair
//- and initializing all elements to zero
inline RectangularMatrix(const labelPair& dims, const zero);
//- Construct given number of rows/columns
//- initializing all elements to the given value
//- Construct given number of rows/columns by using a label pair
//- and initializing all elements to the given value
inline RectangularMatrix(const labelPair& dims, const Type& val);
//- Construct from a block of another matrix
@ -107,7 +117,7 @@ public:
//- Construct as copy of a square matrix
inline RectangularMatrix(const SquareMatrix<Type>& mat);
//- Construct from Istream.
//- Construct from Istream
inline explicit RectangularMatrix(Istream& is);
//- Clone
@ -124,41 +134,6 @@ public:
};
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class Type>
class typeOfInnerProduct<Type, RectangularMatrix<Type>, RectangularMatrix<Type>>
{
public:
typedef RectangularMatrix<Type> type;
};
template<class Type>
class typeOfInnerProduct<Type, RectangularMatrix<Type>, SquareMatrix<Type>>
{
public:
typedef RectangularMatrix<Type> type;
};
template<class Type>
class typeOfInnerProduct<Type, SquareMatrix<Type>, RectangularMatrix<Type>>
{
public:
typedef RectangularMatrix<Type> type;
};
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
template<class Type>
RectangularMatrix<Type> outer(const Field<Type>& f1, const Field<Type>& f2);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,13 +28,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::RectangularMatrix<Type>::RectangularMatrix()
:
Matrix<RectangularMatrix<Type>, Type>()
{}
template<class Type>
inline Foam::RectangularMatrix<Type>::RectangularMatrix
(
@ -197,8 +190,38 @@ inline void Foam::RectangularMatrix<Type>::operator=(const Type& val)
namespace Foam
{
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class Type>
class typeOfInnerProduct<Type, RectangularMatrix<Type>, RectangularMatrix<Type>>
{
public:
typedef RectangularMatrix<Type> type;
};
template<class Type>
class typeOfInnerProduct<Type, RectangularMatrix<Type>, SquareMatrix<Type>>
{
public:
typedef RectangularMatrix<Type> type;
};
template<class Type>
class typeOfInnerProduct<Type, SquareMatrix<Type>, RectangularMatrix<Type>>
{
public:
typedef RectangularMatrix<Type> type;
};
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
// Return the outer product of Field-Field as RectangularMatrix
template<class Type>
inline Foam::RectangularMatrix<Type> outer
(
@ -224,5 +247,4 @@ inline Foam::RectangularMatrix<Type> outer
} // End namespace Foam
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2013-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -30,10 +30,80 @@ License
#include "RectangularMatrix.H"
#include "labelList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Foam::scalar Foam::detDecomposed
template<class CompOp>
Foam::List<Foam::label> Foam::SquareMatrix<Type>::sortPermutation
(
CompOp& compare
) const
{
List<label> p(this->m());
std::iota(p.begin(), p.end(), 0);
std::sort
(
p.begin(),
p.end(),
[&](label i, label j){ return compare((*this)(i,i), (*this)(j,j)); }
);
return p;
}
template<class Type>
void Foam::SquareMatrix<Type>::applyPermutation(const List<label>& p)
{
#ifdef FULLDEBUG
if (this->m() != p.size())
{
FatalErrorInFunction
<< "Attempt to column-reorder according to an uneven list: " << nl
<< "SquareMatrix diagonal size = " << this->m() << nl
<< "Permutation list size = " << p.size() << nl
<< abort(FatalError);
}
#endif
SquareMatrix<Type> reordered(this->sizes());
label j = 0;
for (const label i : p)
{
reordered.subColumn(j) = this->subColumn(i);
++j;
}
this->transfer(reordered);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
template<class AnyType>
void Foam::SquareMatrix<Type>::operator=(const Identity<AnyType>)
{
Matrix<SquareMatrix<Type>, Type>::operator=(Zero);
for (label i = 0; i < this->n(); ++i)
{
this->operator()(i, i) = pTraits<Type>::one;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- Return the determinant of the LU decomposed SquareMatrix
template<class Type>
scalar detDecomposed
(
const SquareMatrix<Type>& matrix,
const label sign
@ -50,8 +120,9 @@ Foam::scalar Foam::detDecomposed
}
//- Return the determinant of SquareMatrix
template<class Type>
Foam::scalar Foam::det(const SquareMatrix<Type>& matrix)
scalar det(const SquareMatrix<Type>& matrix)
{
SquareMatrix<Type> matrixTmp = matrix;
@ -63,8 +134,9 @@ Foam::scalar Foam::det(const SquareMatrix<Type>& matrix)
}
//- Return the SquareMatrix det and the LU decomposition in the original matrix
template<class Type>
Foam::scalar Foam::det(SquareMatrix<Type>& matrix)
scalar det(SquareMatrix<Type>& matrix)
{
labelList pivotIndices(matrix.m());
label sign;
@ -74,19 +146,52 @@ Foam::scalar Foam::det(SquareMatrix<Type>& matrix)
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
//- Return Matrix column-reordered according to
//- a given permutation labelList
template<class Type>
template<class AnyType>
void Foam::SquareMatrix<Type>::operator=(const Identity<AnyType>)
SquareMatrix<Type> applyPermutation
(
const SquareMatrix<Type>& mat,
const List<label>& p
)
{
Matrix<SquareMatrix<Type>, Type>::operator=(Zero);
for (label i=0; i < this->n(); ++i)
#ifdef FULLDEBUG
if (mat.m() != p.size())
{
this->operator()(i, i) = pTraits<Type>::one;
FatalErrorInFunction
<< "Attempt to column-reorder according to an uneven list: " << nl
<< "SquareMatrix diagonal size = " << mat.m() << nl
<< "Permutation list size = " << p.size() << nl
<< abort(FatalError);
}
#endif
SquareMatrix<Type> reordered(mat.sizes());
label j = 0;
for (const label i : p)
{
reordered.subColumn(j) = mat.subColumn(i);
++j;
}
return reordered;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
class typeOfInnerProduct<Type, SquareMatrix<Type>, SquareMatrix<Type>>
{
public:
typedef SquareMatrix<Type> type;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,8 +28,11 @@ Class
Foam::SquareMatrix
Description
A templated 2D square matrix of objects of \<T\>, where the n x n matrix
dimension is known and used for subscript bounds checking, etc.
A templated (N x N) square matrix of objects of \<Type\>,
containing N*N elements, derived from Matrix.
See also
Test-SquareMatrix.C
SourceFiles
SquareMatrixI.H
@ -42,6 +45,7 @@ SourceFiles
#include "Matrix.H"
#include "Identity.H"
#include <numeric>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -64,10 +68,19 @@ class SquareMatrix
public:
// Constructors
// Generated Methods
//- Construct null
inline SquareMatrix();
//- Default construct
SquareMatrix() = default;
//- Copy construct
SquareMatrix(const SquareMatrix&) = default;
//- Copy assignment
SquareMatrix& operator=(const SquareMatrix&) = default;
// Constructors
//- Construct for given size (rows == cols)
inline explicit SquareMatrix(const label n);
@ -85,20 +98,25 @@ public:
template<class AnyType>
inline SquareMatrix(const label n, const Identity<AnyType>);
//- Construct for given size (rows == cols) by using a labelPair
//- initializing to the identity matrix
template<class AnyType>
inline SquareMatrix(const labelPair& dims, const Identity<AnyType>);
//- Construct given number of rows/columns (checked to be equal)
//- Construct given number of rows/columns
//- by using a labelPair (checked to be equal)
// For constructor consistency in rectangular matrices
inline explicit SquareMatrix(const labelPair& dims);
//- Construct given number of rows/columns (checked to be equal)
//- initializing all elements to zero
//- Construct given number of rows/columns
//- by using a labelPair (checked to be equal)
//- and initializing all elements to zero
// For constructor consistency with rectangular matrices
inline SquareMatrix(const labelPair& dims, const zero);
//- Construct given number of rows/columns (checked to be equal)
//- initializing all elements to the given value
//- Construct given number of rows/columns
//- by using a labelPair (checked to be equal)
//- and initializing all elements to the given value
// For constructor consistency with rectangular matrices
inline SquareMatrix(const labelPair& dims, const Type& val);
@ -106,7 +124,7 @@ public:
//- initializing all elements to zero
inline SquareMatrix(const label m, const label n, const zero);
//- Construct from sub-matrix block
//- Construct from const sub-matrix block
template<class MatrixType>
inline SquareMatrix(const ConstMatrixBlock<MatrixType>& mat);
@ -149,6 +167,17 @@ public:
//- Return true if the square matrix is reduced tridiagonal
inline bool tridiagonal() const;
// Sort
//- Return a sort permutation labelList according to
//- a given comparison on the diagonal entries
template<class CompOp>
List<label> sortPermutation(CompOp& compare) const;
//- Column-reorder this Matrix according to
//- a given permutation labelList
void applyPermutation(const List<label>& p);
// Member Operators
@ -164,28 +193,6 @@ public:
};
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- Return the LU decomposed SquareMatrix det
template<class Type>
scalar detDecomposed(const SquareMatrix<Type>&, const label sign);
//- Return the SquareMatrix det
template<class Type>
scalar det(const SquareMatrix<Type>&);
//- Return the SquareMatrix det and the LU decomposition in the original matrix
template<class Type>
scalar det(SquareMatrix<Type>&);
template<class Type>
class typeOfInnerProduct<Type, SquareMatrix<Type>, SquareMatrix<Type>>
{
public:
typedef SquareMatrix<Type> type;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,13 +37,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix()
:
Matrix<SquareMatrix<Type>, Type>()
{}
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix(const label n)
:
@ -83,7 +76,7 @@ inline Foam::SquareMatrix<Type>::SquareMatrix
:
Matrix<SquareMatrix<Type>, Type>(n, n, Zero)
{
for (label i=0; i < n; ++i)
for (label i = 0; i < n; ++i)
{
this->operator()(i, i) = pTraits<Type>::one;
}
@ -318,6 +311,7 @@ namespace Foam
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
// Return the outer product of Field-Field as SquareMatrix
template<class Type>
inline Foam::SquareMatrix<Type> symmOuter
(

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -126,4 +126,4 @@ void Foam::SymmetricSquareMatrix<Type>::operator=(const Identity<AnyType>)
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,8 +28,11 @@ Class
Foam::SymmetricSquareMatrix
Description
A templated 2D square symmetric matrix of objects of \<T\>, where the
n x n matrix dimension is known and used for subscript bounds checking, etc.
A templated (N x N) square matrix of objects of \<Type\>,
containing N*N elements, derived from Matrix.
See also
Test-SymmetricSquareMatrix.C
SourceFiles
SymmetricSquareMatrixI.H
@ -59,10 +62,19 @@ class SymmetricSquareMatrix
public:
// Constructors
// Generated Methods
//- Construct null
inline SymmetricSquareMatrix();
//- Default construct
SymmetricSquareMatrix() = default;
//- Copy construct
SymmetricSquareMatrix(const SymmetricSquareMatrix&) = default;
//- Copy assignment
SymmetricSquareMatrix& operator=(const SymmetricSquareMatrix&) = default;
// Constructors
//- Construct for given size (rows == cols)
inline explicit SymmetricSquareMatrix(const label n);
@ -76,7 +88,7 @@ public:
inline SymmetricSquareMatrix(const label n, const Type& val);
//- Construct for given size (rows == cols)
//- setting the diagonal to one and off-diagonals to zero
//- initializing to the identity matrix
template<class AnyType>
inline SymmetricSquareMatrix(const label n, const Identity<AnyType>);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,13 +37,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::SymmetricSquareMatrix<Type>::SymmetricSquareMatrix()
:
Matrix<SymmetricSquareMatrix<Type>, Type>()
{}
template<class Type>
inline Foam::SymmetricSquareMatrix<Type>::SymmetricSquareMatrix(const label n)
: