suanPan
SparseMat.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2024 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#ifndef SPARSEMAT_HPP
30#define SPARSEMAT_HPP
31
32#include "MetaMat.hpp"
33
34template<sp_d T> class SparseMat : public MetaMat<T> {
35protected:
37
38 int direct_solve(Mat<T>& X, Mat<T>&& B) override { return this->direct_solve(X, B); }
39
40public:
41 SparseMat(const uword in_row, const uword in_col, const uword in_elem = 0)
42 : MetaMat<T>(in_row, in_col, 0) { this->triplet_mat.init(in_elem); }
43
44 [[nodiscard]] bool is_empty() const override { return this->triplet_mat.is_empty(); }
45
46 void zeros() override {
47 this->factored = false;
48 this->triplet_mat.zeros();
49 }
50
51 void nullify(const uword idx) override {
52 this->factored = false;
53 suanpan::for_each(this->triplet_mat.n_elem, [&](const uword I) { if(this->triplet_mat.row(I) == idx || this->triplet_mat.col(I) == idx) this->triplet_mat.val_mem()[I] = T(0); });
54 }
55
56 [[nodiscard]] T max() const override { return this->triplet_mat.max(); }
57
58 [[nodiscard]] Col<T> diag() const override { return this->triplet_mat.diag(); }
59
60 T operator()(const uword in_row, const uword in_col) const override { return this->triplet_mat(in_row, in_col); }
61
62 T& at(const uword in_row, const uword in_col) override {
63 this->factored = false;
64 return this->triplet_mat.at(in_row, in_col);
65 }
66
67 [[nodiscard]] const T* memptr() const override { throw invalid_argument("not supported"); }
68
69 T* memptr() override { throw invalid_argument("not supported"); }
70
71 void scale_accu(const T scalar, const shared_ptr<MetaMat<T>>& in_mat) override {
72 if(nullptr == in_mat) return;
73 if(!in_mat->triplet_mat.is_empty()) return this->scale_accu(scalar, in_mat->triplet_mat);
74 this->factored = false;
75 for(auto I = 0llu; I < in_mat->n_rows; ++I) for(auto J = 0llu; J < in_mat->n_cols; ++J) if(const auto t_val = in_mat->operator()(I, J); !suanpan::approx_equal(T(0), t_val)) at(I, J) = scalar * t_val;
76 }
77
78 void scale_accu(const T scalar, const triplet_form<T, uword>& in_mat) override {
79 this->factored = false;
80 if(1. == scalar) this->triplet_mat += in_mat;
81 else if(-1. == scalar) this->triplet_mat -= in_mat;
82 else this->triplet_mat.assemble(in_mat, 0, 0, scalar);
83 }
84
85 Mat<T> operator*(const Mat<T>& in_mat) const override { return this->triplet_mat * in_mat; }
86
87 void operator*=(const T scalar) override {
88 this->factored = false;
89 this->triplet_mat *= scalar;
90 }
91
92 [[nodiscard]] int sign_det() const override { throw invalid_argument("not supported"); }
93
94 void csc_condense() override { this->triplet_mat.csc_condense(); }
95
96 void csr_condense() override { this->triplet_mat.csr_condense(); }
97};
98
99#endif
100
A MetaMat class that holds matrices.
Definition: MetaMat.hpp:72
triplet_form< T, uword > triplet_mat
Definition: MetaMat.hpp:116
bool factored
Definition: MetaMat.hpp:74
A SparseMat class that holds matrices.
Definition: SparseMat.hpp:34
void zeros() override
Definition: SparseMat.hpp:46
int sign_det() const override
Definition: SparseMat.hpp:92
T * memptr() override
Definition: SparseMat.hpp:69
T operator()(const uword in_row, const uword in_col) const override
Access element (read-only), returns zero if out-of-bound.
Definition: SparseMat.hpp:60
Mat< T > operator*(const Mat< T > &in_mat) const override
Definition: SparseMat.hpp:85
const T * memptr() const override
Definition: SparseMat.hpp:67
SparseMat(const uword in_row, const uword in_col, const uword in_elem=0)
Definition: SparseMat.hpp:41
T max() const override
Definition: SparseMat.hpp:56
void csc_condense() override
Definition: SparseMat.hpp:94
void nullify(const uword idx) override
Definition: SparseMat.hpp:51
void scale_accu(const T scalar, const triplet_form< T, uword > &in_mat) override
Definition: SparseMat.hpp:78
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition: SparseMat.hpp:62
void csr_condense() override
Definition: SparseMat.hpp:96
void scale_accu(const T scalar, const shared_ptr< MetaMat< T > > &in_mat) override
Definition: SparseMat.hpp:71
int direct_solve(Mat< T > &X, Mat< T > &&B) override
Definition: SparseMat.hpp:38
bool is_empty() const override
Definition: SparseMat.hpp:44
void operator*=(const T scalar) override
Definition: SparseMat.hpp:87
Col< T > diag() const override
Definition: SparseMat.hpp:58
void zeros()
Definition: triplet_form.hpp:176
data_t max() const
Definition: triplet_form.hpp:171
void csc_condense()
Definition: triplet_form.hpp:209
bool is_empty() const
Definition: triplet_form.hpp:169
data_t & at(index_t, index_t)
Definition: triplet_form.hpp:384
void csr_condense()
Definition: triplet_form.hpp:204
const index_t n_elem
Definition: triplet_form.hpp:130
void init(const index_t in_elem)
Definition: triplet_form.hpp:181
Col< data_t > diag() const
Definition: triplet_form.hpp:571
void assemble(const Mat< data_t > &, const Col< uword > &)
Definition: triplet_form.hpp:456
std::enable_if_t<!std::numeric_limits< T >::is_integer, bool > approx_equal(T x, T y, int ulp=2)
Definition: utility.h:60
void for_each(const IT start, const IT end, F &&FN)
Definition: utility.h:28