30#ifndef SYMMPACKMAT_HPP
31#define SYMMPACKMAT_HPP
36 static constexpr char UPLO =
'L';
42 int solve_trs(Mat<T>&, Mat<T>&&);
51 :
DenseMat<
T>(in_size, in_size, (in_size + 1) * in_size / 2)
52 , length(2 * in_size - 1) {}
54 unique_ptr<MetaMat<T>>
make_copy()
override {
return std::make_unique<SymmPackMat>(*
this); }
59 const auto t_factor = (length -
K) *
K / 2;
63 T operator()(
const uword in_row,
const uword in_col)
const override {
return this->
memory[in_row >= in_col ? in_row + (length - in_col) * in_col / 2 : in_col + (length - in_row) * in_row / 2]; }
65 T&
unsafe_at(
const uword in_row,
const uword in_col)
override {
67 return this->
memory[in_row + (length - in_col) * in_col / 2];
70 T&
at(
const uword in_row,
const uword in_col)
override {
71 if(in_row < in_col) [[unlikely]]
return bin =
T(0);
75 Mat<T>
operator*(
const Mat<T>&)
const override;
81 auto Y = Mat<T>(arma::size(X), fill::none);
83 const auto N =
static_cast<int>(this->n_rows);
84 constexpr auto INC = 1;
88 if constexpr(std::is_same_v<T, float>) {
90 suanpan::for_each(X.n_cols, [&](
const uword I) { arma_fortran(arma_sspmv)(&UPLO, &N, (E*)&ALPHA, (E*)this->memptr(), (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
94 suanpan::for_each(X.n_cols, [&](
const uword I) { arma_fortran(arma_dspmv)(&UPLO, &N, (E*)&ALPHA, (E*)this->memptr(), (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
101 if(this->factored)
return this->solve_trs(X, std::forward<Mat<T>>(B));
103 suanpan_assert([&] {
if(this->n_rows != this->n_cols)
throw invalid_argument(
"requires a square matrix"); });
107 const auto N =
static_cast<int>(this->n_rows);
108 const auto NRHS =
static_cast<int>(B.n_cols);
109 const auto LDB =
static_cast<int>(B.n_rows);
110 this->factored =
true;
112 if constexpr(std::is_same_v<T, float>) {
114 arma_fortran(arma_sppsv)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
119 arma_fortran(arma_dppsv)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
123 this->s_memory = this->to_float();
124 arma_fortran(arma_spptrf)(&UPLO, &
N, this->s_memory.memptr(), &INFO);
125 if(0 == INFO) INFO = this->solve_trs(X, std::forward<Mat<T>>(B));
129 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
137 const auto N =
static_cast<int>(this->n_rows);
138 const auto NRHS =
static_cast<int>(B.n_cols);
139 const auto LDB =
static_cast<int>(B.n_rows);
141 if constexpr(std::is_same_v<T, float>) {
143 arma_fortran(arma_spptrs)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
148 arma_fortran(arma_dpptrs)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
152 this->mixed_trs(X, std::forward<Mat<T>>(B), [&](fmat& residual) {
153 arma_fortran(arma_spptrs)(&UPLO, &
N, &NRHS, this->s_memory.memptr(), residual.memptr(), &LDB, &INFO);
158 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
A DenseMat class that holds matrices.
Definition: DenseMat.hpp:39
std::unique_ptr< T[]> memory
Definition: DenseMat.hpp:48
A SymmPackMat class that holds matrices.
Definition: SymmPackMat.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: SymmPackMat.hpp:63
void nullify(const uword K) override
Definition: SymmPackMat.hpp:56
unique_ptr< MetaMat< T > > make_copy() override
Definition: SymmPackMat.hpp:54
SymmPackMat(const uword in_size)
Definition: SymmPackMat.hpp:50
T & unsafe_at(const uword in_row, const uword in_col) override
Access element without bound check.
Definition: SymmPackMat.hpp:65
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition: SymmPackMat.hpp:70
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