libMesh
Loading...
Searching...
No Matches
dense_matrix.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_DENSE_MATRIX_H
21#define LIBMESH_DENSE_MATRIX_H
22
23// Local Includes
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"
28
29// For the definition of PetscBLASInt.
30#if (LIBMESH_HAVE_PETSC)
31# include "libmesh/petsc_macro.h"
32# ifdef I
33# define LIBMESH_SAW_I
34# endif
35
36#include "libmesh/ignore_warnings.h"
37# include <petscsys.h>
38#include "libmesh/restore_warnings.h"
39
40# ifndef LIBMESH_SAW_I
41# undef I // Avoid complex.h contamination
42# endif
43#endif
44
45// C++ includes
46#include <algorithm>
47#include <initializer_list>
48#include <vector>
49
50#ifdef LIBMESH_HAVE_METAPHYSICL
51#include "metaphysicl/dualnumber_decl.h"
52#include "metaphysicl/raw_type.h"
53#endif
54
55namespace libMesh
56{
57
58// Forward Declarations
59template <typename T> class DenseVector;
60
71template<typename T>
73{
74public:
75
79 DenseMatrix(const unsigned int new_m=0,
80 const unsigned int new_n=0);
81
87 template <typename T2>
88 DenseMatrix(unsigned int nrow,
89 unsigned int ncol,
90 std::initializer_list<T2> init_list);
91
96 DenseMatrix (DenseMatrix &&) = default;
97 DenseMatrix (const DenseMatrix &) = default;
98 DenseMatrix & operator= (const DenseMatrix &) = default;
100 virtual ~DenseMatrix() = default;
101
107 virtual void zero() override final;
108
112 DenseMatrix sub_matrix(unsigned int row_id, unsigned int row_size,
113 unsigned int col_id, unsigned int col_size) const;
114
118 T operator() (const unsigned int i,
119 const unsigned int j) const;
120
124 T & operator() (const unsigned int i,
125 const unsigned int j);
126
127 virtual T el(const unsigned int i,
128 const unsigned int j) const override final
129 { return (*this)(i,j); }
130
131 virtual T & el(const unsigned int i,
132 const unsigned int j) override final
133 { return (*this)(i,j); }
134
135 virtual void left_multiply (const DenseMatrixBase<T> & M2) override final;
136
140 template <typename T2>
141 void left_multiply (const DenseMatrixBase<T2> & M2);
142
143 virtual void right_multiply (const DenseMatrixBase<T> & M2) override final;
144
148 template <typename T2>
149 void right_multiply (const DenseMatrixBase<T2> & M2);
150
155 void vector_mult (DenseVector<T> & dest,
156 const DenseVector<T> & arg) const;
157
163 template <typename T2>
165 const DenseVector<T2> & arg) const;
166
172 const DenseVector<T> & arg) const;
173
179 template <typename T2>
181 const DenseVector<T2> & arg) const;
182
187 void vector_mult_add (DenseVector<T> & dest,
188 const T factor,
189 const DenseVector<T> & arg) const;
190
196 template <typename T2, typename T3>
197 void vector_mult_add (DenseVector<typename CompareTypes<T, typename CompareTypes<T2,T3>::supertype>::supertype> & dest,
198 const T2 factor,
199 const DenseVector<T3> & arg) const;
200
204 void get_principal_submatrix (unsigned int sub_m, unsigned int sub_n, DenseMatrix<T> & dest) const;
205
209 void get_principal_submatrix (unsigned int sub_m, DenseMatrix<T> & dest) const;
210
228 void outer_product(const DenseVector<T> & a, const DenseVector<T> & b);
229
239 template <typename T2>
241
245 void swap(DenseMatrix<T> & other_matrix);
246
254 void resize(const unsigned int new_m,
255 const unsigned int new_n);
256
260 void scale (const T factor);
261
265 void scale_column (const unsigned int col, const T factor);
266
272 DenseMatrix<T> & operator *= (const T factor);
273
279 template<typename T2, typename T3>
280 typename std::enable_if<
281 ScalarTraits<T2>::value, void >::type add (const T2 factor,
282 const DenseMatrix<T3> & mat);
283
287 bool operator== (const DenseMatrix<T> & mat) const;
288
292 bool operator!= (const DenseMatrix<T> & mat) const;
293
300
307
312 auto min () const -> decltype(libmesh_real(T(0)));
313
318 auto max () const -> decltype(libmesh_real(T(0)));
319
328 auto l1_norm () const;
329
338 auto linfty_norm () const;
339
343 void left_multiply_transpose (const DenseMatrix<T> & A);
344
349 template <typename T2>
350 void left_multiply_transpose (const DenseMatrix<T2> & A);
351
352
356 void right_multiply_transpose (const DenseMatrix<T> & A);
357
362 template <typename T2>
363 void right_multiply_transpose (const DenseMatrix<T2> & A);
364
368 T transpose (const unsigned int i,
369 const unsigned int j) const;
370
374 void get_transpose(DenseMatrix<T> & dest) const;
375
383 std::vector<T> & get_values() { return _val; }
384
388 const std::vector<T> & get_values() const { return _val; }
389
396 void condense(const unsigned int i,
397 const unsigned int j,
398 const T val,
399 DenseVector<T> & rhs)
400 { DenseMatrixBase<T>::condense (i, j, val, rhs); }
401
416 void lu_solve (const DenseVector<T> & b,
417 DenseVector<T> & x);
418
440 template <typename T2>
441 void cholesky_solve(const DenseVector<T2> & b,
442 DenseVector<T2> & x);
443
452 void svd(DenseVector<Real> & sigma);
453
465 void svd(DenseVector<Real> & sigma,
468
486 void svd_solve(const DenseVector<T> & rhs,
487 DenseVector<T> & x,
488 Real rcond=std::numeric_limits<Real>::epsilon()) const;
489
497 void evd(DenseVector<T> & lambda_real,
498 DenseVector<T> & lambda_imag);
499
518 void evd_left(DenseVector<T> & lambda_real,
519 DenseVector<T> & lambda_imag,
520 DenseMatrix<T> & VL);
521
540 void evd_right(DenseVector<T> & lambda_real,
541 DenseVector<T> & lambda_imag,
542 DenseMatrix<T> & VR);
543
554 void evd_left_and_right(DenseVector<T> & lambda_real,
555 DenseVector<T> & lambda_imag,
556 DenseMatrix<T> & VL,
557 DenseMatrix<T> & VR);
558
566 T det();
567
571 friend bool isfinite (const DenseMatrix<T> & var)
572 {
573 using std::isfinite;
574 using libMesh::isfinite; // for T==complex
575 for (const T & v : var._val)
576 if (!isfinite(v))
577 return false;
578 return true;
579 }
580
589 friend bool isinf (const DenseMatrix<T> & var)
590 {
591 using std::isinf;
592 using libMesh::isinf; // for T==complex
593 using std::isnan;
594 using libMesh::isnan;
595 bool has_inf = false;
596 for (const T & v : var._val)
597 {
598 // NaN anywhere makes us NaN, not inf
599 if (isnan(v))
600 return false;
601 has_inf = has_inf || isinf(v);
602 }
603 return has_inf;
604 }
605
614 friend bool isnan (const DenseMatrix<T> & var)
615 {
616 using std::isnan;
617 using libMesh::isnan; // for T==complex
618 for (const T & v : var._val)
619 if (isnan(v))
620 return true;
621 return false;
622 }
623
635 // void inverse();
636
643
648 {
649 static const bool value = false;
650 };
651
652private:
653
657 std::vector<T> _val;
658
664 void _lu_decompose ();
665
672 DenseVector<T> & x) const;
673
681 void _cholesky_decompose();
682
690 template <typename T2>
692 DenseVector<T2> & x) const;
693
702
708
719
727 void _multiply_blas(const DenseMatrixBase<T> & other,
729
740
746 void _svd_lapack(DenseVector<Real> & sigma);
747
753 void _svd_lapack(DenseVector<Real> & sigma,
756
760 void _svd_solve_lapack(const DenseVector<T> & rhs,
761 DenseVector<T> & x,
762 Real rcond) const;
763
768 void _svd_helper (char JOBU,
769 char JOBVT,
770 std::vector<Real> & sigma_val,
771 std::vector<Number> & U_val,
772 std::vector<Number> & VT_val);
773
782 void _evd_lapack(DenseVector<T> & lambda_real,
783 DenseVector<T> & lambda_imag,
784 DenseMatrix<T> * VL = nullptr,
785 DenseMatrix<T> * VR = nullptr);
786
792#if (LIBMESH_HAVE_PETSC && LIBMESH_USE_REAL_NUMBERS)
793 typedef PetscBLASInt pivot_index_t;
794#else
795 typedef int pivot_index_t;
796#endif
797 std::vector<pivot_index_t> _pivots;
798
809 DenseVector<T> & x);
810
823 void _matvec_blas(T alpha, T beta,
824 DenseVector<T> & dest,
825 const DenseVector<T> & arg,
826 bool trans=false) const;
827
832 template <typename T2>
834
839 template <typename T2>
841};
842
843
844
845
846
847// ------------------------------------------------------------
851namespace DenseMatrices
852{
853
859
868
869}
870
871
872
873using namespace DenseMatrices;
874
875// The PETSc Lapack wrappers are only for PetscScalar, therefore we
876// can't e.g. get a Lapack version of DenseMatrix<Real>::lu_solve()
877// when libmesh/PETSc are compiled with complex numbers.
878#if defined(LIBMESH_HAVE_PETSC) && \
879 defined(LIBMESH_USE_REAL_NUMBERS) && \
880 defined(LIBMESH_DEFAULT_DOUBLE_PRECISION)
881template <>
882struct DenseMatrix<double>::UseBlasLapack
883{
884 static const bool value = true;
885};
886#endif
887
888
889// ------------------------------------------------------------
890// Dense Matrix member functions
891template<typename T>
892inline
893DenseMatrix<T>::DenseMatrix(const unsigned int new_m,
894 const unsigned int new_n) :
895 DenseMatrixBase<T>(new_m,new_n),
896 use_blas_lapack(DenseMatrix<T>::UseBlasLapack::value),
897 _val(),
898 _decomposition_type(NONE)
899{
900 this->resize(new_m,new_n);
901}
902
903template <typename T>
904template <typename T2>
906 unsigned int ncol,
907 std::initializer_list<T2> init_list) :
908 DenseMatrixBase<T>(nrow, ncol),
909 use_blas_lapack(DenseMatrix<T>::UseBlasLapack::value),
910 _val(init_list.begin(), init_list.end()),
911 _decomposition_type(NONE)
912{
913 // Make sure the user passed us an amount of data which is
914 // consistent with the size of the matrix.
915 libmesh_assert_equal_to(nrow * ncol, init_list.size());
916}
917
918
919
920template<typename T>
921inline
923{
924 using std::swap;
925 swap(this->_m, other_matrix._m);
926 swap(this->_n, other_matrix._n);
927 _val.swap(other_matrix._val);
928 DecompositionType _temp = _decomposition_type;
929 _decomposition_type = other_matrix._decomposition_type;
930 other_matrix._decomposition_type = _temp;
931}
932
933
934template <typename T>
935template <typename T2>
936inline
939{
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);
945
946 return *this;
947}
948
949
950
951template<typename T>
952inline
953void DenseMatrix<T>::resize(const unsigned int new_m,
954 const unsigned int new_n)
955{
956 _val.resize(new_m*new_n);
957
958 this->_m = new_m;
959 this->_n = new_n;
960
961 // zero and set decomposition_type to NONE
962 this->zero();
963}
964
965
966
967template<typename T>
968inline
970{
971 _decomposition_type = NONE;
972
973 std::fill (_val.begin(), _val.end(), static_cast<T>(0));
974}
975
976
977
978template<typename T>
979inline
980DenseMatrix<T> DenseMatrix<T>::sub_matrix(unsigned int row_id, unsigned int row_size,
981 unsigned int col_id, unsigned int col_size) const
982{
983 libmesh_assert_less (row_id + row_size - 1, this->_m);
984 libmesh_assert_less (col_id + col_size - 1, this->_n);
985
986 DenseMatrix<T> sub;
987 sub._m = row_size;
988 sub._n = col_size;
989 sub._val.resize(row_size * col_size);
990
991 unsigned int end_col = this->_n - col_size - col_id;
992 unsigned int p = row_id * this->_n;
993 unsigned int q = 0;
994 for (unsigned int i=0; i<row_size; i++)
995 {
996 // skip the beginning columns
997 p += col_id;
998 for (unsigned int j=0; j<col_size; j++)
999 sub._val[q++] = _val[p++];
1000 // skip the rest columns
1001 p += end_col;
1002 }
1003
1004 return sub;
1005}
1006
1007
1008
1009template<typename T>
1010inline
1011T DenseMatrix<T>::operator () (const unsigned int i,
1012 const unsigned int j) const
1013{
1014 libmesh_assert_less (i*j, _val.size());
1015 libmesh_assert_less (i, this->_m);
1016 libmesh_assert_less (j, this->_n);
1017
1018
1019 // return _val[(i) + (this->_m)*(j)]; // col-major
1020 return _val[(i)*(this->_n) + (j)]; // row-major
1021}
1022
1023
1024
1025template<typename T>
1026inline
1027T & DenseMatrix<T>::operator () (const unsigned int i,
1028 const unsigned int j)
1029{
1030 libmesh_assert_less (i*j, _val.size());
1031 libmesh_assert_less (i, this->_m);
1032 libmesh_assert_less (j, this->_n);
1033
1034 //return _val[(i) + (this->_m)*(j)]; // col-major
1035 return _val[(i)*(this->_n) + (j)]; // row-major
1036}
1037
1038
1039
1040
1041
1042template<typename T>
1043inline
1044void DenseMatrix<T>::scale (const T factor)
1045{
1046 for (auto & v : _val)
1047 v *= factor;
1048}
1049
1050
1051template<typename T>
1052inline
1053void DenseMatrix<T>::scale_column (const unsigned int col, const T factor)
1054{
1055 for (auto i : make_range(this->m()))
1056 (*this)(i, col) *= factor;
1057}
1058
1059
1060
1061template<typename T>
1062inline
1064{
1065 this->scale(factor);
1066 return *this;
1067}
1068
1069
1070
1071template<typename T>
1072template<typename T2, typename T3>
1073inline
1074typename std::enable_if<
1075 ScalarTraits<T2>::value, void >::type
1076DenseMatrix<T>::add (const T2 factor,
1077 const DenseMatrix<T3> & mat)
1078{
1079 libmesh_assert_equal_to (this->m(), mat.m());
1080 libmesh_assert_equal_to (this->n(), mat.n());
1081
1082 for (auto i : make_range(this->m()))
1083 for (auto j : make_range(this->n()))
1084 (*this)(i,j) += factor * mat(i,j);
1085}
1086
1087
1088
1089template<typename T>
1090inline
1092{
1093 for (auto i : index_range(_val))
1094 if (_val[i] != mat._val[i])
1095 return false;
1096
1097 return true;
1098}
1099
1100
1101
1102template<typename T>
1103inline
1105{
1106 for (auto i : index_range(_val))
1107 if (_val[i] != mat._val[i])
1108 return true;
1109
1110 return false;
1111}
1112
1113
1114
1115template<typename T>
1116inline
1118{
1119 for (auto i : index_range(_val))
1120 _val[i] += mat._val[i];
1121
1122 return *this;
1123}
1124
1125
1126
1127template<typename T>
1128inline
1130{
1131 for (auto i : index_range(_val))
1132 _val[i] -= mat._val[i];
1133
1134 return *this;
1135}
1136
1137
1138
1139template<typename T>
1140inline
1141auto DenseMatrix<T>::min () const -> decltype(libmesh_real(T(0)))
1142{
1143 libmesh_assert (this->_m);
1144 libmesh_assert (this->_n);
1145 typedef decltype(libmesh_real(T(0))) realfromT;
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);});
1150}
1151
1152
1153
1154template<typename T>
1155inline
1156auto DenseMatrix<T>::max () const -> decltype(libmesh_real(T(0)))
1157{
1158 libmesh_assert (this->_m);
1159 libmesh_assert (this->_n);
1160 typedef decltype(libmesh_real(T(0))) realfromT;
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);});
1165}
1166
1167
1168
1169template<typename T>
1170inline
1172{
1173 libmesh_assert (this->_m);
1174 libmesh_assert (this->_n);
1175
1176 using std::abs;
1177 auto columnsum = abs(T(0));
1178 for (unsigned int i=0; i!=this->_m; i++)
1179 {
1180 columnsum += abs((*this)(i,0));
1181 }
1182 auto my_max = columnsum;
1183 for (unsigned int j=1; j!=this->_n; j++)
1184 {
1185 columnsum = 0.;
1186 for (unsigned int i=0; i!=this->_m; i++)
1187 {
1188 columnsum += abs((*this)(i,j));
1189 }
1190 my_max = (my_max > columnsum? my_max : columnsum);
1191 }
1192 return my_max;
1193}
1194
1195
1196
1197template<typename T>
1198inline
1200{
1201 libmesh_assert (this->_m);
1202 libmesh_assert (this->_n);
1203 using std::abs;
1204
1205 auto rowsum = abs(T(0));
1206 for (unsigned int j=0; j!=this->_n; j++)
1207 {
1208 rowsum += abs((*this)(0,j));
1209 }
1210 auto my_max = rowsum;
1211 for (unsigned int i=1; i!=this->_m; i++)
1212 {
1213 rowsum = 0.;
1214 for (unsigned int j=0; j!=this->_n; j++)
1215 {
1216 rowsum += abs((*this)(i,j));
1217 }
1218 my_max = (my_max > rowsum? my_max : rowsum);
1219 }
1220 return my_max;
1221}
1222
1223
1224
1225template<typename T>
1226inline
1227T DenseMatrix<T>::transpose (const unsigned int i,
1228 const unsigned int j) const
1229{
1230 // Implement in terms of operator()
1231 return (*this)(j,i);
1232}
1233
1234
1235
1236
1237
1238// template<typename T>
1239// inline
1240// void DenseMatrix<T>::condense(const unsigned int iv,
1241// const unsigned int jv,
1242// const T val,
1243// DenseVector<T> & rhs)
1244// {
1245// libmesh_assert_equal_to (this->_m, rhs.size());
1246// libmesh_assert_equal_to (iv, jv);
1247
1248
1249// // move the known value into the RHS
1250// // and zero the column
1251// for (auto i : make_range(this->m()))
1252// {
1253// rhs(i) -= ((*this)(i,jv))*val;
1254// (*this)(i,jv) = 0.;
1255// }
1256
1257// // zero the row
1258// for (auto j : make_range(this->n()))
1259// (*this)(iv,j) = 0.;
1260
1261// (*this)(iv,jv) = 1.;
1262// rhs(iv) = val;
1263
1264// }
1265
1266
1267} // namespace libMesh
1268
1269#ifdef LIBMESH_HAVE_METAPHYSICL
1271{
1272template <typename T>
1273struct RawType<libMesh::DenseMatrix<T>>
1274{
1276
1278 {
1279 const auto m = in.m(), n = in.n();
1280 value_type ret(m, 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));
1284
1285 return ret;
1286 }
1287};
1288}
1289#endif
1290
1291
1292#endif // LIBMESH_DENSE_MATRIX_H
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.
auto linfty_norm() const
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.
Defines a dense vector for use in Finite Element-type computations.
static const Real b
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...
Definition int_range.h:153
LIBMESH_DEVICE_INLINE bool isfinite(std::complex< T > a)
LIBMESH_DEVICE_INLINE bool isnan(std::complex< T > a)
libmesh_assert(ctx)
const Number zero
.
Definition libmesh.h:297
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...
Definition int_range.h:176
T libmesh_transform_reduce(InputIt begin, InputIt end, T init, BinaryOp reduce, UnaryOp transform)
static value_type value(const libMesh::DenseMatrix< T > &in)
libMesh::DenseMatrix< typename RawType< T >::value_type > value_type
Helper structure for determining whether to use blas_lapack.
static const bool value
Definition xdr_io.C:55