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>
146 std::fill_n(
begin(), size(), val);
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
209 for (label i = 0; i < mRows_; ++i)
211 for (label j = 0; j < nCols_; ++j)
213 (*this)(i, j) = Mb(i,j);
219 template<
class Form,
class Type>
220 template<
class MatrixType>
223 const MatrixBlock<MatrixType>& Mb
232 for (label i = 0; i < mRows_; ++i)
234 for (label j = 0; j < nCols_; ++j)
236 (*this)(i, j) = Mb(i, j);
244 template<
class Form,
class Type>
253 template<
class Form,
class Type>
267 template<
class Form,
class Type>
272 const label len = size();
287 template<
class Form,
class Type>
295 std::swap(mRows_, mat.mRows_);
296 std::swap(nCols_, mat.nCols_);
297 std::swap(v_, mat.v_);
301 template<
class Form,
class Type>
321 template<
class Form,
class Type>
324 if (m == mRows_ &&
n == nCols_)
329 Matrix<Form, Type> newMatrix(m,
n,
Zero);
331 const label mrow =
min(m, mRows_);
332 const label ncol =
min(
n, nCols_);
334 for (label i = 0; i < mrow; ++i)
336 for (label j = 0; j < ncol; ++j)
338 newMatrix(i, j) = (*this)(i, j);
346 template<
class Form,
class Type>
349 if (mrow == mRows_ && ncol == nCols_)
354 const label oldLen = (mRows_ * nCols_);
356 const label newLen = (mrow * ncol);
358 if (oldLen == newLen)
376 template<
class Form,
class Type>
379 for (Type& val : *
this)
389 template<
class Form,
class Type>
394 for (label i = 0; i < m(); ++i)
396 for (label j = 0; j <
n(); ++j)
406 template<
class Form,
class Type>
411 for (label i = 0; i < m(); ++i)
413 for (label j = 0; j <
n(); ++j)
415 At(j, i) = (*this)(i, j);
423 template<
class Form,
class Type>
426 const label len =
Foam::min(mRows_, nCols_);
428 List<Type> result(len);
430 for (label i=0; i < len; ++i)
432 result[i] = (*this)(i, i);
439 template<
class Form,
class Type>
442 const label len =
Foam::min(mRows_, nCols_);
445 if (list.size() != len)
448 <<
"List size (" << list.size()
453 for (label i=0; i < len; ++i)
455 (*this)(i, i) = list[i];
460 template<
class Form,
class Type>
463 const label len =
Foam::min(mRows_, nCols_);
467 for (label i=0; i < len; ++i)
469 val += (*this)(i, i);
476 template<
class Form,
class Type>
479 const label colIndex,
483 scalar result =
Zero;
485 for (label i=0; i < mRows_; ++i)
487 result +=
magSqr((*
this)(i, colIndex));
494 template<
class Form,
class Type>
497 scalar result =
Zero;
499 for (
const Type& val : *
this)
508 template<
class Form,
class Type>
511 if (!is_contiguous<Type>::value)
514 <<
"Invalid for non-contiguous data types" 517 return this->size_bytes();
523 template<
class Form,
class Type>
531 if (mRows_ != mat.mRows_ || nCols_ != mat.nCols_)
546 template<
class Form,
class Type>
557 template<
class Form,
class Type>
558 template<
class MatrixType>
561 const ConstMatrixBlock<MatrixType>& Mb
564 for (label i = 0; i < mRows_; ++i)
566 for (label j = 0; j < nCols_; ++j)
568 (*this)(i, j) = Mb(i, j);
574 template<
class Form,
class Type>
575 template<
class MatrixType>
578 const MatrixBlock<MatrixType>& Mb
581 for (label i = 0; i < mRows_; ++i)
583 for (label j = 0; j < nCols_; ++j)
585 (*this)(i, j) = Mb(i, j);
591 template<
class Form,
class Type>
594 std::fill_n(
begin(), size(), val);
598 template<
class Form,
class Type>
605 template<
class Form,
class Type>
612 <<
"Attempted addition to self" 616 if (m() != other.
m() ||
n() != other.
n())
619 <<
"Attempt to add matrices with different sizes: (" 620 << m() <<
", " <<
n() <<
") != (" 621 << other.
m() <<
", " << other.
n() <<
')' <<
nl 626 auto iter2 = other.
cbegin();
627 for (Type& val : *
this)
635 template<
class Form,
class Type>
642 <<
"Attempted subtraction from self" 646 if (m() != other.m() ||
n() != other.n())
649 <<
"Attempt to subtract matrices with different sizes: (" 650 << m() <<
", " <<
n() <<
") != (" 651 << other.m() <<
", " << other.n() <<
')' <<
nl 656 auto iter2 = other.cbegin();
657 for (Type& val : *
this)
665 template<
class Form,
class Type>
668 for (Type& val : *
this)
675 template<
class Form,
class Type>
678 for (Type& val : *
this)
685 template<
class Form,
class Type>
688 for (Type& val : *
this)
695 template<
class Form,
class Type>
698 for (Type& val : *
this)
713 template<
class Form,
class Type>
722 return *(std::max_element(mat.
cbegin(), mat.
cend()));
727 template<
class Form,
class Type>
736 return *(std::min_element(mat.
cbegin(), mat.
cend()));
741 template<
class Form,
class Type>
746 for (
const Type& val : mat)
759 template<
class Form,
class Type>
760 Form
operator-(
const Matrix<Form, Type>& mat)
762 Form result(mat.sizes());
777 template<
class Form1,
class Form2,
class Type>
780 const Matrix<Form1, Type>&
A,
781 const Matrix<Form2, Type>&
B 785 if (
A.m() !=
B.m() ||
A.n() !=
B.n())
788 <<
"Attempt to add matrices with different sizes: (" 789 <<
A.m() <<
", " <<
A.n() <<
") != (" 790 <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 795 Form1 result(
A.sizes());
811 template<
class Form1,
class Form2,
class Type>
814 const Matrix<Form1, Type>&
A,
815 const Matrix<Form2, Type>&
B 819 if (
A.m() !=
B.m() ||
A.n() !=
B.n())
822 <<
"Attempt to subtract matrices with different sizes: (" 823 <<
A.m() <<
", " <<
A.n() <<
") != (" 824 <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 829 Form1 result(
A.sizes());
845 template<
class Form,
class Type>
846 Form
operator*(
const Type&
s,
const Matrix<Form, Type>& mat)
848 Form result(mat.sizes());
855 [&](
const Type& val) {
return s * val; }
863 template<
class Form,
class Type>
864 Form
operator*(
const Matrix<Form, Type>& mat,
const Type&
s)
871 template<
class Form,
class Type>
872 Form
operator+(
const Type&
s,
const Matrix<Form, Type>& mat)
874 Form result(mat.sizes());
881 [&](
const Type& val) {
return s + val; }
889 template<
class Form,
class Type>
890 Form
operator+(
const Matrix<Form, Type>& mat,
const Type&
s)
897 template<
class Form,
class Type>
898 Form
operator-(
const Type&
s,
const Matrix<Form, Type>& mat)
900 Form result(mat.sizes());
907 [&](
const Type& val) {
return s - val; }
915 template<
class Form,
class Type>
918 Form result(mat.
sizes());
925 [&](
const Type& val) {
return val -
s; }
933 template<
class Form,
class Type>
936 Form result(mat.
sizes());
943 [&](
const Type& val) {
return val /
s; }
951 template<
class Form1,
class Form2,
class Type>
963 <<
"Attempt to multiply incompatible matrices:" <<
nl 964 <<
"Matrix A : (" <<
A.m() <<
", " <<
A.n() <<
')' <<
nl 965 <<
"Matrix B : (" <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 966 <<
"The columns of A must equal rows of B" 978 for (label i = 0; i < AB.m(); ++i)
980 for (label
k = 0;
k <
B.m(); ++
k)
982 for (label j = 0; j < AB.n(); ++j)
984 AB(i, j) +=
A(i,
k)*
B(
k, j);
994 template<
class Form1,
class Form2,
class Type>
998 const Matrix<Form1, Type>& AT,
999 const Matrix<Form2, Type>&
B 1003 if (AT.m() !=
B.m())
1006 <<
"Attempt to multiply incompatible matrices:" <<
nl 1007 <<
"Matrix A : (" << AT.m() <<
", " << AT.n() <<
')' <<
nl 1008 <<
"Matrix B : (" <<
B.m() <<
", " <<
B.n() <<
')' <<
nl 1009 <<
"The rows of A must equal rows of B" 1021 for (label
k = 0;
k <
B.m(); ++
k)
1023 for (label i = 0; i < AB.m(); ++i)
1025 for (label j = 0; j < AB.n(); ++j)
1037 template<
class Form1,
class Form2,
class Type>
1041 const Matrix<Form1, Type>&
A,
1042 const Matrix<Form2, Type>& BT
1046 if (
A.n() != BT.n())
1049 <<
"Attempt to multiply incompatible matrices:" <<
nl 1050 <<
"Matrix A : (" <<
A.m() <<
", " <<
A.n() <<
')' <<
nl 1051 <<
"Matrix B : (" << BT.m() <<
", " << BT.n() <<
')' <<
nl 1052 <<
"The columns of A must equal columns of B" 1064 for (label i = 0; i < AB.m(); ++i)
1066 for (label j = 0; j < AB.n(); ++j)
1068 for (label
k = 0;
k < BT.n(); ++
k)
const Type & min(const Matrix< Form, Type > &mat)
Find min value in Matrix.
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
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.
dimensionedScalar sqrt(const dimensionedScalar &ds)
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
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< 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.
MinMax< label > minMax(const labelHashSet &set)
Find the min/max values of labelHashSet.
label n() const noexcept
The number of columns.
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...
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.
label m() const noexcept
The number of rows.
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)