https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Functions
MatrixTools Namespace Reference

Functions

void inverse (const std::vector< std::vector< Real > > &m, std::vector< std::vector< Real > > &m_inv)
 Inverse the dense square matrix m using LAPACK routines.
 
void inverse (std::vector< PetscScalar > &A, unsigned int n)
 Inverts the dense "matrix" A using LAPACK routines.
 

Function Documentation

◆ inverse() [1/2]

void MatrixTools::inverse ( const std::vector< std::vector< Real > > &  m,
std::vector< std::vector< Real > > &  m_inv 
)

Inverse the dense square matrix m using LAPACK routines.

If you need to invert a matrix "in place", make the two arguments the same @ param m The matrix to invert @ param[out] m_inv The inverse of m, which must be of the same size as m. @ return if zero then the inversion was successful. Otherwise m was not square, contained illegal entries or was singular

Definition at line 23 of file MatrixTools.C.

24{
25 unsigned int n = m.size();
26
27 // check the matrix m exists and is square
28 if (n == 0)
29 throw MooseException("Input matrix empty during matrix inversion.");
30 if (n != m_inv.size() || n != m[0].size() || n != m_inv[0].size())
31 throw MooseException("Input and output matrix are not same size square matrices.");
32
33 // build the vectorial representation
34 std::vector<PetscScalar> A;
35 for (const auto & rowvec : m)
36 for (const auto & matrix_entry : rowvec)
37 A.push_back(matrix_entry);
38
39 inverse(A, n);
40
41 // build the inverse
42 unsigned int i = 0;
43 for (auto & rowvec : m_inv)
44 for (auto & inv_entry : rowvec)
45 inv_entry = A[i++];
46}
for(PetscInt i=0;i< nvars;++i)
Provides a way for users to bail out of the current solve.
void inverse(const std::vector< std::vector< Real > > &m, std::vector< std::vector< Real > > &m_inv)
Inverse the dense square matrix m using LAPACK routines.
Definition MatrixTools.C:23

Referenced by inverse(), and RankFourTensorTempl< T >::invSymm().

◆ inverse() [2/2]

void MatrixTools::inverse ( std::vector< PetscScalar > &  A,
unsigned int  n 
)

Inverts the dense "matrix" A using LAPACK routines.

Parameters
Aupon input this is a row vector representing a square matrix of size sqrt(n)*sqrt(n). Upon output it is the inverse (as a row-vector)
nsize of the vector A
Returns
if zero then inversion was successful. Otherwise A contained illegal entries or was singular

Definition at line 49 of file MatrixTools.C.

50{
51 mooseAssert(n >= 1, "MatrixTools::inverse - n (leading dimension) needs to be positive");
52 mooseAssert(n <= std::numeric_limits<unsigned int>::max(),
53 "MatrixTools::inverse - n (leading dimension) too large");
54
55 std::vector<PetscBLASInt> ipiv(n);
56 std::vector<PetscScalar> buffer(n * 64);
57
58 // Following does a LU decomposition of "square matrix A"
59 // upon return "A = P*L*U" if return_value == 0
60 // Here I use quotes because A is actually an array of length n^2, not a matrix of size n-by-n
61 PetscBLASInt return_value;
62 LAPACKgetrf_(reinterpret_cast<PetscBLASInt *>(&n),
63 reinterpret_cast<PetscBLASInt *>(&n),
64 &A[0],
65 reinterpret_cast<PetscBLASInt *>(&n),
66 &ipiv[0],
67 &return_value);
68
69 if (return_value != 0)
70 throw MooseException(
71 return_value < 0
72 ? "Argument " + Moose::stringify(-return_value) +
73 " was invalid during LU factorization in MatrixTools::inverse."
74 : "Matrix on-diagonal entry " + Moose::stringify(return_value) +
75 " was exactly zero during LU factorization in MatrixTools::inverse.");
76
77 // get the inverse of A
78 PetscBLASInt buffer_size = buffer.size();
79 LAPACKgetri_(reinterpret_cast<PetscBLASInt *>(&n),
80 &A[0],
81 reinterpret_cast<PetscBLASInt *>(&n),
82 &ipiv[0],
83 &buffer[0],
84 &buffer_size,
85 &return_value);
86
87 if (return_value != 0)
88 throw MooseException(return_value < 0
89 ? "Argument " + Moose::stringify(-return_value) +
90 " was invalid during invert in MatrixTools::inverse."
91 : "Matrix on-diagonal entry " + Moose::stringify(return_value) +
92 " was exactly zero during invert in MatrixTools::inverse.");
93}
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64