20#ifndef LIBMESH_DENSE_MATRIX_H
21#define LIBMESH_DENSE_MATRIX_H
24#include "libmesh/libmesh_common.h"
25#include "libmesh/dense_matrix_base.h"
26#include "libmesh/int_range.h"
27#include "libmesh/parallel_algorithms.h"
30#if (LIBMESH_HAVE_PETSC)
31# include "libmesh/petsc_macro.h"
36#include "libmesh/ignore_warnings.h"
38#include "libmesh/restore_warnings.h"
47#include <initializer_list>
50#ifdef LIBMESH_HAVE_METAPHYSICL
51#include "metaphysicl/dualnumber_decl.h"
52#include "metaphysicl/raw_type.h"
59template <
typename T>
class DenseVector;
80 const unsigned int new_n=0);
87 template <
typename T2>
90 std::initializer_list<T2> init_list);
107 virtual void zero() override final;
113 unsigned int col_id,
unsigned int col_size) const;
118 T operator() (const
unsigned int i,
119 const
unsigned int j) const;
124 T & operator() (const
unsigned int i,
125 const
unsigned int j);
127 virtual T
el(const
unsigned int i,
128 const
unsigned int j) const override final
129 {
return (*
this)(i,j); }
131 virtual T &
el(
const unsigned int i,
132 const unsigned int j)
override final
133 {
return (*
this)(i,j); }
140 template <
typename T2>
148 template <
typename T2>
163 template <
typename T2>
179 template <
typename T2>
196 template <
typename T2,
typename T3>
239 template <
typename T2>
255 const unsigned int new_n);
279 template<
typename T2,
typename T3>
280 typename std::enable_if<
349 template <typename T2>
362 template <typename T2>
369 const
unsigned int j) const;
397 const unsigned int j,
440 template <
typename T2>
488 Real rcond=std::numeric_limits<Real>::epsilon())
const;
575 for (
const T & v : var.
_val)
595 bool has_inf =
false;
596 for (
const T & v : var.
_val)
601 has_inf = has_inf ||
isinf(v);
618 for (
const T & v : var.
_val)
690 template <
typename T2>
770 std::vector<Real> & sigma_val,
771 std::vector<Number> & U_val,
772 std::vector<Number> & VT_val);
792#if (LIBMESH_HAVE_PETSC && LIBMESH_USE_REAL_NUMBERS)
826 bool trans=
false)
const;
832 template <
typename T2>
839 template <
typename T2>
851namespace DenseMatrices
873using namespace DenseMatrices;
878#if defined(LIBMESH_HAVE_PETSC) && \
879 defined(LIBMESH_USE_REAL_NUMBERS) && \
880 defined(LIBMESH_DEFAULT_DOUBLE_PRECISION)
884 static const bool value =
true;
894 const unsigned int new_n) :
898 _decomposition_type(NONE)
900 this->
resize(new_m,new_n);
904template <
typename T2>
907 std::initializer_list<T2> init_list) :
910 _val(init_list.begin(), init_list.end()),
911 _decomposition_type(NONE)
915 libmesh_assert_equal_to(nrow * ncol, init_list.size());
925 swap(this->_m, other_matrix.
_m);
926 swap(this->_n, other_matrix.
_n);
927 _val.swap(other_matrix.
_val);
935template <
typename T2>
940 unsigned int mat_m = mat.
m(), mat_n = mat.
n();
941 this->resize(mat_m, mat_n);
942 for (
unsigned int i=0; i<mat_m; i++)
943 for (
unsigned int j=0; j<mat_n; j++)
944 (*
this)(i,j) = mat(i,j);
954 const unsigned int new_n)
971 _decomposition_type = NONE;
973 std::fill (_val.begin(), _val.end(),
static_cast<T
>(0));
981 unsigned int col_id,
unsigned int col_size)
const
983 libmesh_assert_less (row_id + row_size - 1, this->_m);
984 libmesh_assert_less (col_id + col_size - 1, this->_n);
989 sub.
_val.resize(row_size * col_size);
991 unsigned int end_col = this->_n - col_size - col_id;
992 unsigned int p = row_id * this->_n;
994 for (
unsigned int i=0; i<row_size; i++)
998 for (
unsigned int j=0; j<col_size; j++)
999 sub.
_val[q++] = _val[p++];
1012 const unsigned int j)
const
1014 libmesh_assert_less (i*j, _val.size());
1015 libmesh_assert_less (i, this->_m);
1016 libmesh_assert_less (j, this->_n);
1020 return _val[(i)*(this->_n) + (j)];
1028 const unsigned int j)
1030 libmesh_assert_less (i*j, _val.size());
1031 libmesh_assert_less (i, this->_m);
1032 libmesh_assert_less (j, this->_n);
1035 return _val[(i)*(this->_n) + (j)];
1046 for (
auto & v : _val)
1056 (*
this)(i, col) *= factor;
1065 this->scale(factor);
1072template<
typename T2,
typename T3>
1074typename std::enable_if<
1079 libmesh_assert_equal_to (this->m(), mat.
m());
1080 libmesh_assert_equal_to (this->n(), mat.
n());
1084 (*
this)(i,j) += factor * mat(i,j);
1094 if (_val[i] != mat.
_val[i])
1107 if (_val[i] != mat.
_val[i])
1120 _val[i] += mat.
_val[i];
1132 _val[i] -= mat.
_val[i];
1147 (_val.begin(), _val.end(), std::numeric_limits<realfromT>::max(),
1148 [](
const auto & a,
const auto &
b){using std::min; return min(a,b);},
1149 [](
const T & v){return libmesh_real(v);});
1162 (_val.begin(), _val.end(), std::numeric_limits<realfromT>::lowest(),
1163 [](
const auto & a,
const auto &
b){using std::max; return max(a,b);},
1164 [](
const T & v){return libmesh_real(v);});
1177 auto columnsum = abs(T(0));
1178 for (
unsigned int i=0; i!=this->_m; i++)
1180 columnsum += abs((*
this)(i,0));
1182 auto my_max = columnsum;
1183 for (
unsigned int j=1; j!=this->_n; j++)
1186 for (
unsigned int i=0; i!=this->_m; i++)
1188 columnsum += abs((*
this)(i,j));
1190 my_max = (my_max > columnsum? my_max : columnsum);
1205 auto rowsum = abs(T(0));
1206 for (
unsigned int j=0; j!=this->_n; j++)
1208 rowsum += abs((*
this)(0,j));
1210 auto my_max = rowsum;
1211 for (
unsigned int i=1; i!=this->_m; i++)
1214 for (
unsigned int j=0; j!=this->_n; j++)
1216 rowsum += abs((*
this)(i,j));
1218 my_max = (my_max > rowsum? my_max : rowsum);
1228 const unsigned int j)
const
1231 return (*
this)(j,i);
1269#ifdef LIBMESH_HAVE_METAPHYSICL
1272template <
typename T>
1279 const auto m = in.
m(), n = in.
n();
1281 for (
unsigned int i = 0; i < m; ++i)
1282 for (
unsigned int j = 0; j < n; ++j)
1283 ret(i,j) = raw_value(in(i,j));
Defines an abstract dense matrix base class for use in Finite Element-type computations.
void condense(const unsigned int i, const unsigned int j, const T val, DenseVectorBase< T > &rhs)
Condense-out the (i,j) entry of the matrix, forcing it to take on the value val.
unsigned int _n
The column dimension.
unsigned int _m
The row dimension.
Defines a dense matrix for use in Finite Element-type computations.
void scale_column(const unsigned int col, const T factor)
Multiplies every element in the column col matrix by factor.
auto min() const -> decltype(libmesh_real(T(0)))
void _cholesky_decompose()
Decomposes a symmetric positive definite matrix into a product of two lower triangular matrices accor...
void _lu_decompose()
Form the LU decomposition of the matrix.
DenseMatrix(DenseMatrix &&)=default
The 5 special functions can be defaulted for this class, as it does not manage any memory itself.
friend bool isnan(const DenseMatrix< T > &var)
Returns true iff any entry is NaN.
void evd_left(DenseVector< T > &lambda_real, DenseVector< T > &lambda_imag, DenseMatrix< T > &VL)
Compute the eigenvalues (both real and imaginary parts) and left eigenvectors of a general matrix,...
void _lu_back_substitute(const DenseVector< T > &b, DenseVector< T > &x) const
Solves the system Ax=b through back substitution.
void right_multiply_transpose(const DenseMatrix< T > &A)
Right multiplies by the transpose of the matrix A.
DenseMatrix(const unsigned int new_m=0, const unsigned int new_n=0)
Constructor.
friend bool isfinite(const DenseMatrix< T > &var)
Returns true iff every entry is finite.
void _left_multiply_transpose(const DenseMatrix< T2 > &A)
Left multiplies by the transpose of the matrix A which may contain a different numerical type.
friend bool isinf(const DenseMatrix< T > &var)
Returns true iff no entry is NaN and any entry is infinite.
std::vector< T > & get_values()
virtual T & el(const unsigned int i, const unsigned int j) override final
void _right_multiply_transpose(const DenseMatrix< T2 > &A)
Right multiplies by the transpose of the matrix A which may contain a different numerical type.
void _evd_lapack(DenseVector< T > &lambda_real, DenseVector< T > &lambda_imag, DenseMatrix< T > *VL=nullptr, DenseMatrix< T > *VR=nullptr)
Computes the eigenvalues of the matrix using the Lapack routine "DGEEV".
DenseMatrix(unsigned int nrow, unsigned int ncol, std::initializer_list< T2 > init_list)
Constructor taking the number of rows, columns, and an initializer_list, which must be of length nrow...
DenseMatrix sub_matrix(unsigned int row_id, unsigned int row_size, unsigned int col_id, unsigned int col_size) const
Get submatrix with the smallest row and column indices and the submatrix size.
void _lu_decompose_lapack()
Computes an LU factorization of the matrix using the Lapack routine "getrf".
void svd_solve(const DenseVector< T > &rhs, DenseVector< T > &x, Real rcond=std::numeric_limits< Real >::epsilon()) const
Solve the system of equations for in the least-squares sense.
DenseMatrix & operator=(const DenseMatrix &)=default
void _svd_lapack(DenseVector< Real > &sigma)
Computes an SVD of the matrix using the Lapack routine "getsvd".
void vector_mult(DenseVector< T > &dest, const DenseVector< T > &arg) const
Performs the matrix-vector multiplication, dest := (*this) * arg.
void cholesky_solve(const DenseVector< T2 > &b, DenseVector< T2 > &x)
For symmetric positive definite (SPD) matrices.
bool operator!=(const DenseMatrix< T > &mat) const
DenseMatrix(const DenseMatrix &)=default
void _cholesky_back_substitute(const DenseVector< T2 > &b, DenseVector< T2 > &x) const
Solves the equation Ax=b for the unknown value x and rhs b based on the Cholesky factorization of A.
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
void svd(DenseVector< Real > &sigma)
Compute the singular value decomposition of the matrix.
bool use_blas_lapack
Computes the inverse of the dense matrix (assuming it is invertible) by first computing the LU decomp...
std::vector< T > _val
The actual data values, stored as a 1D array.
void lu_solve(const DenseVector< T > &b, DenseVector< T > &x)
Solve the system Ax=b given the input vector b.
void _svd_solve_lapack(const DenseVector< T > &rhs, DenseVector< T > &x, Real rcond) const
Called by svd_solve(rhs).
T operator()(const unsigned int i, const unsigned int j) const
virtual void right_multiply(const DenseMatrixBase< T > &M2) override final
Performs the operation: (*this) <- (*this) * M3.
void get_principal_submatrix(unsigned int sub_m, unsigned int sub_n, DenseMatrix< T > &dest) const
Put the sub_m x sub_n principal submatrix into dest.
std::vector< pivot_index_t > _pivots
void evd_right(DenseVector< T > &lambda_real, DenseVector< T > &lambda_imag, DenseMatrix< T > &VR)
Compute the eigenvalues (both real and imaginary parts) and right eigenvectors of a general matrix,...
void get_transpose(DenseMatrix< T > &dest) const
Put the tranposed matrix into dest.
virtual T el(const unsigned int i, const unsigned int j) const override final
void swap(DenseMatrix< T > &other_matrix)
STL-like swap method.
void vector_mult_transpose(DenseVector< T > &dest, const DenseVector< T > &arg) const
Performs the matrix-vector multiplication, dest := (*this)^T * arg.
DenseMatrix< T > & operator-=(const DenseMatrix< T > &mat)
Subtracts mat from this matrix.
T transpose(const unsigned int i, const unsigned int j) const
const std::vector< T > & get_values() const
DecompositionType _decomposition_type
This flag keeps track of which type of decomposition has been performed on the matrix.
void _svd_helper(char JOBU, char JOBVT, std::vector< Real > &sigma_val, std::vector< Number > &U_val, std::vector< Number > &VT_val)
Helper function that actually performs the SVD.
void scale(const T factor)
Multiplies every element in the matrix by factor.
virtual void left_multiply(const DenseMatrixBase< T > &M2) override final
Performs the operation: (*this) <- M2 * (*this)
void outer_product(const DenseVector< T > &a, const DenseVector< T > &b)
Computes the outer (dyadic) product of two vectors and stores in (*this).
PetscBLASInt pivot_index_t
Array used to store pivot indices.
void _matvec_blas(T alpha, T beta, DenseVector< T > &dest, const DenseVector< T > &arg, bool trans=false) const
Uses the BLAS GEMV function (through PETSc) to compute.
void condense(const unsigned int i, const unsigned int j, const T val, DenseVector< T > &rhs)
Condense-out the (i,j) entry of the matrix, forcing it to take on the value val.
void evd(DenseVector< T > &lambda_real, DenseVector< T > &lambda_imag)
Compute the eigenvalues (both real and imaginary parts) of a general matrix.
std::enable_if< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseMatrix< T3 > &mat)
Adds factor times mat to this matrix.
void evd_left_and_right(DenseVector< T > &lambda_real, DenseVector< T > &lambda_imag, DenseMatrix< T > &VL, DenseMatrix< T > &VR)
Compute the eigenvalues (both real and imaginary parts) as well as the left and right eigenvectors of...
virtual ~DenseMatrix()=default
bool operator==(const DenseMatrix< T > &mat) const
void _multiply_blas(const DenseMatrixBase< T > &other, _BLAS_Multiply_Flag flag)
The _multiply_blas function computes A <- op(A) * op(B) using BLAS gemm function.
void left_multiply_transpose(const DenseMatrix< T > &A)
Left multiplies by the transpose of the matrix A.
auto max() const -> decltype(libmesh_real(T(0)))
void vector_mult_add(DenseVector< T > &dest, const T factor, const DenseVector< T > &arg) const
Performs the scaled matrix-vector multiplication, dest += factor * (*this) * arg.
DenseMatrix< T > & operator*=(const T factor)
Multiplies every element in the matrix by factor.
DecompositionType
The decomposition schemes above change the entries of the matrix A.
DenseMatrix< T > & operator+=(const DenseMatrix< T > &mat)
Adds mat to this matrix.
void _lu_back_substitute_lapack(const DenseVector< T > &b, DenseVector< T > &x)
Companion function to _lu_decompose_lapack().
virtual void zero() override final
Sets all elements of the matrix to 0 and resets any decomposition flag which may have been previously...
_BLAS_Multiply_Flag
Enumeration used to determine the behavior of the _multiply_blas function.
@ LEFT_MULTIPLY_TRANSPOSE
@ RIGHT_MULTIPLY_TRANSPOSE
Defines a dense vector for use in Finite Element-type computations.
DenseMatrix< Complex > ComplexDenseMatrix
This typedef may be either a real-only matrix, or a truly complex matrix, depending on how Number was...
DenseMatrix< Real > RealDenseMatrix
Convenient definition of a real-only dense matrix.
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
LIBMESH_DEVICE_INLINE bool isfinite(std::complex< T > a)
LIBMESH_DEVICE_INLINE bool isnan(std::complex< T > a)
LIBMESH_DEVICE_INLINE T libmesh_real(T a)
LIBMESH_DEVICE_INLINE bool isinf(std::complex< T > a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
T libmesh_transform_reduce(InputIt begin, InputIt end, T init, BinaryOp reduce, UnaryOp transform)
Helper structure for determining whether to use blas_lapack.