18 #include "libmesh/type_tensor.h" 19 #include "libmesh/dense_vector.h" 21 #include "metaphysicl/raw_type.h" 60 const TypeVector<T> & col2,
61 const TypeVector<T> & col3);
73 void reshape(
const unsigned int rows,
const unsigned int cols);
79 T &
operator()(
const unsigned int i,
const unsigned int j = 0);
87 T
operator()(
const unsigned int i,
const unsigned int j = 0)
const;
102 void fill(TypeTensor<T> & tensor);
107 void fill(DenseMatrix<T> & rhs);
112 void fill(DenseVector<T> & rhs);
170 unsigned int n()
const;
175 unsigned int m()
const;
233 template <
typename T2>
333 template <
typename T2>
335 template <
typename T2>
339 template <
typename T>
346 template <
typename T>
350 if (cols * rows == _n_entries)
359 _n_entries = _n_rows * _n_cols;
360 _values.resize(_n_entries);
364 template <
typename T>
368 if ((i * j) >= _n_entries)
369 mooseError(
"Reference outside of ColumnMajorMatrix bounds!");
372 return _values[(j * _n_rows) + i];
375 template <
typename T>
379 if ((i * j) >= _n_entries)
380 mooseError(
"Reference outside of ColumnMajorMatrix bounds!");
383 return _values[(j * _n_rows) + i];
386 template <
typename T>
392 for (
unsigned int i = 0; i < _n_rows; i++)
394 for (
unsigned int j = 0; j < _n_cols; j++)
395 Moose::out << std::setw(15) << s(i, j) <<
" ";
397 Moose::out << std::endl;
401 template <
typename T>
407 for (
unsigned int i = 0; i < _n_rows; i++)
409 for (
unsigned int j = 0; j < _n_cols; j++)
410 os << std::setw(15) << std::scientific << std::setprecision(8) << s(i, j) <<
" ";
416 template <
typename T>
422 "Cannot fill tensor! The ColumnMajorMatrix doesn't have the same number of entries!");
429 template <
typename T>
433 if (rhs.n() * rhs.m() != _n_entries)
435 "Cannot fill dense matrix! The ColumnMajorMatrix doesn't have the same number of entries!");
437 for (
unsigned int j = 0, index = 0; j < rhs.m(); ++j)
438 for (
unsigned int i = 0; i < rhs.n(); ++i, ++index)
439 rhs(i, j) = _values[index];
442 template <
typename T>
446 if (_n_rows != rhs.size() || _n_cols != 1)
447 mooseError(
"ColumnMajorMatrix and DenseVector must be the same shape for a fill!");
449 for (
unsigned int i = 0; i < _n_rows; ++i)
453 template <
typename T>
461 for (
unsigned int i = 0; i < _n_rows; i++)
462 for (
unsigned int j = 0; j < _n_cols; j++)
463 ret_matrix(j, i) = s(i, j);
468 template <
typename T>
478 for (
unsigned int i = 0; i < _n_rows; i++)
479 for (
unsigned int j = 0; j < _n_cols; j++)
480 ret_matrix(i, j) = s(i, j) - I(i, j) * (s.
tr() / 3.0);
485 template <
typename T>
489 this->checkSquareness();
491 for (
unsigned int i = 0; i < _n_rows; i++)
492 (*
this)(i, i) =
value;
495 template <
typename T>
499 this->checkSquareness();
501 for (
unsigned int i = 0; i < _n_rows; i++)
502 (*
this)(i, i) +=
value;
505 template <
typename T>
509 this->checkSquareness();
513 for (
unsigned int i = 0; i < _n_rows; i++)
514 trace += (*
this)(i, i);
519 template <
typename T>
523 for (
unsigned int i = 0; i < _n_entries; i++)
527 template <
typename T>
531 this->checkSquareness();
535 for (
unsigned int i = 0; i < _n_rows; i++)
539 template <
typename T>
543 this->checkShapeEquality(rhs);
547 for (
unsigned int j = 0; j < _n_cols; j++)
548 for (
unsigned int i = 0; i < _n_rows; i++)
549 value += (*
this)(i, j) * rhs(i, j);
554 template <
typename T>
561 template <
typename T>
568 template <
typename T>
575 template <
typename T>
582 template <
typename T>
590 _values.resize(_n_entries);
599 for (
unsigned int j = 0; j < _n_cols; j++)
600 for (
unsigned int i = 0; i < _n_cols; i++)
606 template <
typename T>
607 template <
typename T2>
611 this->reshape(rhs.
m(), rhs.
n());
613 for (MooseIndex(rhs.
m()) i = 0; i < rhs.
m(); ++i)
614 for (MooseIndex(rhs.
n()) j = 0; j < rhs.
n(); ++j)
615 (*
this)(i, j) = rhs(i, j);
620 template <
typename T>
626 for (
unsigned int i = 0; i < _n_entries; i++)
627 ret_matrix.
_values[i] = _values[i] * scalar;
632 template <
typename T>
637 mooseError(
"Cannot perform matvec operation! The column dimension of " 638 "the ColumnMajorMatrix does not match the TypeVector!");
642 for (
unsigned int i = 0; i < _n_rows; ++i)
643 for (
unsigned int j = 0; j < _n_cols; ++j)
644 ret_matrix.
_values[i] += (*
this)(i, j) * rhs(j);
666 template <
typename T>
672 "Cannot perform matrix multiply! The shapes of the two operands are not compatible!");
676 for (
unsigned int i = 0; i < ret_matrix.
_n_rows; ++i)
677 for (
unsigned int j = 0; j < ret_matrix.
_n_cols; ++j)
678 for (
unsigned int k = 0; k < _n_cols; ++k)
679 ret_matrix(i, j) += (*this)(i, k) * rhs(k, j);
684 template <
typename T>
688 this->checkShapeEquality(rhs);
692 for (
unsigned int i = 0; i < _n_entries; i++)
698 template <
typename T>
702 this->checkShapeEquality(rhs);
706 for (
unsigned int i = 0; i < _n_entries; i++)
712 template <
typename T>
716 this->checkShapeEquality(rhs);
718 for (
unsigned int i = 0; i < _n_entries; i++)
724 template <
typename T>
729 mooseError(
"Cannot perform matrix addition and assignment! The shapes of the two operands are " 734 (*this)(i, j) += rhs(i, j);
739 template <
typename T>
743 this->checkShapeEquality(rhs);
745 for (
unsigned int i = 0; i < _n_entries; i++)
751 template <
typename T>
757 for (
unsigned int i = 0; i < _n_entries; i++)
758 ret_matrix.
_values[i] = _values[i] + scalar;
763 template <
typename T>
767 for (
unsigned int i = 0; i < _n_entries; i++)
768 _values[i] *= scalar;
772 template <
typename T>
776 for (
unsigned int i = 0; i < _n_entries; i++)
777 _values[i] /= scalar;
781 template <
typename T>
785 for (
unsigned int i = 0; i < _n_entries; i++)
786 _values[i] += scalar;
790 template <
typename T>
796 return std::equal(_values.begin(), _values.end(), rhs.
_values.begin());
799 template <
typename T>
803 return !(*
this == rhs);
810 template <
typename T>
818 for (MooseIndex(in.
m()) i = 0; i < in.
m(); ++i)
819 for (MooseIndex(in.
n()) j = 0; j < in.
n(); ++j)
void inverse(ColumnMajorMatrixTempl< T > &invA) const
Returns inverse of a general matrix.
T tr() const
The trace of the CMM.
ColumnMajorMatrixTempl< T > & operator/=(T scalar)
Scalar Division plus assignment.
friend void dataStore(std::ostream &, ColumnMajorMatrixTempl< T2 > &, void *)
void print_scientific(std::ostream &os)
Prints to file.
void identity()
Turn the matrix into an identity matrix.
This class defines a Tensor that can change its shape.
ColumnMajorMatrixTempl< T > & operator-=(const ColumnMajorMatrixTempl< T > &rhs)
Matrix Matrix Subtraction plus assignment.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
void reshape(const unsigned int rows, const unsigned int cols)
Change the shape of the tensor.
ColumnMajorMatrixTempl< T > abs()
Returns a matrix that is the absolute value of the matrix this was called on.
T norm()
The Euclidean norm of the matrix.
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
ColumnMajorMatrixTempl< T > kronecker(const ColumnMajorMatrixTempl< T > &rhs) const
Kronecker Product.
ColumnMajorMatrixTempl< T > deviatoric()
Returns a matrix that is the deviatoric of the matrix this was called on.
std::basic_ostream< charT, traits > * os
void eigen(ColumnMajorMatrixTempl< T > &eval, ColumnMajorMatrixTempl< T > &evec) const
Returns eigen system solve for a symmetric real matrix.
void checkSquareness() const
Check if matrix is square.
void setDiag(T value)
Set the value of each of the diagonals to the passed in value.
T * rawData()
Returns a reference to the raw data pointer.
void addDiag(T value)
Add to each of the diagonals the passsed in value.
ColumnMajorMatrixTempl< T > operator*(T scalar) const
Scalar multiplication of the ColumnMajorMatrixTempl.
ColumnMajorMatrixTempl< T > & operator*=(T scalar)
Scalar Multiplication plus assignment.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void fill(TypeTensor< T > &tensor)
Fills the passed in tensor with the values from this tensor.
unsigned int m() const
Returns the number of columns.
ColumnMajorMatrixTempl< T > operator+(const ColumnMajorMatrixTempl< T > &rhs) const
Matrix Matrix Addition.
ColumnMajorMatrixTempl< T > operator-(const ColumnMajorMatrixTempl< T > &rhs) const
Matrix Matrix Subtraction.
T doubleContraction(const ColumnMajorMatrixTempl< T > &rhs) const
Double contraction of two matrices ie A : B = Sum(A_ab * B_ba)
bool operator!=(const ColumnMajorMatrixTempl< T > &rhs) const
ColumnMajorMatrixTempl< Real > ColumnMajorMatrix
void zero()
Zero the matrix.
ColumnMajorMatrixTempl< T > transpose() const
Returns a matrix that is the transpose of the matrix this was called on.
void eigenNonsym(ColumnMajorMatrixTempl< T > &eval_real, ColumnMajorMatrixTempl< T > &eval_img, ColumnMajorMatrixTempl< T > &evec_right, ColumnMajorMatrixTempl< T > &eve_left) const
Returns eigen system solve for a non-symmetric real matrix.
void print()
Print the tensor.
void checkShapeEquality(const ColumnMajorMatrixTempl< T > &rhs) const
Check if matrices are of same shape.
T & operator()(const unsigned int i, const unsigned int j=0)
Get the i,j entry j defaults to zero so you can use it as a column vector.
ColumnMajorMatrixTempl(const unsigned int rows=Moose::dim, const unsigned int cols=Moose::dim)
Constructor that sets an initial number of entries and shape.
friend void dataLoad(std::istream &, ColumnMajorMatrixTempl< T2 > &, void *)
IntRange< T > make_range(T beg, T end)
ColumnMajorMatrixTempl< T > & operator=(const TypeTensor< T > &rhs)
Sets the values in this tensor to the values on the RHS.
unsigned int n() const
Returns the number of rows.
void exp(ColumnMajorMatrixTempl< T > &z) const
Returns matrix that is the exponential of the matrix this was called on.
ColumnMajorMatrixTempl< T > & operator+=(const ColumnMajorMatrixTempl< T > &rhs)
Matrix Matrix Addition plus assignment.
unsigned int numEntries() const
The total number of entries in the Tensor.
bool operator==(const ColumnMajorMatrixTempl< T > &rhs) const
Equality operators.