30#ifndef BANDSYMMMAT_HPP
31#define BANDSYMMMAT_HPP
36 static constexpr char UPLO =
'L';
43 int solve_trs(Mat<T>&, Mat<T>&&);
52 :
DenseMat<
T>(in_size, in_size, (in_bandwidth + 1) * in_size)
54 , m_rows(in_bandwidth + 1) {}
56 unique_ptr<MetaMat<T>>
make_copy()
override {
return std::make_unique<BandSymmMat>(*
this); }
61 const auto t_factor =
K * m_rows -
K;
65 T operator()(
const uword in_row,
const uword in_col)
const override {
66 if(in_row > band + in_col || in_col > in_row + band) [[unlikely]]
return bin =
T(0);
67 return this->
memory[in_row > in_col ? in_row - in_col + in_col * m_rows : in_col - in_row + in_row * m_rows];
70 T&
unsafe_at(
const uword in_row,
const uword in_col)
override {
72 return this->
memory[in_row - in_col + in_col * m_rows];
75 T&
at(
const uword in_row,
const uword in_col)
override {
76 if(in_row > band + in_col || in_row < in_col) [[unlikely]]
return bin =
T(0);
80 Mat<T>
operator*(
const Mat<T>&)
const override;
86 Mat<T> Y(arma::size(X));
88 const auto N =
static_cast<int>(this->n_cols);
89 const auto K =
static_cast<int>(band);
90 const auto LDA =
static_cast<int>(m_rows);
91 constexpr auto INC = 1;
95 if constexpr(std::is_same_v<T, float>) {
97 suanpan::for_each(X.n_cols, [&](
const uword I) { arma_fortran(arma_ssbmv)(&UPLO, &N, &K, (E*)&ALPHA, (E*)this->memptr(), &LDA, (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
101 suanpan::for_each(X.n_cols, [&](
const uword I) { arma_fortran(arma_dsbmv)(&UPLO, &N, &K, (E*)&ALPHA, (E*)this->memptr(), &LDA, (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
108 if(this->factored)
return this->solve_trs(X, std::forward<Mat<T>>(B));
110 suanpan_assert([&] {
if(this->n_rows != this->n_cols)
throw invalid_argument(
"requires a square matrix"); });
114 const auto N =
static_cast<int>(this->n_rows);
115 const auto KD =
static_cast<int>(band);
116 const auto NRHS =
static_cast<int>(B.n_cols);
117 const auto LDAB =
static_cast<int>(m_rows);
118 const auto LDB =
static_cast<int>(B.n_rows);
119 this->factored =
true;
121 if constexpr(std::is_same_v<T, float>) {
123 arma_fortran(arma_spbsv)(&UPLO, &
N, &KD, &NRHS, (
E*)this->memptr(), &LDAB, (
E*)B.memptr(), &LDB, &INFO);
128 arma_fortran(arma_dpbsv)(&UPLO, &
N, &KD, &NRHS, (
E*)this->memptr(), &LDAB, (
E*)B.memptr(), &LDB, &INFO);
132 this->s_memory = this->to_float();
133 arma_fortran(arma_spbtrf)(&UPLO, &
N, &KD, this->s_memory.memptr(), &LDAB, &INFO);
134 if(0 == INFO) INFO = this->solve_trs(X, std::forward<Mat<T>>(B));
138 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
146 const auto N =
static_cast<int>(this->n_rows);
147 const auto KD =
static_cast<int>(band);
148 const auto NRHS =
static_cast<int>(B.n_cols);
149 const auto LDAB =
static_cast<int>(m_rows);
150 const auto LDB =
static_cast<int>(B.n_rows);
152 if constexpr(std::is_same_v<T, float>) {
154 arma_fortran(arma_spbtrs)(&UPLO, &
N, &KD, &NRHS, (
E*)this->memptr(), &LDAB, (
E*)B.memptr(), &LDB, &INFO);
159 arma_fortran(arma_dpbtrs)(&UPLO, &
N, &KD, &NRHS, (
E*)this->memptr(), &LDAB, (
E*)B.memptr(), &LDB, &INFO);
163 this->mixed_trs(X, std::forward<Mat<T>>(B), [&](fmat& residual) {
164 arma_fortran(arma_spbtrs)(&UPLO, &
N, &KD, &NRHS, this->s_memory.memptr(), &LDAB, residual.memptr(), &LDB, &INFO);
169 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
A BandSymmMat class that holds matrices.
Definition: BandSymmMat.hpp:35
T operator()(const uword in_row, const uword in_col) const override
Access element (read-only), returns zero if out-of-bound.
Definition: BandSymmMat.hpp:65
BandSymmMat(const uword in_size, const uword in_bandwidth)
Definition: BandSymmMat.hpp:51
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition: BandSymmMat.hpp:75
unique_ptr< MetaMat< T > > make_copy() override
Definition: BandSymmMat.hpp:56
void nullify(const uword K) override
Definition: BandSymmMat.hpp:58
T & unsafe_at(const uword in_row, const uword in_col) override
Access element without bound check.
Definition: BandSymmMat.hpp:70
A DenseMat class that holds matrices.
Definition: DenseMat.hpp:39
std::unique_ptr< T[]> memory
Definition: DenseMat.hpp:48
void for_each(const IT start, const IT end, F &&FN)
Definition: utility.h:28
void suanpan_assert(const std::function< void()> &F)
Definition: suanPan.h:296
#define suanpan_error(...)
Definition: suanPan.h:309