33#include "../DenseMat.hpp"
36 static constexpr char TRAN =
'N';
42 int solve_trs(Mat<T>&, Mat<T>&&);
55 BandMat(
const uword in_size,
const uword in_l,
const uword in_u)
56 :
DenseMat<
T>(in_size, in_size, (2 * in_l + in_u + 1) * in_size)
58 ,
m_rows(2 * in_l + in_u + 1)
62 suanpan_warning(
"The storage requirement for the banded matrix is larger than that of a full matrix, consider using a full/sparse matrix instead.\n");
65 unique_ptr<MetaMat<T>>
make_copy()
override {
return std::make_unique<BandMat>(*
this); }
73 T operator()(
const uword in_row,
const uword in_col)
const override {
74 if(in_row > in_col +
l_band || in_row +
u_band < in_col) [[unlikely]]
return bin =
T(0);
75 return this->
memory[in_row + s_band + in_col * (
m_rows - 1)];
78 T&
unsafe_at(
const uword in_row,
const uword in_col)
override {
80 return this->
memory[in_row + s_band + in_col * (
m_rows - 1)];
83 T&
at(
const uword in_row,
const uword in_col)
override {
84 if(in_row > in_col +
l_band || in_row +
u_band < in_col) [[unlikely]]
return bin =
T(0);
88 Mat<T>
operator*(
const Mat<T>&)
const override;
92 for(
unsigned I = 0; I < this->
pivot.n_elem; ++I)
93 if((this->
operator()(I, I) <
T(0)) ^ (
static_cast<int>(I) + 1 != this->
pivot(I))) det_sign = -det_sign;
101 Mat<T> Y(arma::size(X));
103 const auto M =
static_cast<blas_int
>(this->n_rows);
104 const auto N =
static_cast<blas_int
>(this->n_cols);
105 const auto KL =
static_cast<blas_int
>(l_band);
106 const auto KU =
static_cast<blas_int
>(u_band);
107 const auto LDA =
static_cast<blas_int
>(m_rows);
108 constexpr blas_int INC = 1;
112 if constexpr(std::is_same_v<T, float>) {
114 suanpan::for_each(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); });
118 suanpan::for_each(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); });
125 if(this->factored)
return this->solve_trs(X, std::move(B));
127 suanpan_assert([&] {
if(this->n_rows != this->n_cols)
throw invalid_argument(
"requires a square matrix"); });
131 auto N =
static_cast<blas_int
>(this->n_rows);
132 const auto KL =
static_cast<blas_int
>(l_band);
133 const auto KU =
static_cast<blas_int
>(u_band);
134 const auto NRHS =
static_cast<blas_int
>(B.n_cols);
135 const auto LDAB =
static_cast<blas_int
>(m_rows);
136 const auto LDB =
static_cast<blas_int
>(B.n_rows);
137 this->pivot.zeros(
N);
138 this->factored =
true;
140 if constexpr(std::is_same_v<T, float>) {
142 arma_fortran(arma_sgbsv)(&
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
147 arma_fortran(arma_dgbsv)(&
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
151 this->s_memory = this->to_float();
152 arma_fortran(arma_sgbtrf)(&
N, &
N, &KL, &KU, this->s_memory.memptr(), &LDAB, this->pivot.memptr(), &INFO);
153 if(0 == INFO) INFO = this->solve_trs(X, std::move(B));
157 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
165 const auto N =
static_cast<blas_int
>(this->n_rows);
166 const auto KL =
static_cast<blas_int
>(l_band);
167 const auto KU =
static_cast<blas_int
>(u_band);
168 const auto NRHS =
static_cast<blas_int
>(B.n_cols);
169 const auto LDAB =
static_cast<blas_int
>(m_rows);
170 const auto LDB =
static_cast<blas_int
>(B.n_rows);
172 if constexpr(std::is_same_v<T, float>) {
174 arma_fortran(arma_sgbtrs)(&TRAN, &
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
179 arma_fortran(arma_dgbtrs)(&TRAN, &
N, &KL, &KU, &NRHS, (
E*)this->memptr(), &LDAB, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
183 this->mixed_trs(X, std::move(B), [&](fmat& residual) {
184 arma_fortran(arma_sgbtrs)(&TRAN, &
N, &KL, &KU, &NRHS, this->s_memory.memptr(), &LDAB, this->pivot.memptr(), residual.memptr(), &LDB, &INFO);
189 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:78
BandMat(const uword in_size, const uword in_l, const uword in_u)
Definition BandMat.hpp:55
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition BandMat.hpp:83
const uword u_band
Definition BandMat.hpp:48
unique_ptr< MetaMat< T > > make_copy() override
Definition BandMat.hpp:65
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:73
const uword l_band
Definition BandMat.hpp:47
const uword m_rows
Definition BandMat.hpp:45
int sign_det() const override
Definition BandMat.hpp:90
void nullify(const uword K) override
Definition BandMat.hpp:67
A DenseMat class that holds matrices.
Definition DenseMat.hpp:39
podarray< blas_int > pivot
Definition DenseMat.hpp:45
std::unique_ptr< T[]> memory
Definition DenseMat.hpp:48
void for_each(const IT start, const IT end, F &&FN)
Definition utility.h:28
#define suanpan_warning(...)
Definition suanPan.h:372
void suanpan_assert(const std::function< void()> &F)
Definition suanPan.h:360
#define suanpan_error(...)
Definition suanPan.h:373