suanPan
Loading...
Searching...
No Matches
triplet_form.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2023 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 ******************************************************************************/
17
18#ifndef TRIPLET_FORM_HPP
19#define TRIPLET_FORM_HPP
20
21#include <Toolbox/utility.h>
22#include <numeric>
23
24template<sp_d data_t, sp_i index_t> class csc_form;
25template<sp_d data_t, sp_i index_t> class csr_form;
26
27enum class SparseBase : short unsigned {
28 ZERO,
29 ONE
30};
31
32template<sp_i index_t> class csr_comparator {
33 const index_t* const row_idx;
34 const index_t* const col_idx;
35
36public:
37 csr_comparator(const index_t* const in_row_idx, const index_t* const in_col_idx)
38 : row_idx(in_row_idx)
39 , col_idx(in_col_idx) {}
40
41 bool operator()(const index_t idx_a, const index_t idx_b) const {
42 if(row_idx[idx_a] == row_idx[idx_b]) return col_idx[idx_a] < col_idx[idx_b];
43 return row_idx[idx_a] < row_idx[idx_b];
44 }
45};
46
47template<sp_i index_t> class csc_comparator {
48 const index_t* const row_idx;
49 const index_t* const col_idx;
50
51public:
52 csc_comparator(const index_t* const in_row_idx, const index_t* const in_col_idx)
53 : row_idx(in_row_idx)
54 , col_idx(in_col_idx) {}
55
56 bool operator()(const index_t idx_a, const index_t idx_b) const {
57 if(col_idx[idx_a] == col_idx[idx_b]) return row_idx[idx_a] < row_idx[idx_b];
58 return col_idx[idx_a] < col_idx[idx_b];
59 }
60};
61
62template<sp_d data_t, sp_i index_t> class triplet_form final {
63 const data_t bin = data_t(0);
64
65 using index_ptr = std::unique_ptr<index_t[]>;
66 using data_ptr = std::unique_ptr<data_t[]>;
67
68 index_ptr row_idx = nullptr; // index storage
69 index_ptr col_idx = nullptr; // index storage
70 data_ptr val_idx = nullptr; // value storage
71
72 bool csc_sorted = false;
73 bool csr_sorted = false;
74
75 template<sp_d in_dt, sp_i in_it> void copy_to(const std::unique_ptr<in_it[]>& new_row_idx, const std::unique_ptr<in_it[]>& new_col_idx, const std::unique_ptr<in_dt[]>& new_val_idx, const index_t begin, const index_t row_offset, const index_t col_offset, const data_t scalar) const { copy_to(new_row_idx.get(), new_col_idx.get(), new_val_idx.get(), begin, row_offset, col_offset, scalar); }
76
77 template<sp_d in_dt, sp_i in_it> void copy_to(in_it* const new_row_idx, in_it* const new_col_idx, in_dt* const new_val_idx, const index_t begin, const index_t row_offset, const index_t col_offset, const data_t scalar) const {
78 suanpan_for(index_t(0), n_elem, [&](const index_t I) {
79 new_row_idx[I + begin] = in_it(row_idx[I] + row_offset);
80 new_col_idx[I + begin] = in_it(col_idx[I] + col_offset);
81 new_val_idx[I + begin] = in_dt(scalar * val_idx[I]);
82 });
83 }
84
88 void reserve(const index_t in_elem) {
89 if(in_elem <= n_alloc) return;
90
91 access::rw(n_alloc) = index_t(std::pow(2., std::ceil(std::log2(in_elem)) + 1));
92
93 index_ptr new_row_idx(new index_t[n_alloc]);
94 index_ptr new_col_idx(new index_t[n_alloc]);
95 data_ptr new_val_idx(new data_t[n_alloc]);
96
97 copy_to(new_row_idx, new_col_idx, new_val_idx, 0, 0, 0, 1);
98
99 row_idx = std::move(new_row_idx);
100 col_idx = std::move(new_col_idx);
101 val_idx = std::move(new_val_idx);
102 }
103
104 void invalidate_sorting_flag() { csc_sorted = csr_sorted = false; }
105
106 void condense(bool = false);
107
108 void populate_diagonal() {
109 const auto t_elem = std::min(n_rows, n_cols);
110 reserve(n_elem + t_elem);
111 suanpan_for(index_t(0), t_elem, [&](const index_t I) {
112 row_idx[n_elem + I] = I;
113 col_idx[n_elem + I] = I;
114 val_idx[n_elem + I] = data_t(0);
115 });
116 access::rw(n_elem) += t_elem;
117 invalidate_sorting_flag();
118 }
119
120public:
121 typedef data_t data_type;
122 typedef index_t index_type;
123
124 template<sp_d in_dt, sp_i in_it> friend class csc_form;
125 template<sp_d in_dt, sp_i in_it> friend class csr_form;
126 template<sp_d in_dt, sp_i in_it> friend class triplet_form;
127
128 const index_t n_rows = 0;
129 const index_t n_cols = 0;
130 const index_t n_elem = 0;
131 const index_t n_alloc = 0;
132
133 triplet_form() = default;
136 triplet_form& operator=(const triplet_form&);
137 triplet_form& operator=(triplet_form&&) noexcept;
138 ~triplet_form() = default;
139
140 triplet_form(const index_t in_rows, const index_t in_cols, const index_t in_elem = index_t(0))
141 : n_rows(in_rows)
142 , n_cols(in_cols) { init(in_elem); }
143
144 template<sp_d in_dt> explicit triplet_form(const SpMat<in_dt>&);
145 template<sp_d in_dt, sp_i in_it> explicit triplet_form(triplet_form<in_dt, in_it>&, SparseBase = SparseBase::ZERO, bool = false);
146
147 [[nodiscard]] const index_t* row_mem() const { return row_idx.get(); }
148
149 [[nodiscard]] const index_t* col_mem() const { return col_idx.get(); }
150
151 [[nodiscard]] const data_t* val_mem() const { return val_idx.get(); }
152
153 [[nodiscard]] index_t* row_mem() { return row_idx.get(); }
154
155 [[nodiscard]] index_t* col_mem() { return col_idx.get(); }
156
157 [[nodiscard]] data_t* val_mem() { return val_idx.get(); }
158
159 [[nodiscard]] index_t row(const index_t I) const { return row_idx[I]; }
160
161 [[nodiscard]] index_t col(const index_t I) const { return col_idx[I]; }
162
163 [[nodiscard]] data_t val(const index_t I) const { return val_idx[I]; }
164
165 [[nodiscard]] bool is_csr_sorted() const { return csr_sorted; }
166
167 [[nodiscard]] bool is_csc_sorted() const { return csc_sorted; }
168
169 [[nodiscard]] bool is_empty() const { return 0 == n_elem; }
170
171 [[nodiscard]] data_t max() const {
172 if(is_empty()) return data_t(0);
173 return *suanpan_max_element(val_idx.get(), val_idx.get() + n_elem);
174 }
175
176 void zeros() {
177 access::rw(n_elem) = 0;
178 invalidate_sorting_flag();
179 }
180
181 void init(const index_t in_elem) {
182 zeros();
183 reserve(in_elem);
184 }
185
186 void init(const index_t in_rows, const index_t in_cols, const index_t in_elem) {
187 access::rw(n_rows) = in_rows;
188 access::rw(n_cols) = in_cols;
189 init(in_elem);
190 }
191
192 const data_t& operator()(const index_t row, const index_t col) const {
193 for(index_t I = 0; I < n_elem; ++I) if(row == row_idx[I] && col == col_idx[I]) return val_idx[I];
194 return access::rw(bin) = 0.;
195 }
196
197 data_t& at(index_t, index_t);
198
199 void print() const;
200
201 void csr_sort();
202 void csc_sort();
203
205 csr_sort();
206 condense(false);
207 }
208
210 csc_sort();
211 condense(false);
212 }
213
215 populate_diagonal();
216 csr_sort();
217 condense(true);
218 }
219
221 populate_diagonal();
222 csc_sort();
223 condense(true);
224 }
225
226 void assemble(const Mat<data_t>&, const Col<uword>&);
227 template<sp_d in_dt, sp_i in_it> void assemble(const triplet_form<in_dt, in_it>&, index_t, index_t, data_t);
228
229 template<sp_d in_dt, sp_i in_it> void assemble(const triplet_form<in_dt, in_it>& in_mat, const std::vector<index_t>& row_shift, const std::vector<index_t>& col_shift, const std::vector<data_t>& scalar) {
230 suanpan_assert([&] { if(scalar.size() != row_shift.size() || scalar.size() != col_shift.size()) throw invalid_argument("size mismatch detected"); });
231
232 reserve(n_elem + index_t(scalar.size()) * index_t(in_mat.n_elem));
233
234 for(size_t I = 0; I < scalar.size(); ++I) assemble(in_mat, row_shift[I], col_shift[I], scalar[I]);
235 }
236
237 Mat<data_t> operator*(const Col<data_t>& in_mat) const {
238 Mat<data_t> out_mat(in_mat.n_rows, in_mat.n_cols, fill::zeros);
239
240 for(index_t I = 0; I < n_elem; ++I) out_mat(row_idx[I]) += val_idx[I] * in_mat(col_idx[I]);
241
242 return out_mat;
243 }
244
245 Mat<data_t> operator*(const Mat<data_t>& in_mat) const {
246 Mat<data_t> out_mat(in_mat.n_rows, in_mat.n_cols, fill::zeros);
247
248 for(index_t I = 0; I < n_elem; ++I) out_mat.row(row_idx[I]) += val_idx[I] * in_mat.row(col_idx[I]);
249
250 return out_mat;
251 }
252
253 template<sp_d T2> triplet_form<data_t, index_t> operator*(T2) const;
254 template<sp_d T2> triplet_form<data_t, index_t> operator/(T2) const;
257
260 return copy += in_mat;
261 }
262
265 return copy -= in_mat;
266 }
267
270
271 [[nodiscard]] Col<data_t> diag() const;
275};
276
277template<sp_d data_t, sp_i index_t> void triplet_form<data_t, index_t>::condense(const bool full) {
278 if(n_elem < 2) return;
279
280 auto last_row = row_idx[0], last_col = col_idx[0];
281
282 sp_i auto current_pos = index_t(0);
283 sp_d auto last_sum = data_t(0);
284
285 auto populate = [&] {
286 if(suanpan::approx_equal(last_sum, data_t(0)) && (!full || last_row != last_col)) return;
287 row_idx[current_pos] = last_row;
288 col_idx[current_pos] = last_col;
289 val_idx[current_pos] = last_sum;
290 ++current_pos;
291 last_sum = data_t(0);
292 };
293
294 for(index_t I = 0; I < n_elem; ++I) {
295 if(last_row != row_idx[I] || last_col != col_idx[I]) {
296 populate();
297 last_row = row_idx[I];
298 last_col = col_idx[I];
299 }
300 last_sum += val_idx[I];
301 }
302
303 populate();
304
305 access::rw(n_elem) = current_pos;
306}
307
308template<sp_d data_t, sp_i index_t> triplet_form<data_t, index_t>::triplet_form(const triplet_form& in_mat)
309 : csc_sorted{in_mat.csc_sorted}
310 , csr_sorted{in_mat.csr_sorted}
311 , n_rows{in_mat.n_rows}
312 , n_cols{in_mat.n_cols} {
313 init(in_mat.n_alloc);
314 in_mat.copy_to(row_idx, col_idx, val_idx, 0, 0, 0, 1);
315 access::rw(n_elem) = in_mat.n_elem;
316}
317
318template<sp_d data_t, sp_i index_t> triplet_form<data_t, index_t>::triplet_form(triplet_form&& in_mat) noexcept
319 : row_idx{std::move(in_mat.row_idx)}
320 , col_idx{std::move(in_mat.col_idx)}
321 , val_idx{std::move(in_mat.val_idx)}
322 , csc_sorted{in_mat.csc_sorted}
323 , csr_sorted{in_mat.csr_sorted}
324 , n_rows{in_mat.n_rows}
325 , n_cols{in_mat.n_cols}
326 , n_elem{in_mat.n_elem}
327 , n_alloc{in_mat.n_alloc} {}
328
330 if(this == &in_mat) return *this;
331 csc_sorted = in_mat.csc_sorted;
332 csr_sorted = in_mat.csr_sorted;
333 access::rw(n_rows) = in_mat.n_rows;
334 access::rw(n_cols) = in_mat.n_cols;
335 init(in_mat.n_alloc);
336 in_mat.copy_to(row_idx, col_idx, val_idx, 0, 0, 0, 1);
337 access::rw(n_elem) = in_mat.n_elem;
338 return *this;
339}
340
341template<sp_d data_t, sp_i index_t> triplet_form<data_t, index_t>& triplet_form<data_t, index_t>::operator=(triplet_form&& in_mat) noexcept {
342 if(this == &in_mat) return *this;
343 csc_sorted = in_mat.csc_sorted;
344 csr_sorted = in_mat.csr_sorted;
345 access::rw(n_rows) = in_mat.n_rows;
346 access::rw(n_cols) = in_mat.n_cols;
347 access::rw(n_elem) = in_mat.n_elem;
348 access::rw(n_alloc) = in_mat.n_alloc;
349 row_idx = std::move(in_mat.row_idx);
350 col_idx = std::move(in_mat.col_idx);
351 val_idx = std::move(in_mat.val_idx);
352 return *this;
353}
354
355template<sp_d data_t, sp_i index_t> template<sp_d in_dt> triplet_form<data_t, index_t>::triplet_form(const SpMat<in_dt>& in_mat)
356 : n_rows(index_t(in_mat.n_rows))
357 , n_cols(index_t(in_mat.n_cols)) {
358 init(index_t(in_mat.n_nonzero));
359
360 for(auto I = in_mat.begin(); I != in_mat.end(); ++I) at(I.row(), I.col()) = *I;
361}
362
363template<sp_d data_t, sp_i index_t> template<sp_d in_dt, sp_i in_it> triplet_form<data_t, index_t>::triplet_form(triplet_form<in_dt, in_it>& in_mat, const SparseBase base, const bool full)
364 : csc_sorted(in_mat.csc_sorted)
365 , csr_sorted(in_mat.csr_sorted)
366 , n_rows(index_t(in_mat.n_rows))
367 , n_cols(index_t(in_mat.n_cols)) {
368 if(in_mat.is_empty()) return;
369
370 init(index_t(in_mat.n_alloc));
371
372 if(full) in_mat.full_csc_condense();
373 else in_mat.csc_condense();
374
375 const sp_i auto shift = index_t(base);
376
377 in_mat.copy_to(row_idx, col_idx, val_idx, 0, shift, shift, 1);
378
379 access::rw(n_elem) = index_t(in_mat.n_elem);
380}
381
382template<sp_d data_t, sp_i index_t> data_t& triplet_form<data_t, index_t>::at(const index_t row, const index_t col) {
383 suanpan_assert([&] { if(row >= n_rows || col >= n_cols) throw invalid_argument("inconsistent size"); });
384
385 invalidate_sorting_flag();
386 reserve(n_elem + 1);
387 row_idx[n_elem] = row;
388 col_idx[n_elem] = col;
389 return val_idx[access::rw(n_elem)++] = data_t(0);
390}
391
392template<sp_d data_t, sp_i index_t> void triplet_form<data_t, index_t>::print() const {
393 suanpan_info("A sparse matrix in triplet form with size of {} by {}, the density of {:.3f}%.\n", static_cast<unsigned>(n_rows), static_cast<unsigned>(n_cols), static_cast<double>(n_elem) / static_cast<double>(n_rows) / static_cast<double>(n_cols) * 1E2);
394 if(n_elem > index_t(1000)) {
395 suanpan_info("More than 1000 elements exist.\n");
396 return;
397 }
398 for(index_t I = 0; I < n_elem; ++I)
399 suanpan_info("({}, {}) ===> {:+.8E}\n", static_cast<unsigned>(row_idx[I]), static_cast<unsigned>(col_idx[I]), val_idx[I]);
400}
401
402template<sp_d data_t, sp_i index_t> void triplet_form<data_t, index_t>::csr_sort() {
403 if(csr_sorted) return;
404
405 std::vector<index_t> index(n_elem);
406 std::iota(index.begin(), index.end(), index_t(0));
407
408 suanpan_sort(index.begin(), index.end(), csr_comparator<index_t>(row_idx.get(), col_idx.get()));
409
410 index_ptr new_row_idx(new index_t[n_alloc]);
411 index_ptr new_col_idx(new index_t[n_alloc]);
412 data_ptr new_val_idx(new data_t[n_alloc]);
413
414 suanpan_for(index_t(0), n_elem, [&](const index_t I) {
415 new_row_idx[I] = row_idx[index[I]];
416 new_col_idx[I] = col_idx[index[I]];
417 new_val_idx[I] = val_idx[index[I]];
418 });
419
420 row_idx = std::move(new_row_idx);
421 col_idx = std::move(new_col_idx);
422 val_idx = std::move(new_val_idx);
423
424 csr_sorted = true;
425 csc_sorted = false;
426}
427
428template<sp_d data_t, sp_i index_t> void triplet_form<data_t, index_t>::csc_sort() {
429 if(csc_sorted) return;
430
431 std::vector<index_t> index(n_elem);
432 std::iota(index.begin(), index.end(), index_t(0));
433
434 suanpan_sort(index.begin(), index.end(), csc_comparator<index_t>(row_idx.get(), col_idx.get()));
435
436 index_ptr new_row_idx(new index_t[n_alloc]);
437 index_ptr new_col_idx(new index_t[n_alloc]);
438 data_ptr new_val_idx(new data_t[n_alloc]);
439
440 suanpan_for(index_t(0), n_elem, [&](const index_t I) {
441 new_row_idx[I] = row_idx[index[I]];
442 new_col_idx[I] = col_idx[index[I]];
443 new_val_idx[I] = val_idx[index[I]];
444 });
445
446 row_idx = std::move(new_row_idx);
447 col_idx = std::move(new_col_idx);
448 val_idx = std::move(new_val_idx);
449
450 csc_sorted = true;
451 csr_sorted = false;
452}
453
454template<sp_d data_t, sp_i index_t> void triplet_form<data_t, index_t>::assemble(const Mat<data_t>& in_mat, const Col<uword>& in_dof) {
455 if(in_mat.empty()) return;
456
457 invalidate_sorting_flag();
458
459 const auto t_elem = n_elem + index_t(in_mat.n_elem);
460
461 reserve(t_elem);
462
463 suanpan_for(static_cast<uword>(0), in_mat.n_elem, [&](const uword I) {
464 row_idx[n_elem + I] = index_t(in_dof(I % in_dof.n_elem));
465 col_idx[n_elem + I] = index_t(in_dof(I / in_dof.n_elem));
466 val_idx[n_elem + I] = in_mat(I);
467 });
468
469 access::rw(n_elem) = t_elem;
470}
471
472template<sp_d data_t, sp_i index_t> template<sp_d in_dt, sp_i in_it> void triplet_form<data_t, index_t>::assemble(const triplet_form<in_dt, in_it>& in_mat, const index_t row_shift, const index_t col_shift, const data_t scalar) {
473 if(in_mat.is_empty()) return;
474
475 invalidate_sorting_flag();
476
477 const auto t_elem = n_elem + in_mat.n_elem;
478
479 reserve(t_elem);
480
481 in_mat.copy_to(row_idx, col_idx, val_idx, n_elem, row_shift, col_shift, scalar);
482
483 access::rw(n_elem) = t_elem;
484}
485
486template<sp_d data_t, sp_i index_t> template<sp_d T2> triplet_form<data_t, index_t> triplet_form<data_t, index_t>::operator*(const T2 scalar) const {
488
490
491 if(suanpan::approx_equal(T2(1), scalar)) return copy;
492
493 if(suanpan::approx_equal(T2(-1), scalar))
494 suanpan_for_each(copy.val_idx.get(), copy.val_idx.get() + copy.n_elem, [](data_t& I) { I = -I; });
495 else
496 suanpan_for_each(copy.val_idx.get(), copy.val_idx.get() + copy.n_elem, [=](data_t& I) { I *= data_t(scalar); });
497
498 return copy;
499}
500
501template<sp_d data_t, sp_i index_t> template<sp_d T2> triplet_form<data_t, index_t> triplet_form<data_t, index_t>::operator/(const T2 scalar) const {
503
504 if(suanpan::approx_equal(T2(1), scalar)) return copy;
505
506 if(suanpan::approx_equal(T2(-1), scalar))
507 suanpan_for_each(copy.val_idx.get(), copy.val_idx.get() + copy.n_elem, [](data_t& I) { I = -I; });
508 else
509 suanpan_for_each(copy.val_idx.get(), copy.val_idx.get() + copy.n_elem, [=](data_t& I) { I /= data_t(scalar); });
510
511 return copy;
512}
513
514template<sp_d data_t, sp_i index_t> template<sp_d T2> triplet_form<data_t, index_t>& triplet_form<data_t, index_t>::operator*=(const T2 scalar) {
515 if(suanpan::approx_equal(T2(1), scalar)) return *this;
516
517 if(suanpan::approx_equal(T2(0), scalar)) zeros();
518 else if(suanpan::approx_equal(T2(-1), scalar))
519 suanpan_for_each(val_idx.get(), val_idx.get() + n_elem, [](data_t& I) { I = -I; });
520 else
521 suanpan_for_each(val_idx.get(), val_idx.get() + n_elem, [=](data_t& I) { I *= data_t(scalar); });
522
523 return *this;
524}
525
526template<sp_d data_t, sp_i index_t> template<sp_d T2> triplet_form<data_t, index_t>& triplet_form<data_t, index_t>::operator/=(const T2 scalar) {
527 if(suanpan::approx_equal(T2(1), scalar)) return *this;
528
529 if(suanpan::approx_equal(T2(-1), scalar))
530 suanpan_for_each(val_idx.get(), val_idx.get() + n_elem, [](data_t& I) { I = -I; });
531 else
532 suanpan_for_each(val_idx.get(), val_idx.get() + n_elem, [=](data_t& I) { I /= data_t(scalar); });
533
534 return *this;
535}
536
538 if(in_mat.is_empty()) return *this;
539
540 invalidate_sorting_flag();
541
542 const auto t_elem = n_elem + index_t(in_mat.n_elem);
543
544 reserve(t_elem);
545
546 in_mat.copy_to(row_idx, col_idx, val_idx, n_elem, 0, 0, 1);
547
548 access::rw(n_elem) = t_elem;
549
550 return *this;
551}
552
554 if(in_mat.is_empty()) return *this;
555
556 invalidate_sorting_flag();
557
558 const auto t_elem = n_elem + index_t(in_mat.n_elem);
559
560 reserve(t_elem);
561
562 in_mat.copy_to(row_idx, col_idx, val_idx, n_elem, 0, 0, -1);
563
564 access::rw(n_elem) = t_elem;
565
566 return *this;
567}
568
569template<sp_d data_t, sp_i index_t> Col<data_t> triplet_form<data_t, index_t>::diag() const {
570 Col<data_t> diag_vec(std::min(n_rows, n_cols), fill::zeros);
571 for(index_t I = 0; I < n_elem; ++I) if(row(I) == col(I)) diag_vec(row(I)) += val(I);
572 return diag_vec;
573}
574
576 auto out_mat = *this;
577
578 suanpan_for(index_t(0), out_mat.n_elem, [&](const index_t I) { out_mat.val_idx[I] *= out_mat.row(I) == out_mat.col(I); });
579
580 return out_mat;
581}
582
584 auto out_mat = *this;
585
586 suanpan_for(index_t(0), out_mat.n_elem, [&](const index_t I) { out_mat.val_idx[I] *= out_mat.row(I) < out_mat.col(I); });
587
588 return out_mat;
589}
590
592 auto out_mat = *this;
593
594 suanpan_for(index_t(0), out_mat.n_elem, [&](const index_t I) { out_mat.val_idx[I] *= out_mat.col(I) < out_mat.row(I); });
595
596 return out_mat;
597}
598
599template<sp_d data_t, sp_i index_t> triplet_form<data_t, index_t> operator+(const triplet_form<data_t, index_t>& mat_a, const triplet_form<data_t, index_t>& mat_b) {
600 auto out = mat_a;
601 out += mat_b;
602 return out;
603}
604
606 mat_a += mat_b;
607 return std::forward<triplet_form<data_t, index_t>>(mat_a);
608}
609
611 mat_b += mat_a;
612 return std::forward<triplet_form<data_t, index_t>>(mat_b);
613}
614
616 mat_a += mat_b;
617 return std::forward<triplet_form<data_t, index_t>>(mat_a);
618}
619
620#endif
Storage< T >::iterator begin(Storage< T > &S)
Definition: Storage.hpp:200
Definition: triplet_form.hpp:47
bool operator()(const index_t idx_a, const index_t idx_b) const
Definition: triplet_form.hpp:56
csc_comparator(const index_t *const in_row_idx, const index_t *const in_col_idx)
Definition: triplet_form.hpp:52
Definition: csc_form.hpp:25
Definition: triplet_form.hpp:32
bool operator()(const index_t idx_a, const index_t idx_b) const
Definition: triplet_form.hpp:41
csr_comparator(const index_t *const in_row_idx, const index_t *const in_col_idx)
Definition: triplet_form.hpp:37
Definition: csr_form.hpp:25
Definition: triplet_form.hpp:62
triplet_form< data_t, index_t > & operator/=(T2)
Definition: triplet_form.hpp:526
void csr_sort()
Definition: triplet_form.hpp:402
triplet_form< data_t, index_t > strictly_lower() const
Definition: triplet_form.hpp:591
void zeros()
Definition: triplet_form.hpp:176
data_t * val_mem()
Definition: triplet_form.hpp:157
void assemble(const triplet_form< in_dt, in_it > &, index_t, index_t, data_t)
Definition: triplet_form.hpp:472
triplet_form()=default
const index_t n_rows
Definition: triplet_form.hpp:128
const index_t n_alloc
Definition: triplet_form.hpp:131
data_t max() const
Definition: triplet_form.hpp:171
triplet_form< data_t, index_t > & operator+=(const triplet_form< data_t, index_t > &)
Definition: triplet_form.hpp:537
const data_t & operator()(const index_t row, const index_t col) const
Definition: triplet_form.hpp:192
void csc_condense()
Definition: triplet_form.hpp:209
void csc_sort()
Definition: triplet_form.hpp:428
triplet_form< data_t, index_t > strictly_upper() const
Definition: triplet_form.hpp:583
Mat< data_t > operator*(const Mat< data_t > &in_mat) const
Definition: triplet_form.hpp:245
triplet_form(triplet_form &&) noexcept
Definition: triplet_form.hpp:318
triplet_form< data_t, index_t > diagonal() const
Definition: triplet_form.hpp:575
bool is_empty() const
Definition: triplet_form.hpp:169
data_t & at(index_t, index_t)
Definition: triplet_form.hpp:382
void full_csr_condense()
Definition: triplet_form.hpp:214
void csr_condense()
Definition: triplet_form.hpp:204
bool is_csc_sorted() const
Definition: triplet_form.hpp:167
triplet_form(const SpMat< in_dt > &)
Definition: triplet_form.hpp:355
triplet_form(const triplet_form &)
Definition: triplet_form.hpp:308
index_t * row_mem()
Definition: triplet_form.hpp:153
data_t data_type
Definition: triplet_form.hpp:121
triplet_form & operator=(const triplet_form &)
Definition: triplet_form.hpp:329
index_t * col_mem()
Definition: triplet_form.hpp:155
triplet_form< data_t, index_t > operator*(T2) const
Definition: triplet_form.hpp:486
const index_t n_cols
Definition: triplet_form.hpp:129
triplet_form< data_t, index_t > & operator*=(T2)
Definition: triplet_form.hpp:514
triplet_form< data_t, index_t > operator-(const triplet_form< data_t, index_t > &in_mat) const
Definition: triplet_form.hpp:263
const data_t * val_mem() const
Definition: triplet_form.hpp:151
Mat< data_t > operator*(const Col< data_t > &in_mat) const
Definition: triplet_form.hpp:237
void init(const index_t in_rows, const index_t in_cols, const index_t in_elem)
Definition: triplet_form.hpp:186
const index_t n_elem
Definition: triplet_form.hpp:130
void init(const index_t in_elem)
Definition: triplet_form.hpp:181
const index_t * row_mem() const
Definition: triplet_form.hpp:147
void print() const
Definition: triplet_form.hpp:392
Col< data_t > diag() const
Definition: triplet_form.hpp:569
friend class triplet_form
Definition: triplet_form.hpp:126
bool is_csr_sorted() const
Definition: triplet_form.hpp:165
index_t index_type
Definition: triplet_form.hpp:122
void assemble(const triplet_form< in_dt, in_it > &in_mat, const std::vector< index_t > &row_shift, const std::vector< index_t > &col_shift, const std::vector< data_t > &scalar)
Definition: triplet_form.hpp:229
index_t col(const index_t I) const
Definition: triplet_form.hpp:161
data_t val(const index_t I) const
Definition: triplet_form.hpp:163
void assemble(const Mat< data_t > &, const Col< uword > &)
Definition: triplet_form.hpp:454
const index_t * col_mem() const
Definition: triplet_form.hpp:149
index_t row(const index_t I) const
Definition: triplet_form.hpp:159
triplet_form< data_t, index_t > & operator-=(const triplet_form< data_t, index_t > &)
Definition: triplet_form.hpp:553
void full_csc_condense()
Definition: triplet_form.hpp:220
triplet_form(triplet_form< in_dt, in_it > &, SparseBase=SparseBase::ZERO, bool=false)
Definition: triplet_form.hpp:363
triplet_form< data_t, index_t > operator+(const triplet_form< data_t, index_t > &in_mat) const
Definition: triplet_form.hpp:258
triplet_form< data_t, index_t > operator/(T2) const
Definition: triplet_form.hpp:501
Definition: suanPan.h:318
Definition: suanPan.h:319
std::enable_if_t<!std::numeric_limits< T >::is_integer, bool > approx_equal(T x, T y, int ulp=2)
Definition: utility.h:58
#define suanpan_info
Definition: suanPan.h:293
#define suanpan_for_each
Definition: suanPan.h:177
void suanpan_assert(const std::function< void()> &F)
Definition: suanPan.h:284
#define suanpan_sort
Definition: suanPan.h:176
SparseBase
Definition: triplet_form.hpp:27
triplet_form< data_t, index_t > operator+(const triplet_form< data_t, index_t > &mat_a, const triplet_form< data_t, index_t > &mat_b)
Definition: triplet_form.hpp:599
constexpr T suanpan_max_element(T start, T end)
Definition: utility.h:36
void suanpan_for(const IT start, const IT end, F &&FN)
Definition: utility.h:27