33#include "../DenseMat.hpp"
36 static constexpr char TRAN =
'N';
38 int solve_trs(Mat<T>&, Mat<T>&&);
46 FullMat(
const uword in_rows,
const uword in_cols)
47 :
DenseMat<
T>(in_rows, in_cols, in_rows * in_cols) {}
49 unique_ptr<MetaMat<T>>
make_copy()
override {
return std::make_unique<FullMat>(*
this); }
57 T operator()(
const uword in_row,
const uword in_col)
const override {
return this->
memory[in_row + in_col * this->
n_rows]; }
59 T&
at(
const uword in_row,
const uword in_col)
override {
64 Mat<T>
operator*(
const Mat<T>&)
const override;
68 for(
unsigned I = 0; I < this->
pivot.n_elem; ++I)
69 if((this->
operator()(I, I) <
T(0)) ^ (
static_cast<int>(I) + 1 != this->
pivot(I))) det_sign = -det_sign;
75 Mat<T> C(arma::size(B));
77 const auto M =
static_cast<blas_int
>(this->n_rows);
78 const auto N =
static_cast<blas_int
>(this->n_cols);
80 T ALPHA =
T(1), BETA =
T(0);
83 constexpr blas_int INC = 1;
85 if constexpr(std::is_same_v<T, float>) {
87 arma_fortran(arma_sgemv)(&TRAN, &
M, &
N, (
E*)&ALPHA, (
E*)this->memptr(), &
M, (
E*)B.memptr(), &INC, (
E*)&BETA, (
E*)C.memptr(), &INC);
91 arma_fortran(arma_dgemv)(&TRAN, &
M, &
N, (
E*)&ALPHA, (
E*)this->memptr(), &
M, (
E*)B.memptr(), &INC, (
E*)&BETA, (
E*)C.memptr(), &INC);
95 const auto K =
static_cast<blas_int
>(B.n_cols);
97 if constexpr(std::is_same_v<T, float>) {
99 arma_fortran(arma_sgemm)(&TRAN, &TRAN, &
M, &
K, &
N, (
E*)&ALPHA, (
E*)this->memptr(), &
M, (
E*)B.memptr(), &
N, (
E*)&BETA, (
E*)C.memptr(), &
M);
103 arma_fortran(arma_dgemm)(&TRAN, &TRAN, &
M, &
K, &
N, (
E*)&ALPHA, (
E*)this->memptr(), &
M, (
E*)B.memptr(), &
N, (
E*)&BETA, (
E*)C.memptr(), &
M);
111 if(this->factored)
return this->solve_trs(X, std::move(B));
113 suanpan_assert([&] {
if(this->n_rows != this->n_cols)
throw invalid_argument(
"requires a square matrix"); });
117 auto N =
static_cast<blas_int
>(this->n_rows);
118 const auto NRHS =
static_cast<blas_int
>(B.n_cols);
119 const auto LDB =
static_cast<blas_int
>(B.n_rows);
120 this->pivot.zeros(
N);
121 this->factored =
true;
123 if constexpr(std::is_same_v<T, float>) {
125 arma_fortran(arma_sgesv)(&
N, &NRHS, (
E*)this->memptr(), &
N, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
130 arma_fortran(arma_dgesv)(&
N, &NRHS, (
E*)this->memptr(), &
N, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
134 this->s_memory = this->to_float();
135 arma_fortran(arma_sgetrf)(&
N, &
N, this->s_memory.memptr(), &
N, this->pivot.memptr(), &INFO);
136 if(0 == INFO) INFO = this->solve_trs(X, std::move(B));
140 suanpan_error(
"Error code {} received, the matrix is probably singular.\n", INFO);
148 const auto N =
static_cast<blas_int
>(this->n_rows);
149 const auto NRHS =
static_cast<blas_int
>(B.n_cols);
150 const auto LDB =
static_cast<blas_int
>(B.n_rows);
152 if constexpr(std::is_same_v<T, float>) {
154 arma_fortran(arma_sgetrs)(&TRAN, &
N, &NRHS, (
E*)this->memptr(), &
N, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
159 arma_fortran(arma_dgetrs)(&TRAN, &
N, &NRHS, (
E*)this->memptr(), &
N, this->pivot.memptr(), (
E*)B.memptr(), &LDB, &INFO);
163 this->mixed_trs(X, std::move(B), [&](fmat& residual) {
164 arma_fortran(arma_sgetrs)(&TRAN, &
N, &NRHS, this->s_memory.memptr(), &
N, this->pivot.memptr(), residual.memptr(), &LDB, &INFO);
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
A FullMat class that holds matrices.
Definition FullMat.hpp:35
int sign_det() const override
Definition FullMat.hpp:66
FullMat(const uword in_rows, const uword in_cols)
Definition FullMat.hpp:46
T operator()(const uword in_row, const uword in_col) const override
Access element (read-only), returns zero if out-of-bound.
Definition FullMat.hpp:57
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition FullMat.hpp:59
void nullify(const uword K) override
Definition FullMat.hpp:51
unique_ptr< MetaMat< T > > make_copy() override
Definition FullMat.hpp:49
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:360
#define suanpan_error(...)
Definition suanPan.h:373