34 template<
class Form,
class Type>
35 template<
class ListType>
44 if (mat.
n() !=
x.size())
47 <<
"Attempt to multiply incompatible Matrix and Vector:" <<
nl 48 <<
"Matrix : (" << mat.
m() <<
", " << mat.
n() <<
')' <<
nl 49 <<
"Matrix columns != Vector size (" <<
x.size() <<
')' <<
nl 55 auto& result = tresult.ref();
57 for (label i = 0; i < mat.
m(); ++i)
59 for (label j = 0; j < mat.
n(); ++j)
61 result[i] += mat(i, j)*
x[j];
69 template<
class Form,
class Type>
70 template<
class ListType>
79 if (mat.
m() !=
x.size())
82 <<
"Attempt to multiply incompatible Matrix and Vector:" <<
nl 83 <<
"Matrix : (" << mat.
m() <<
", " << mat.
n() <<
')' <<
nl 84 <<
"Matrix rows != Vector size (" <<
x.size() <<
')' <<
nl 89 auto tresult = tmp<Field<Type>>
::New(mat.
n(),
Zero);
90 auto& result = tresult.ref();
92 for (label i = 0; i < mat.
m(); ++i)
94 const Type& val =
x[i];
95 for (label j = 0; j < mat.
n(); ++j)
97 result[j] += val*mat(i, j);
107 template<
class Form,
class Type>
120 template<
class Form,
class Type>
135 template<
class Form,
class Type>
150 template<
class Form,
class Type>
166 template<
class Form,
class Type>
179 template<
class Form,
class Type>
180 template<
class Form2>
196 template<
class Form,
class Type>
197 template<
class MatrixType>
200 const ConstMatrixBlock<MatrixType>& Mb
208 for (label i = 0; i < mRows_; ++i)
210 for (label j = 0; j < nCols_; ++j)
212 (*this)(i, j) = Mb(i,j);
218 template<
class Form,
class Type>
219 template<
class MatrixType>
222 const MatrixBlock<MatrixType>& Mb
230 for (label i = 0; i < mRows_; ++i)
232 for (label j = 0; j < nCols_; ++j)
234 (*this)(i, j) = Mb(i, j);
242 template<
class Form,
class Type>
254 template<
class Form,
class Type>
268 template<
class Form,
class Type>
273 const label len = size();
288 template<
class Form,
class Type>
296 std::swap(mRows_, mat.mRows_);
297 std::swap(nCols_, mat.nCols_);
298 std::swap(v_, mat.v_);
302 template<
class Form,
class Type>
322 template<
class Form,
class Type>
325 if (m == mRows_ &&
n == nCols_)
330 Matrix<Form, Type> newMatrix(m,
n,
Zero);
332 const label mrow =
min(m, mRows_);
333 const label ncol =
min(
n, nCols_);
335 for (label i = 0; i < mrow; ++i)
337 for (label j = 0; j < ncol; ++j)
339 newMatrix(i, j) = (*this)(i, j);
347 template<
class Form,
class Type>
350 if (mrow == mRows_ && ncol == nCols_)
355 const label oldLen = (mRows_ * nCols_);
357 const label newLen = (mrow * ncol);
359 if (oldLen == newLen)
377 template<
class Form,
class Type>
380 for (Type& val : *
this)
390 template<
class Form,
class Type>
395 for (label i = 0; i < m(); ++i)
397 for (label j = 0; j <
n(); ++j)
407 template<
class Form,
class Type>
412 for (label i = 0; i < m(); ++i)
414 for (label j = 0; j <
n(); ++j)
416 At(j, i) = (*this)(i, j);
424 template<
class Form,
class Type>
427 const label len =
Foam::min(mRows_, nCols_);
429 List<Type> result(len);
431 for (label i=0; i < len; ++i)
433 result[i] = (*this)(i, i);
440 template<
class Form,
class Type>
443 const label len =
Foam::min(mRows_, nCols_);
446 if (list.size() != len)
449 <<
"List size (" << list.size()
454 for (label i=0; i < len; ++i)
456 (*this)(i, i) = list[i];
461 template<
class Form,
class Type>
464 const label len =
Foam::min(mRows_, nCols_);
468 for (label i=0; i < len; ++i)
470 val += (*this)(i, i);
477 template<
class Form,
class Type>
480 const label colIndex,
484 scalar result =
Zero;
486 for (label i=0; i < mRows_; ++i)
488 result +=
magSqr((*
this)(i, colIndex));
495 template<
class Form,
class Type>
498 scalar result =
Zero;
500 for (
const Type& val : *
this)
509 template<
class Form,
class Type>
512 if (!is_contiguous<Type>::value)
515 <<
"Invalid for non-contiguous data types" 518 return this->size_bytes();
524 template<
class Form,
class Type>
532 if (mRows_ != mat.mRows_ || nCols_ != mat.nCols_)
547 template<
class Form,
class Type>
558 template<
class Form,
class Type>
559 template<
class MatrixType>
562 const ConstMatrixBlock<MatrixType>& Mb
565 for (label i = 0; i < mRows_; ++i)
567 for (label j = 0; j < nCols_; ++j)
569 (*this)(i, j) = Mb(i, j);
575 template<
class Form,
class Type>
576 template<
class MatrixType>
579 const MatrixBlock<MatrixType>& Mb
582 for (label i = 0; i < mRows_; ++i)
584 for (label j = 0; j < nCols_; ++j)
586 (*this)(i, j) = Mb(i, j);
592 template<
class Form,
class Type>
599 template<
class Form,
class Type>
606 template<
class Form,
class Type>
613 <<
"Attempted addition to self" 617 if (m() != other.
m() ||
n() != other.
n())
620 <<
"Attempt to add matrices with different sizes: (" 621 << m() <<
", " <<
n() <<
") != (" 622 << other.
m() <<
", " << other.
n() <<
')' <<
nl 627 auto iter2 = other.
cbegin();
628 for (Type& val : *
this)
636 template<
class Form,
class Type>
643 <<
"Attempted subtraction from self" 647 if (m() != other.m() ||
n() != other.n())
650 <<
"Attempt to subtract matrices with different sizes: (" 651 << m() <<
", " <<
n() <<
") != (" 652 << other.m() <<
", " << other.n() <<
')' <<
nl 657 auto iter2 = other.cbegin();
658 for (Type& val : *
this)
666 template<
class Form,
class Type>
669 for (Type& val : *
this)
676 template<
class Form,
class Type>
679 for (Type& val : *
this)
686 template<
class Form,
class Type>
689 for (Type& val : *
this)
696 template<
class Form,
class Type>
699 for (Type& val : *
this)
714 template<
class Form,
class Type>
723 return *(std::max_element(mat.
cbegin(), mat.
cend()));
728 template<
class Form,
class Type>
737 return *(std::min_element(mat.
cbegin(), mat.
cend()));
742 template<
class Form,
class Type>
747 for (
const Type& val : mat)
760 template<
class Form,
class Type>
761 Form
operator-(
const Matrix<Form, Type>& mat)
763 Form result(mat.sizes());
778 template<
class Form1,
class Form2,
class Type>
781 const Matrix<Form1, Type>&
A,
782 const Matrix<Form2, Type>&
B 786 if (
A.m() !=
B.m() ||
A.n() !=
B.n())
789 <<
"Attempt to add matrices with different sizes: (" 790 <<
A.m() <<
", " <<
A.n() <<
") != (" 791 <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 796 Form1 result(
A.sizes());
812 template<
class Form1,
class Form2,
class Type>
815 const Matrix<Form1, Type>&
A,
816 const Matrix<Form2, Type>&
B 820 if (
A.m() !=
B.m() ||
A.n() !=
B.n())
823 <<
"Attempt to subtract matrices with different sizes: (" 824 <<
A.m() <<
", " <<
A.n() <<
") != (" 825 <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 830 Form1 result(
A.sizes());
846 template<
class Form,
class Type>
847 Form
operator*(
const Type&
s,
const Matrix<Form, Type>& mat)
849 Form result(mat.sizes());
856 [&](
const Type& val) {
return s * val; }
864 template<
class Form,
class Type>
865 Form
operator*(
const Matrix<Form, Type>& mat,
const Type&
s)
872 template<
class Form,
class Type>
873 Form
operator+(
const Type&
s,
const Matrix<Form, Type>& mat)
875 Form result(mat.sizes());
882 [&](
const Type& val) {
return s + val; }
890 template<
class Form,
class Type>
891 Form
operator+(
const Matrix<Form, Type>& mat,
const Type&
s)
898 template<
class Form,
class Type>
899 Form
operator-(
const Type&
s,
const Matrix<Form, Type>& mat)
901 Form result(mat.sizes());
908 [&](
const Type& val) {
return s - val; }
916 template<
class Form,
class Type>
919 Form result(mat.
sizes());
926 [&](
const Type& val) {
return val -
s; }
934 template<
class Form,
class Type>
937 Form result(mat.
sizes());
944 [&](
const Type& val) {
return val /
s; }
952 template<
class Form1,
class Form2,
class Type>
964 <<
"Attempt to multiply incompatible matrices:" <<
nl 965 <<
"Matrix A : (" <<
A.m() <<
", " <<
A.n() <<
')' <<
nl 966 <<
"Matrix B : (" <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 967 <<
"The columns of A must equal rows of B" 979 for (label i = 0; i < AB.m(); ++i)
981 for (label
k = 0;
k <
B.m(); ++
k)
983 for (label j = 0; j < AB.n(); ++j)
985 AB(i, j) +=
A(i,
k)*
B(
k, j);
995 template<
class Form1,
class Form2,
class Type>
999 const Matrix<Form1, Type>& AT,
1000 const Matrix<Form2, Type>&
B 1004 if (AT.m() !=
B.m())
1007 <<
"Attempt to multiply incompatible matrices:" <<
nl 1008 <<
"Matrix A : (" << AT.m() <<
", " << AT.n() <<
')' <<
nl 1009 <<
"Matrix B : (" <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 1010 <<
"The rows of A must equal rows of B" 1022 for (label
k = 0;
k <
B.m(); ++
k)
1024 for (label i = 0; i < AB.m(); ++i)
1026 for (label j = 0; j < AB.n(); ++j)
1038 template<
class Form1,
class Form2,
class Type>
1042 const Matrix<Form1, Type>&
A,
1043 const Matrix<Form2, Type>& BT
1047 if (
A.n() != BT.n())
1050 <<
"Attempt to multiply incompatible matrices:" <<
nl 1051 <<
"Matrix A : (" <<
A.m() <<
", " <<
A.n() <<
')' <<
nl 1052 <<
"Matrix B : (" << BT.m() <<
", " << BT.n() <<
')' <<
nl 1053 <<
"The columns of A must equal columns of B" 1065 for (label i = 0; i < AB.m(); ++i)
1067 for (label j = 0; j < AB.n(); ++j)
1069 for (label
k = 0;
k < BT.n(); ++
k)
const Type & min(const Matrix< Form, Type > &mat)
Find min value in Matrix.
Abstract template class to provide the form resulting from the inner-product of two forms...
void resize(const label m, const label n)
Change Matrix dimensions, preserving the elements.
std::enable_if< !std::is_same< complex, T >::value, const T &>::type conj(const T &val)
The 'conjugate' of non-complex returns itself (pass-through) it does not return a complex! ...
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Form T() const
Return conjugate transpose of Matrix.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
constexpr char nl
The newline '\n' character (0x0a)
A min/max value pair with additional methods. In addition to conveniently storing values...
scalar columnNorm(const label colIndex, const bool noSqrt=false) const
Return L2-Norm of chosen column.
label n() const noexcept
The number of columns.
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
label k
Boltzmann constant.
List< Type > release()
Release storage management of Matrix contents by transferring management to a List.
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
void transfer(Matrix< Form, Type > &mat)
Transfer the contents of the argument Matrix into this Matrix and annul the argument Matrix...
bool empty() const noexcept
Return true if Matrix is empty (i.e., size() is zero)
void operator+=(const Matrix< Form, Type > &other)
Matrix addition.
void operator-=(const Matrix< Form, Type > &other)
Matrix subtraction.
void swap(UList< T > &list)
Swap content with another UList of the same type in constant time.
MinMax< label > minMax(const labelHashSet &set)
Find the min/max values of labelHashSet.
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
Type trace() const
Return the trace.
void operator*=(const Type &s)
Matrix scalar multiplication.
List< Type > diag() const
Extract the diagonal elements. Method may change in the future.
labelPair sizes() const
Return row/column sizes.
iterator end() noexcept
Return an iterator to end traversing a Matrix.
scalar norm(const bool noSqrt=false) const
Return Frobenius norm of Matrix.
void clear()
Clear Matrix, i.e. set sizes to zero.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
errorManip< error > abort(error &err)
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
const Type * cdata() const noexcept
Return const pointer to the first data element, which can also be used to address into Matrix content...
label m() const noexcept
The number of rows.
void swap(Matrix< Form, Type > &mat)
Swap contents.
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order: ...
Pair< label > labelPair
A pair of labels.
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
std::streamsize byteSize() const
Number of contiguous bytes for the Matrix data, runtime FatalError if type is not contiguous...
const_iterator cend() const noexcept
Return const_iterator to end traversing a constant Matrix.
Form transpose() const
Return non-conjugate transpose of Matrix.
void operator/=(const Type &s)
Matrix scalar division.
Matrix() noexcept
Default construct (empty matrix)
void round(const scalar tol=SMALL)
Round elements with magnitude smaller than tol (SMALL) to zero.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing a constant Matrix.
void resize_nocopy(const label mrow, const label ncol)
Change Matrix dimensions without preserving existing content.
A class for managing temporary objects.
void operator=(const Matrix< Form, Type > &mat)
Copy assignment. Takes linear time.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
static const Foam::dimensionedScalar B("", Foam::dimless, 18.678)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
static constexpr const zero Zero
Global zero (0)