29 #ifndef TOON_INCLUDE_LU_H 30 #define TOON_INCLUDE_LU_H 34 #include <TooN/TooN.h> 35 #include <TooN/lapack.h> 66 template <
int Size=-1,
class Precision=
double>
72 template<
int S1,
int S2,
class Base>
74 :my_lu(m.num_rows(),m.num_cols()),my_IPIV(m.num_rows()){
79 template<
int S1,
int S2,
class Base>
87 FortranInteger lda = m.num_rows();
88 FortranInteger M = m.num_rows();
89 FortranInteger N = m.num_rows();
91 getrf_(&M,&N,&my_lu[0][0],&lda,&my_IPIV[0],&my_info);
94 std::cerr <<
"error in LU, INFO was " << my_info << std::endl;
100 template <
int Rows,
int NRHS,
class Base>
107 FortranInteger M=rhs.num_cols();
108 FortranInteger N=my_lu.num_rows();
110 FortranInteger lda=my_lu.num_rows();
111 FortranInteger ldb=rhs.num_cols();
112 trsm_(
"R",
"U",
"N",
"N",&M,&N,&alpha,&my_lu[0][0],&lda,&result[0][0],&ldb);
113 trsm_(
"R",
"L",
"N",
"U",&M,&N,&alpha,&my_lu[0][0],&lda,&result[0][0],&ldb);
116 for(
int i=N-1; i>=0; i--){
117 const int swaprow = my_IPIV[i]-1;
118 for(
int j=0; j<NRHS; j++){
119 Precision temp = result[i][j];
120 result[i][j] = result[swaprow][j];
121 result[swaprow][j] = temp;
129 template <
int Rows,
class Base>
137 FortranInteger N=my_lu.num_rows();
139 FortranInteger lda=my_lu.num_rows();
140 FortranInteger ldb=1;
141 trsm_(
"R",
"U",
"N",
"N",&M,&N,&alpha,&my_lu[0][0],&lda,&result[0],&ldb);
142 trsm_(
"R",
"L",
"N",
"U",&M,&N,&alpha,&my_lu[0][0],&lda,&result[0],&ldb);
145 for(
int i=N-1; i>=0; i--){
146 const int swaprow = my_IPIV[i]-1;
147 Precision temp = result[i];
148 result[i] = result[swaprow];
149 result[swaprow] = temp;
158 FortranInteger N = my_lu.num_rows();
159 FortranInteger lda=my_lu.num_rows();
160 FortranInteger lwork=-1;
162 getri_(&N, &Inverse[0][0], &lda, &my_IPIV[0], &size, &lwork, &my_info);
163 lwork=FortranInteger(size);
164 Precision* WORK =
new Precision[lwork];
165 getri_(&N, &Inverse[0][0], &lda, &my_IPIV[0], WORK, &lwork, &my_info);
178 inline int get_sign()
const {
180 for(
int i=0; i<my_lu.num_rows()-1; i++){
181 if(my_IPIV[i] > i+1){
191 Precision result = get_sign();
192 for (
int i=0; i<my_lu.num_rows(); i++){
204 FortranInteger my_info;
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A matrix.
Definition: matrix.hh:105
Matrix< Size, Size, Precision > get_inverse()
Calculate inverse of the matrix.
Definition: LU.h:156
LU(const Matrix< S1, S2, Precision, Base > &m)
Construct the LU decomposition of a matrix.
Definition: LU.h:73
Performs LU decomposition and back substitutes to solve equations.
Definition: LU.h:67
Matrix< Size, NRHS, Precision > backsub(const Matrix< Rows, NRHS, Precision, Base > &rhs)
Calculate result of multiplying the inverse of M by another matrix.
Definition: LU.h:101
Precision determinant() const
Calculate the determinant of the matrix.
Definition: LU.h:190
Vector< Size, Precision > backsub(const Vector< Rows, Precision, Base > &rhs)
Calculate result of multiplying the inverse of M by a vector.
Definition: LU.h:130
void compute(const Matrix< S1, S2, Precision, Base > &m)
Perform the LU decompsition of another matrix.
Definition: LU.h:80
Definition: size_mismatch.hh:103
int get_info() const
Get the LAPACK info.
Definition: LU.h:199
const Matrix< Size, Size, Precision > & get_lu() const
Returns the L and U matrices.
Definition: LU.h:175