suanPan
Loading...
Searching...
No Matches
sort_rcm.h
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 ******************************************************************************/
40#ifndef RCM_H
41#define RCM_H
42
45
46uvec sort_rcm(const std::vector<uvec>&, const uvec&);
47
48template<typename eT> uvec sort_rcm(const SpMat<eT>& MEAT) {
49 suanpan_assert([&] { if(!MEAT.is_square()) throw logic_error("can only be applied to square matrix"); });
50
51 wall_clock TM;
52 TM.tic();
53
55 auto S = MEAT.n_cols;
56
58 uvec E(S, fill::none);
59 suanpan_for(0llu, S, [&](const uword I) { E(I) = MEAT.col(I).n_nonzero; });
60
61 std::vector<uvec> A(S);
62 suanpan_for(0llu, S, [&](const uword K) {
63 unsigned J = 0;
64 uvec IDX(E(K));
65 for(auto L = MEAT.begin_col(K); L != MEAT.end_col(K); ++L) IDX(J++) = L.row();
66 A[K] = IDX(sort_index(E(IDX)));
67 });
68
70 uvec G = sort_index(E);
71
73 std::vector<bool> M(S, false);
75 uvec R(S, fill::zeros);
76
81
84 uword IDXA = 0, IDXB = S - 1, IDXC = S - 1;
85
88
94 while(IDXA < S) {
95 if(IDXB == IDXC) {
97 while(IDXA < S && M[G(IDXA)]) ++IDXA;
100 if(IDXA == S) break;
102 R(IDXC--) = G(IDXA);
104 M[G(IDXA++)] = true;
105 }
111 for(const auto& IDX : A[R(IDXB--)]) if(!M[IDX]) M[R(IDXC--) = IDX] = true;
112 }
113
114 suanpan_debug("RCM algorithm takes {:.5E} seconds.\n", TM.toc());
115
116 return R;
117}
118
119template<typename eT> uvec sort_rcm(const Mat<eT>& MEAT) { return sort_rcm(SpMat<eT>(MEAT)); }
120
121template<typename dt> uvec sort_rcm(const csc_form<dt, uword>& csc_mat) {
123 auto S = csc_mat.n_cols;
124
126 uvec E(S, fill::none);
127 suanpan_for(0llu, S, [&](const uword I) { E(I) = csc_mat.col(I + 1) - csc_mat.col(I); });
128 std::vector<uvec> A(S);
129 suanpan_for(0llu, S, [&](const uword I) {
130 const uvec IDX(csc_mat.row_mem() + csc_mat.col(I), E(I));
131 A[I] = IDX(sort_index(E(IDX)));
132 });
133
134 return sort_rcm(A, E);
135}
136
137template<typename dt, typename it> uvec sort_rcm(triplet_form<dt, it>& triplet_mat) {
138 csc_form<dt, uword> csc_mat(triplet_mat);
139
140 return sort_rcm(csc_mat);
141}
142
143#endif
144
Definition csc_form.hpp:25
const index_t n_cols
Definition csc_form.hpp:51
const index_t * row_mem() const
Definition csc_form.hpp:61
index_t col(const index_t I) const
Definition csc_form.hpp:75
Definition triplet_form.hpp:62
uvec sort_rcm(const std::vector< uvec > &, const uvec &)
Definition sort_rcm.cpp:20
#define suanpan_debug(...)
Definition suanPan.h:307
void suanpan_assert(const std::function< void()> &F)
Definition suanPan.h:296
void suanpan_for(const IT start, const IT end, F &&FN)
Definition utility.h:27