36 static constexpr char TRAN =
'N';
43 int solve_trs(Mat<T>&, Mat<T>&&);
54 BandMat(
const uword in_size,
const uword in_l,
const uword in_u)
55 :
DenseMat<
T>(in_size, in_size, (2 * in_l + in_u + 1) * in_size)
57 , m_rows(2 * in_l + in_u + 1)
61 unique_ptr<MetaMat<T>>
make_copy()
override {
return std::make_unique<BandMat>(*
this); }
69 T operator()(
const uword in_row,
const uword in_col)
const override {
70 if(in_row > in_col +
l_band || in_row +
u_band < in_col) [[unlikely]]
return bin =
T(0);
71 return this->
memory[in_row + s_band + in_col * (m_rows - 1)];
74 T&
unsafe_at(
const uword in_row,
const uword in_col)
override {
76 return this->
memory[in_row + s_band + in_col * (m_rows - 1)];
79 T&
at(
const uword in_row,
const uword in_col)
override {
80 if(in_row > in_col +
l_band || in_row +
u_band < in_col) [[unlikely]]
return bin =
T(0);
84 Mat<T>
operator*(
const Mat<T>&)
const override;
90 Mat<T> Y(arma::size(X));
92 const auto M =
static_cast<int>(this->n_rows);
93 const auto N =
static_cast<int>(this->n_cols);
94 const auto KL =
static_cast<int>(l_band);
95 const auto KU =
static_cast<int>(u_band);
96 const auto LDA =
static_cast<int>(m_rows);
97 constexpr auto INC = 1;
101 if constexpr(std::is_same_v<T, float>) {
103 suanpan_for(0llu, X.n_cols, [&](
const uword I) { arma_fortran(arma_sgbmv)(&TRAN, &M, &N, &KL, &KU, (E*)&ALPHA, (E*)(this->memptr() + l_band), &LDA, (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
107 suanpan_for(0llu, X.n_cols, [&](
const uword I) { arma_fortran(arma_dgbmv)(&TRAN, &M, &N, &KL, &KU, (E*)&ALPHA, (E*)(this->memptr() + l_band), &LDA, (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
114 if(this->factored)
return this->solve_trs(X, std::forward<Mat<T>>(B));
116 suanpan_assert([&] {
if(this->n_rows != this->n_cols)
throw invalid_argument(
"requires a square matrix"); });
120 auto N =
static_cast<int>(this->n_rows);
121 const auto KL =
static_cast<int>(l_band);
122 const auto KU =
static_cast<int>(u_band);
123 const auto NRHS =
static_cast<int>(B.n_cols);
124 const auto LDAB =
static_cast<int>(m_rows);
125 const auto LDB =
static_cast<int>(B.n_rows);
126 this->pivot.zeros(
N);
127 this->factored =
true;
129 if constexpr(std::is_same_v<T, float>) {
131 arma_fortran(arma_sgbsv)(&
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
136 arma_fortran(arma_dgbsv)(&
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
140 this->s_memory = this->to_float();
141 arma_fortran(arma_sgbtrf)(&
N, &
N, &KL, &KU, this->s_memory.memptr(), &LDAB, this->pivot.memptr(), &INFO);
142 if(0 == INFO) INFO = this->solve_trs(X, std::forward<Mat<T>>(B));
146 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
154 const auto N =
static_cast<int>(this->n_rows);
155 const auto KL =
static_cast<int>(l_band);
156 const auto KU =
static_cast<int>(u_band);
157 const auto NRHS =
static_cast<int>(B.n_cols);
158 const auto LDAB =
static_cast<int>(m_rows);
159 const auto LDB =
static_cast<int>(B.n_rows);
161 if constexpr(std::is_same_v<T, float>) {
163 arma_fortran(arma_sgbtrs)(&TRAN, &
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
168 arma_fortran(arma_dgbtrs)(&TRAN, &
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
172 this->mixed_trs(X, std::forward<Mat<T>>(B), [&](fmat& residual) {
173 arma_fortran(arma_sgbtrs)(&TRAN, &
N, &KL, &KU, &NRHS, this->s_memory.memptr(), &LDAB, this->pivot.memptr(), residual.memptr(), &LDB, &INFO);
178 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
A BandMat class that holds matrices.
Definition BandMat.hpp:35
T & unsafe_at(const uword in_row, const uword in_col) override
Access element without bound check.
Definition BandMat.hpp:74
BandMat(const uword in_size, const uword in_l, const uword in_u)
Definition BandMat.hpp:54
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition BandMat.hpp:79
const uword u_band
Definition BandMat.hpp:47
unique_ptr< MetaMat< T > > make_copy() override
Definition BandMat.hpp:61
T operator()(const uword in_row, const uword in_col) const override
Access element (read-only), returns zero if out-of-bound.
Definition BandMat.hpp:69
const uword l_band
Definition BandMat.hpp:46
void nullify(const uword K) override
Definition BandMat.hpp:63
A DenseMat class that holds matrices.
Definition DenseMat.hpp:39
std::unique_ptr< T[]> memory
Definition DenseMat.hpp:48
void suanpan_assert(const std::function< void()> &F)
Definition suanPan.h:296
#define suanpan_error(...)
Definition suanPan.h:309
void suanpan_for(const IT start, const IT end, F &&FN)
Definition utility.h:27