suanPan
Loading...
Searching...
No Matches
BandMat.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 BANDMAT_HPP
31#define BANDMAT_HPP
32
33#include "../DenseMat.hpp"
34
35template<sp_d T> class BandMat : public DenseMat<T> {
36 static constexpr char TRAN = 'N';
37
38 static T bin;
39
40 const uword s_band;
41
42 int solve_trs(Mat<T>&, Mat<T>&&);
43
44protected:
45 const uword m_rows; // memory block layout
46
47 const uword l_band;
48 const uword u_band;
49
51
52 int direct_solve(Mat<T>&, Mat<T>&&) override;
53
54public:
55 BandMat(const uword in_size, const uword in_l, const uword in_u)
56 : DenseMat<T>(in_size, in_size, (2 * in_l + in_u + 1) * in_size)
57 , s_band(in_l + in_u)
58 , m_rows(2 * in_l + in_u + 1)
59 , l_band(in_l)
60 , u_band(in_u) {
61 if(m_rows >= in_size)
62 suanpan_warning("The storage requirement for the banded matrix is larger than that of a full matrix, consider using a full/sparse matrix instead.\n");
63 }
64
65 unique_ptr<MetaMat<T>> make_copy() override { return std::make_unique<BandMat>(*this); }
66
67 void nullify(const uword K) override {
68 this->factored = false;
69 suanpan::for_each(std::max(K, u_band) - u_band, std::min(this->n_rows, K + l_band + 1), [&](const uword I) { this->memory[I + s_band + K * (m_rows - 1)] = T(0); });
70 suanpan::for_each(std::max(K, l_band) - l_band, std::min(this->n_cols, K + u_band + 1), [&](const uword I) { this->memory[K + s_band + I * (m_rows - 1)] = T(0); });
71 }
72
73 T operator()(const uword in_row, const uword in_col) const override {
74 if(in_row > in_col + l_band || in_row + u_band < in_col) [[unlikely]] return bin = T(0);
75 return this->memory[in_row + s_band + in_col * (m_rows - 1)];
76 }
77
78 T& unsafe_at(const uword in_row, const uword in_col) override {
79 this->factored = false;
80 return this->memory[in_row + s_band + in_col * (m_rows - 1)];
81 }
82
83 T& at(const uword in_row, const uword in_col) override {
84 if(in_row > in_col + l_band || in_row + u_band < in_col) [[unlikely]] return bin = T(0);
85 return this->unsafe_at(in_row, in_col);
86 }
87
88 Mat<T> operator*(const Mat<T>&) const override;
89
90 [[nodiscard]] int sign_det() const override {
91 auto det_sign = 1;
92 for(unsigned I = 0; I < this->pivot.n_elem; ++I)
93 if((this->operator()(I, I) < T(0)) ^ (static_cast<int>(I) + 1 != this->pivot(I))) det_sign = -det_sign;
94 return det_sign;
95 }
96};
97
98template<sp_d T> T BandMat<T>::bin = T(0);
99
100template<sp_d T> Mat<T> BandMat<T>::operator*(const Mat<T>& X) const {
101 Mat<T> Y(arma::size(X));
102
103 const auto M = static_cast<blas_int>(this->n_rows);
104 const auto N = static_cast<blas_int>(this->n_cols);
105 const auto KL = static_cast<blas_int>(l_band);
106 const auto KU = static_cast<blas_int>(u_band);
107 const auto LDA = static_cast<blas_int>(m_rows);
108 constexpr blas_int INC = 1;
109 T ALPHA = T(1);
110 T BETA = T(0);
111
112 if constexpr(std::is_same_v<T, float>) {
113 using E = float;
114 suanpan::for_each(X.n_cols, [&](const uword I) { arma_fortran(arma_sgbmv)(&TRAN, &M, &N, &KL, &KU, (E*)&ALPHA, (E*)(this->memptr() + l_band), &LDA, (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
115 }
116 else {
117 using E = double;
118 suanpan::for_each(X.n_cols, [&](const uword I) { arma_fortran(arma_dgbmv)(&TRAN, &M, &N, &KL, &KU, (E*)&ALPHA, (E*)(this->memptr() + l_band), &LDA, (E*)X.colptr(I), &INC, (E*)&BETA, (E*)Y.colptr(I), &INC); });
119 }
120
121 return Y;
122}
123
124template<sp_d T> int BandMat<T>::direct_solve(Mat<T>& X, Mat<T>&& B) {
125 if(this->factored) return this->solve_trs(X, std::move(B));
126
127 suanpan_assert([&] { if(this->n_rows != this->n_cols) throw invalid_argument("requires a square matrix"); });
128
129 blas_int INFO = 0;
130
131 auto N = static_cast<blas_int>(this->n_rows);
132 const auto KL = static_cast<blas_int>(l_band);
133 const auto KU = static_cast<blas_int>(u_band);
134 const auto NRHS = static_cast<blas_int>(B.n_cols);
135 const auto LDAB = static_cast<blas_int>(m_rows);
136 const auto LDB = static_cast<blas_int>(B.n_rows);
137 this->pivot.zeros(N);
138 this->factored = true;
139
140 if constexpr(std::is_same_v<T, float>) {
141 using E = float;
142 arma_fortran(arma_sgbsv)(&N, &KL, &KU, &NRHS, (E*)this->memptr(), &LDAB, this->pivot.memptr(), (E*)B.memptr(), &LDB, &INFO);
143 X = std::move(B);
144 }
145 else if(Precision::FULL == this->setting.precision) {
146 using E = double;
147 arma_fortran(arma_dgbsv)(&N, &KL, &KU, &NRHS, (E*)this->memptr(), &LDAB, this->pivot.memptr(), (E*)B.memptr(), &LDB, &INFO);
148 X = std::move(B);
149 }
150 else {
151 this->s_memory = this->to_float();
152 arma_fortran(arma_sgbtrf)(&N, &N, &KL, &KU, this->s_memory.memptr(), &LDAB, this->pivot.memptr(), &INFO);
153 if(0 == INFO) INFO = this->solve_trs(X, std::move(B));
154 }
155
156 if(0 != INFO)
157 suanpan_error("Error code {} received, the matrix is probably singular.\n", INFO);
158
159 return INFO;
160}
161
162template<sp_d T> int BandMat<T>::solve_trs(Mat<T>& X, Mat<T>&& B) {
163 blas_int INFO = 0;
164
165 const auto N = static_cast<blas_int>(this->n_rows);
166 const auto KL = static_cast<blas_int>(l_band);
167 const auto KU = static_cast<blas_int>(u_band);
168 const auto NRHS = static_cast<blas_int>(B.n_cols);
169 const auto LDAB = static_cast<blas_int>(m_rows);
170 const auto LDB = static_cast<blas_int>(B.n_rows);
171
172 if constexpr(std::is_same_v<T, float>) {
173 using E = float;
174 arma_fortran(arma_sgbtrs)(&TRAN, &N, &KL, &KU, &NRHS, (E*)this->memptr(), &LDAB, this->pivot.memptr(), (E*)B.memptr(), &LDB, &INFO);
175 X = std::move(B);
176 }
177 else if(Precision::FULL == this->setting.precision) {
178 using E = double;
179 arma_fortran(arma_dgbtrs)(&TRAN, &N, &KL, &KU, &NRHS, (E*)this->memptr(), &LDAB, this->pivot.memptr(), (E*)B.memptr(), &LDB, &INFO);
180 X = std::move(B);
181 }
182 else
183 this->mixed_trs(X, std::move(B), [&](fmat& residual) {
184 arma_fortran(arma_sgbtrs)(&TRAN, &N, &KL, &KU, &NRHS, this->s_memory.memptr(), &LDAB, this->pivot.memptr(), residual.memptr(), &LDB, &INFO);
185 return INFO;
186 });
187
188 if(0 != INFO)
189 suanpan_error("Error code {} received, the matrix is probably singular.\n", INFO);
190
191 return INFO;
192}
193
194#endif
195
A BandMat class that holds matrices.
Definition BandMat.hpp:35
T & unsafe_at(const uword in_row, const uword in_col) override
Access element without bound check.
Definition BandMat.hpp:78
BandMat(const uword in_size, const uword in_l, const uword in_u)
Definition BandMat.hpp:55
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition BandMat.hpp:83
const uword u_band
Definition BandMat.hpp:48
unique_ptr< MetaMat< T > > make_copy() override
Definition BandMat.hpp:65
T operator()(const uword in_row, const uword in_col) const override
Access element (read-only), returns zero if out-of-bound.
Definition BandMat.hpp:73
const uword l_band
Definition BandMat.hpp:47
const uword m_rows
Definition BandMat.hpp:45
int sign_det() const override
Definition BandMat.hpp:90
void nullify(const uword K) override
Definition BandMat.hpp:67
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
const uword n_cols
Definition MetaMat.hpp:117
const uword n_rows
Definition MetaMat.hpp:116
bool factored
Definition MetaMat.hpp:76
Mat< T > operator*(const Mat< T > &) const override
Definition BandMat.hpp:100
int direct_solve(Mat< T > &, Mat< T > &&) override
Definition BandMat.hpp:124
void for_each(const IT start, const IT end, F &&FN)
Definition utility.h:28
#define suanpan_warning(...)
Definition suanPan.h:372
void suanpan_assert(const std::function< void()> &F)
Definition suanPan.h:360
#define suanpan_error(...)
Definition suanPan.h:373