17 #include "libmesh/type_tensor.h" 18 #include "libmesh/dense_vector.h" 20 #include "metaphysicl/raw_type.h" 59 const TypeVector<T> & col2,
60 const TypeVector<T> & col3);
72 void reshape(
const unsigned int rows,
const unsigned int cols);
78 T &
operator()(
const unsigned int i,
const unsigned int j = 0);
86 T
operator()(
const unsigned int i,
const unsigned int j = 0)
const;
101 void fill(TypeTensor<T> & tensor);
106 void fill(DenseMatrix<T> & rhs);
111 void fill(DenseVector<T> & rhs);
169 unsigned int n()
const;
174 unsigned int m()
const;
232 template <
typename T2>
332 template <
typename T2>
334 template <
typename T2>
338 template <
typename T>
345 template <
typename T>
349 if (cols * rows == _n_entries)
358 _n_entries = _n_rows * _n_cols;
359 _values.resize(_n_entries);
363 template <
typename T>
367 if ((i * j) >= _n_entries)
368 mooseError(
"Reference outside of ColumnMajorMatrix bounds!");
371 return _values[(j * _n_rows) + i];
374 template <
typename T>
378 if ((i * j) >= _n_entries)
379 mooseError(
"Reference outside of ColumnMajorMatrix bounds!");
382 return _values[(j * _n_rows) + i];
385 template <
typename T>
391 for (
unsigned int i = 0; i < _n_rows; i++)
393 for (
unsigned int j = 0; j < _n_cols; j++)
394 Moose::out << std::setw(15) << s(i, j) <<
" ";
396 Moose::out << std::endl;
400 template <
typename T>
406 for (
unsigned int i = 0; i < _n_rows; i++)
408 for (
unsigned int j = 0; j < _n_cols; j++)
409 os << std::setw(15) << std::scientific << std::setprecision(8) << s(i, j) <<
" ";
415 template <
typename T>
421 "Cannot fill tensor! The ColumnMajorMatrix doesn't have the same number of entries!");
428 template <
typename T>
432 if (rhs.n() * rhs.m() != _n_entries)
434 "Cannot fill dense matrix! The ColumnMajorMatrix doesn't have the same number of entries!");
436 for (
unsigned int j = 0, index = 0; j < rhs.m(); ++j)
437 for (
unsigned int i = 0; i < rhs.n(); ++i, ++index)
438 rhs(i, j) = _values[index];
441 template <
typename T>
445 if (_n_rows != rhs.size() || _n_cols != 1)
446 mooseError(
"ColumnMajorMatrix and DenseVector must be the same shape for a fill!");
448 for (
unsigned int i = 0; i < _n_rows; ++i)
452 template <
typename T>
460 for (
unsigned int i = 0; i < _n_rows; i++)
461 for (
unsigned int j = 0; j < _n_cols; j++)
462 ret_matrix(j, i) = s(i, j);
467 template <
typename T>
477 for (
unsigned int i = 0; i < _n_rows; i++)
478 for (
unsigned int j = 0; j < _n_cols; j++)
479 ret_matrix(i, j) = s(i, j) - I(i, j) * (s.
tr() / 3.0);
484 template <
typename T>
488 this->checkSquareness();
490 for (
unsigned int i = 0; i < _n_rows; i++)
491 (*
this)(i, i) =
value;
494 template <
typename T>
498 this->checkSquareness();
500 for (
unsigned int i = 0; i < _n_rows; i++)
501 (*
this)(i, i) +=
value;
504 template <
typename T>
508 this->checkSquareness();
512 for (
unsigned int i = 0; i < _n_rows; i++)
513 trace += (*
this)(i, i);
518 template <
typename T>
522 for (
unsigned int i = 0; i < _n_entries; i++)
526 template <
typename T>
530 this->checkSquareness();
534 for (
unsigned int i = 0; i < _n_rows; i++)
538 template <
typename T>
542 this->checkShapeEquality(rhs);
546 for (
unsigned int j = 0; j < _n_cols; j++)
547 for (
unsigned int i = 0; i < _n_rows; i++)
548 value += (*
this)(i, j) * rhs(i, j);
553 template <
typename T>
560 template <
typename T>
567 template <
typename T>
574 template <
typename T>
581 template <
typename T>
589 _values.resize(_n_entries);
598 for (
unsigned int j = 0; j < _n_cols; j++)
599 for (
unsigned int i = 0; i < _n_cols; i++)
605 template <
typename T>
606 template <
typename T2>
610 this->reshape(rhs.
m(), rhs.
n());
612 for (MooseIndex(rhs.
m()) i = 0; i < rhs.
m(); ++i)
613 for (MooseIndex(rhs.
n()) j = 0; j < rhs.
n(); ++j)
614 (*
this)(i, j) = rhs(i, j);
619 template <
typename T>
625 for (
unsigned int i = 0; i < _n_entries; i++)
626 ret_matrix.
_values[i] = _values[i] * scalar;
631 template <
typename T>
636 mooseError(
"Cannot perform matvec operation! The column dimension of " 637 "the ColumnMajorMatrix does not match the TypeVector!");
641 for (
unsigned int i = 0; i < _n_rows; ++i)
642 for (
unsigned int j = 0; j < _n_cols; ++j)
643 ret_matrix.
_values[i] += (*
this)(i, j) * rhs(j);
665 template <
typename T>
671 "Cannot perform matrix multiply! The shapes of the two operands are not compatible!");
675 for (
unsigned int i = 0; i < ret_matrix.
_n_rows; ++i)
676 for (
unsigned int j = 0; j < ret_matrix.
_n_cols; ++j)
677 for (
unsigned int k = 0; k < _n_cols; ++k)
678 ret_matrix(i, j) += (*this)(i, k) * rhs(k, j);
683 template <
typename T>
687 this->checkShapeEquality(rhs);
691 for (
unsigned int i = 0; i < _n_entries; i++)
697 template <
typename T>
701 this->checkShapeEquality(rhs);
705 for (
unsigned int i = 0; i < _n_entries; i++)
711 template <
typename T>
715 this->checkShapeEquality(rhs);
717 for (
unsigned int i = 0; i < _n_entries; i++)
723 template <
typename T>
728 mooseError(
"Cannot perform matrix addition and assignment! The shapes of the two operands are " 733 (*this)(i, j) += rhs(i, j);
738 template <
typename T>
742 this->checkShapeEquality(rhs);
744 for (
unsigned int i = 0; i < _n_entries; i++)
750 template <
typename T>
756 for (
unsigned int i = 0; i < _n_entries; i++)
757 ret_matrix.
_values[i] = _values[i] + scalar;
762 template <
typename T>
766 for (
unsigned int i = 0; i < _n_entries; i++)
767 _values[i] *= scalar;
771 template <
typename T>
775 for (
unsigned int i = 0; i < _n_entries; i++)
776 _values[i] /= scalar;
780 template <
typename T>
784 for (
unsigned int i = 0; i < _n_entries; i++)
785 _values[i] += scalar;
789 template <
typename T>
795 return std::equal(_values.begin(), _values.end(), rhs.
_values.begin());
798 template <
typename T>
802 return !(*
this == rhs);
809 template <
typename T>
817 for (MooseIndex(in.
m()) i = 0; i < in.
m(); ++i)
818 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.