ARPACK-Armadillo
GeneralLU.h
1 // Copyright (C) 2015 Yixuan Qiu
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef GENERAL_LU_H
8 #define GENERAL_LU_H
9 
10 #include <armadillo>
11 #include <stdexcept>
12 #include "LapackWrapperExtra.h"
13 
19 
29 template <typename Scalar = double>
30 class GeneralLU
31 {
32 private:
33  typedef arma::Mat<Scalar> Matrix;
34  typedef arma::Col<Scalar> Vector;
35  typedef arma::Col<int> IntVector;
36 
37  int dim_n; // size of the matrix
38  Matrix mat_fac; // storing factorization structures
39  IntVector vec_fac; // storing factorization structures
40  bool computed; // whether factorization has been computed
41 
42 public:
49  dim_n(0), computed(false)
50  {}
51 
59  GeneralLU(const Matrix &mat) :
60  dim_n(mat.n_rows),
61  computed(false)
62  {
63  compute(mat);
64  }
65 
72  void compute(const Matrix &mat)
73  {
74  if(!mat.is_square())
75  throw std::invalid_argument("GeneralLU: matrix must be square");
76 
77  dim_n = mat.n_rows;
78  mat_fac = mat;
79  vec_fac.set_size(dim_n);
80 
81  int info;
82  arma::lapack::getrf(&dim_n, &dim_n, mat_fac.memptr(), &dim_n,
83  vec_fac.memptr(), &info);
84 
85  if(info < 0)
86  throw std::invalid_argument("Lapack getrf: illegal value");
87  if(info > 0)
88  throw std::logic_error("GeneralLU: matrix is singular");
89 
90  computed = true;
91  }
92 
104  void solve(Vector &vec_in, Vector &vec_out)
105  {
106  if(!computed)
107  return;
108 
109  vec_out = vec_in;
110 
111  int one = 1;
112  char no_trans = 'N';
113  int info;
114  arma::lapack::getrs(&no_trans, &dim_n, &one, mat_fac.memptr(), &dim_n,
115  vec_fac.memptr(), vec_out.memptr(), &dim_n, &info);
116  if(info < 0)
117  throw std::invalid_argument("Lapack getrs: illegal value");
118  }
119 };
120 
121 
122 
123 #endif // GENERAL_LU_H
void compute(const Matrix &mat)
Definition: GeneralLU.h:72
void solve(Vector &vec_in, Vector &vec_out)
Definition: GeneralLU.h:104
GeneralLU()
Definition: GeneralLU.h:48
GeneralLU(const Matrix &mat)
Definition: GeneralLU.h:59