30#ifndef SYMMPACKMAT_HPP
31#define SYMMPACKMAT_HPP
33#include "../DenseMat.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]]
76 Mat<T>
operator*(
const Mat<T>&)
const override;
78 [[nodiscard]]
int sign_det()
const override {
return 1; }
84 auto Y = Mat<T>(arma::size(X), fill::none);
86 const auto N =
static_cast<blas_int
>(this->n_rows);
87 constexpr blas_int INC = 1;
91 if constexpr(std::is_same_v<T, float>) {
93 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); });
97 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); });
104 if(this->factored)
return this->solve_trs(X, std::move(B));
106 suanpan_assert([&] {
if(this->n_rows != this->n_cols)
throw std::invalid_argument(
"requires a square matrix"); });
110 const auto N =
static_cast<blas_int
>(this->n_rows);
111 const auto NRHS =
static_cast<blas_int
>(B.n_cols);
112 const auto LDB =
static_cast<blas_int
>(B.n_rows);
113 this->factored =
true;
115 if constexpr(std::is_same_v<T, float>) {
117 arma_fortran(arma_sppsv)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
122 arma_fortran(arma_dppsv)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
126 this->s_memory = this->to_float();
127 arma_fortran(arma_spptrf)(&UPLO, &
N, this->s_memory.memptr(), &INFO);
128 if(0 == INFO) INFO = this->solve_trs(X, std::move(B));
132 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
140 const auto N =
static_cast<blas_int
>(this->n_rows);
141 const auto NRHS =
static_cast<blas_int
>(B.n_cols);
142 const auto LDB =
static_cast<blas_int
>(B.n_rows);
144 if constexpr(std::is_same_v<T, float>) {
146 arma_fortran(arma_spptrs)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
151 arma_fortran(arma_dpptrs)(&UPLO, &
N, &NRHS, (
E*)this->memptr(), (
E*)B.memptr(), &LDB, &INFO);
155 this->mixed_trs(X, std::move(B), [&](fmat& residual) {
156 arma_fortran(arma_spptrs)(&UPLO, &
N, &NRHS, this->s_memory.memptr(), residual.memptr(), &LDB, &INFO);
161 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
int sign_det() const override
Definition SymmPackMat.hpp:78
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:363
#define suanpan_error(...)
Definition suanPan.h:376