suanPan
Loading...
Searching...
No Matches
BandSymmMat.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2025 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
29// ReSharper disable CppCStyleCast
30#ifndef BANDSYMMMAT_HPP
31#define BANDSYMMMAT_HPP
32
33#include "../DenseMat.hpp"
34
35template<sp_d T> class BandSymmMat final : public DenseMat<T> {
36 static constexpr char UPLO = 'L';
37
38 static T bin;
39
40 const uword band;
41 const uword m_rows; // memory block layout
42
43 int solve_trs(Mat<T>&, Mat<T>&&);
44
45protected:
47
48 int direct_solve(Mat<T>&, Mat<T>&&) override;
49
50public:
51 BandSymmMat(const uword in_size, const uword in_bandwidth)
52 : DenseMat<T>(in_size, in_size, (in_bandwidth + 1) * in_size)
53 , band(in_bandwidth)
54 , m_rows(in_bandwidth + 1) {}
55
56 unique_ptr<MetaMat<T>> make_copy() override { return std::make_unique<BandSymmMat>(*this); }
57
58 void nullify(const uword K) override {
59 this->factored = false;
60 suanpan::for_each(std::max(band, K) - band, K, [&](const uword I) { this->memory[K - I + I * m_rows] = T(0); });
61 const auto t_factor = K * m_rows - K;
62 suanpan::for_each(K, std::min(this->n_rows, K + band + 1), [&](const uword I) { this->memory[I + t_factor] = T(0); });
63 }
64
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];
68 }
69
70 T& unsafe_at(const uword in_row, const uword in_col) override {
71 this->factored = false;
72 return this->memory[in_row - in_col + in_col * m_rows];
73 }
74
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);
77 return this->unsafe_at(in_row, in_col);
78 }
79
80 Mat<T> operator*(const Mat<T>&) const override;
81
82 [[nodiscard]] int sign_det() const override { return 1; }
83};
84
85template<sp_d T> T BandSymmMat<T>::bin = T(0);
86
87template<sp_d T> Mat<T> BandSymmMat<T>::operator*(const Mat<T>& X) const {
88 Mat<T> Y(arma::size(X));
89
90 const auto N = static_cast<blas_int>(this->n_cols);
91 const auto K = static_cast<blas_int>(band);
92 const auto LDA = static_cast<blas_int>(m_rows);
93 constexpr blas_int INC = 1;
94 T ALPHA = T(1);
95 T BETA = T(0);
96
97 if constexpr(std::is_same_v<T, float>) {
98 using E = float;
99 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); });
100 }
101 else {
102 using E = double;
103 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); });
104 }
105
106 return Y;
107}
108
109template<sp_d T> int BandSymmMat<T>::direct_solve(Mat<T>& X, Mat<T>&& B) {
110 if(this->factored) return this->solve_trs(X, std::move(B));
111
112 suanpan_assert([&] { if(this->n_rows != this->n_cols) throw invalid_argument("requires a square matrix"); });
113
114 blas_int INFO = 0;
115
116 const auto N = static_cast<blas_int>(this->n_rows);
117 const auto KD = static_cast<blas_int>(band);
118 const auto NRHS = static_cast<blas_int>(B.n_cols);
119 const auto LDAB = static_cast<blas_int>(m_rows);
120 const auto LDB = static_cast<blas_int>(B.n_rows);
121 this->factored = true;
122
123 if constexpr(std::is_same_v<T, float>) {
124 using E = float;
125 arma_fortran(arma_spbsv)(&UPLO, &N, &KD, &NRHS, (E*)this->memptr(), &LDAB, (E*)B.memptr(), &LDB, &INFO);
126 X = std::move(B);
127 }
128 else if(Precision::FULL == this->setting.precision) {
129 using E = double;
130 arma_fortran(arma_dpbsv)(&UPLO, &N, &KD, &NRHS, (E*)this->memptr(), &LDAB, (E*)B.memptr(), &LDB, &INFO);
131 X = std::move(B);
132 }
133 else {
134 this->s_memory = this->to_float();
135 arma_fortran(arma_spbtrf)(&UPLO, &N, &KD, this->s_memory.memptr(), &LDAB, &INFO);
136 if(0 == INFO) INFO = this->solve_trs(X, std::move(B));
137 }
138
139 if(0 != INFO)
140 suanpan_error("Error code {} received, the matrix is probably singular.\n", INFO);
141
142 return INFO;
143}
144
145template<sp_d T> int BandSymmMat<T>::solve_trs(Mat<T>& X, Mat<T>&& B) {
146 blas_int INFO = 0;
147
148 const auto N = static_cast<blas_int>(this->n_rows);
149 const auto KD = static_cast<blas_int>(band);
150 const auto NRHS = static_cast<blas_int>(B.n_cols);
151 const auto LDAB = static_cast<blas_int>(m_rows);
152 const auto LDB = static_cast<blas_int>(B.n_rows);
153
154 if constexpr(std::is_same_v<T, float>) {
155 using E = float;
156 arma_fortran(arma_spbtrs)(&UPLO, &N, &KD, &NRHS, (E*)this->memptr(), &LDAB, (E*)B.memptr(), &LDB, &INFO);
157 X = std::move(B);
158 }
159 else if(Precision::FULL == this->setting.precision) {
160 using E = double;
161 arma_fortran(arma_dpbtrs)(&UPLO, &N, &KD, &NRHS, (E*)this->memptr(), &LDAB, (E*)B.memptr(), &LDB, &INFO);
162 X = std::move(B);
163 }
164 else
165 this->mixed_trs(X, std::move(B), [&](fmat& residual) {
166 arma_fortran(arma_spbtrs)(&UPLO, &N, &KD, &NRHS, this->s_memory.memptr(), &LDAB, residual.memptr(), &LDB, &INFO);
167 return INFO;
168 });
169
170 if(0 != INFO)
171 suanpan_error("Error code {} received, the matrix is probably singular.\n", INFO);
172
173 return INFO;
174}
175
176#endif
177
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
int sign_det() const override
Definition BandSymmMat.hpp:82
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
const uword n_rows
Definition MetaMat.hpp:116
bool factored
Definition MetaMat.hpp:76
Mat< T > operator*(const Mat< T > &) const override
Definition BandSymmMat.hpp:87
int direct_solve(Mat< T > &, Mat< T > &&) override
Definition BandSymmMat.hpp:109
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