forked from munuxi/SparseRREF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_type.h
More file actions
2844 lines (2562 loc) · 95.9 KB
/
sparse_type.h
File metadata and controls
2844 lines (2562 loc) · 95.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2025 Zhenjie Li (Li, Zhenjie), Xiang Li (Li, Xiang)
This file is part of SparseRREF. The SparseRREF is free software:
you can redistribute it and/or modify it under the terms of the MIT
License.
*/
#ifndef SPARSE_TYPE_H
#define SPARSE_TYPE_H
#include "sparse_rref.h"
#include "scalar.h"
namespace SparseRREF {
enum SPARSE_TYPE {
SPARSE_CSR, // Compressed sparse row
SPARSE_COO, // Coordinate list
SPARSE_LR // List of rows
};
template <Flint::builtin_integral index_t = int>
struct pivot_t {
index_t r;
index_t c;
};
// sparse vector
template <typename T, Flint::builtin_integral index_t = int> struct sparse_vec {
index_t* indices = nullptr;
T* entries = nullptr;
size_t _nnz = 0;
size_t _alloc = 0;
struct de_iterator_ref {
index_t& ind;
T& val;
};
struct iterator {
index_t* ind_ptr;
T* val_ptr;
iterator& operator++() { ind_ptr++; val_ptr++; return *this; }
iterator operator++(int) { iterator tmp = *this; ind_ptr++; val_ptr++; return tmp; }
iterator& operator--() { ind_ptr--; val_ptr--; return *this; }
iterator operator--(int) { iterator tmp = *this; ind_ptr--; val_ptr--; return tmp; }
iterator& operator+=(size_t n) { ind_ptr += n; val_ptr += n; return *this; }
iterator& operator-=(size_t n) { ind_ptr -= n; val_ptr -= n; return *this; }
iterator operator+(size_t n) const { iterator tmp = *this; tmp += n; return tmp; }
iterator operator-(size_t n) const { iterator tmp = *this; tmp -= n; return tmp; }
bool operator==(const iterator& other) const { return ind_ptr == other.ind_ptr; }
bool operator!=(const iterator& other) const { return ind_ptr != other.ind_ptr; }
de_iterator_ref operator*() const { return { *ind_ptr, *val_ptr }; }
};
// functions of iterator
iterator begin() { return { indices, entries }; }
iterator end() { return { indices + _nnz, entries + _nnz }; }
iterator begin() const { return { indices, entries }; }
iterator end() const { return { indices + _nnz, entries + _nnz }; }
iterator cbegin() const { return { indices, entries }; }
iterator cend() const { return { indices + _nnz, entries + _nnz }; }
auto index_span() const { return std::span<index_t>(indices, _nnz); }
auto entry_span() const { return std::span<T>(entries, _nnz); }
// C++23 is needed for zip_view
// auto index_view() const { return std::ranges::subrange(indices, indices + _nnz); }
// auto entry_view() const { return std::ranges::subrange(entries, entries + _nnz); }
// auto combine_view() const { return std::ranges::zip_view(index_view(), entry_view()); }
sparse_vec() {
indices = nullptr;
entries = nullptr;
_nnz = 0;
_alloc = 0;
}
void clear() {
if (_alloc == 0)
return;
s_free(indices);
indices = nullptr;
for (size_t i = 0; i < _alloc; i++)
entries[i].~T();
s_free(entries);
entries = nullptr;
_alloc = 0;
_nnz = 0;
}
~sparse_vec() {
clear();
}
void reserve(size_t n, const bool is_copy = true) {
if (n == _alloc)
return;
if (n == 0) {
clear();
return;
}
if (_alloc == 0) {
indices = s_malloc<index_t>(n);
entries = s_malloc<T>(n);
for (size_t i = 0; i < n; i++)
new (entries + i) T();
_alloc = n;
return;
}
// when expanding or using realloc, sometimes we need to copy the old data
// if is_copy is false, we do not make sure that the old data is copied to
// the new memory, it is useful when we just want to enlarge the memory
if (!is_copy && n > _alloc) {
auto ii = s_expand(indices, n);
auto ee = s_expand(entries, n);
if (ii == nullptr) {
s_free(indices);
indices = s_malloc<index_t>(n);
}
else {
indices = ii;
}
if (ee == nullptr) {
for (size_t i = 0; i < _alloc; i++) {
entries[i].~T();
}
s_free(entries);
entries = s_malloc<T>(n);
for (size_t i = 0; i < n; i++) {
new (entries + i) T();
}
}
else {
entries = ee;
for (size_t i = _alloc; i < n; i++) {
new (entries + i) T();
}
}
_alloc = n;
_nnz = 0;
return;
}
indices = s_realloc(indices, n);
if (n < _alloc) {
for (size_t i = n; i < _alloc; i++)
entries[i].~T();
entries = s_realloc<T>(entries, n);
}
else {
entries = s_realloc<T>(entries, n);
for (size_t i = _alloc; i < n; i++)
new (entries + i) T();
}
_alloc = n;
}
inline void zero() { _nnz = 0; }
inline void resize(size_t n) { _nnz = n; }
inline void copy(const sparse_vec& l) {
if (this == &l)
return;
if (_alloc < l._nnz)
reserve(l._nnz);
for (size_t i = 0; i < l._nnz; i++) {
indices[i] = l.indices[i];
entries[i] = l.entries[i];
}
_nnz = l._nnz;
}
inline sparse_vec(const sparse_vec& l) { copy(l); }
inline size_t nnz() const { return _nnz; }
inline size_t size() const { return _nnz; }
inline size_t alloc() const { return _alloc; }
sparse_vec(sparse_vec&& l) noexcept {
indices = l.indices;
entries = l.entries;
_nnz = l._nnz;
_alloc = l._alloc;
l.indices = nullptr;
l.entries = nullptr;
l._nnz = 0;
l._alloc = 0;
}
sparse_vec& operator=(const sparse_vec& l) {
if (this == &l)
return *this;
copy(l);
return *this;
}
sparse_vec& operator=(sparse_vec&& l) noexcept {
if (this == &l)
return *this;
clear();
indices = l.indices;
entries = l.entries;
_nnz = l._nnz;
_alloc = l._alloc;
l.indices = nullptr;
l.entries = nullptr;
l._nnz = 0;
l._alloc = 0;
return *this;
}
// this comparison does not clear zero elements / sort the order of indices
bool operator==(const sparse_vec& l) const {
if (this == &l)
return true;
if (_nnz != l._nnz)
return false;
return std::equal(indices, indices + _nnz, l.indices)
&& std::equal(entries, entries + _nnz, l.entries);
}
// this comparison will clear zero elements / sort the order of indices
bool is_equal_to(const sparse_vec& l) const {
if (this == &l)
return true;
auto this_temp = *this;
auto other_temp = l;
this_temp.canonicalize();
other_temp.canonicalize();
if (this_temp._nnz != other_temp._nnz)
return false;
this_temp.sort_indices();
other_temp.sort_indices();
return std::equal(this_temp.indices, this_temp.indices + this_temp._nnz, other_temp.indices)
&& std::equal(this_temp.entries, this_temp.entries + this_temp._nnz, other_temp.entries);
}
inline void push_back(const index_t index, const T& val) {
if (_nnz + 1 > _alloc)
reserve((1 + _alloc) * 2); // +1 to avoid _alloc = 0
indices[_nnz] = index;
entries[_nnz] = val;
_nnz++;
}
inline void push_back(const index_t index, T&& val) {
if (_nnz + 1 > _alloc)
reserve((1 + _alloc) * 2); // +1 to avoid _alloc = 0
indices[_nnz] = index;
entries[_nnz] = std::move(val);
_nnz++;
}
void pop_back() {
if (_nnz != 0)
_nnz--;
}
// take a span of elements
// sparse_vec.take({start, end}) returns a sparse_vec with elements indexed in [start, end)
// elements in the resulting sparse_vec are reindexed in [0, end - start)
sparse_vec<T, index_t> take(const std::pair<index_t, index_t>& span, const bool reserve_nnz = true) const {
if (span.first < 0 || span.second < 0)
throw std::invalid_argument("sparse_vec.take: expect non-negative indices.");
if (span.first > span.second)
throw std::invalid_argument("sparse_vec.take: invalid span.");
sparse_vec<T, index_t> result;
if (reserve_nnz)
result.reserve(_nnz);
for (size_t i = 0; i < _nnz; i++) {
if (indices[i] >= span.first && indices[i] < span.second) {
result.push_back(indices[i] - span.first, entries[i]);
}
}
result.compress();
return result;
}
index_t& operator()(const size_t pos) { return indices[pos]; }
const index_t& operator()(const size_t pos) const { return indices[pos]; }
T& operator[](const size_t pos) { return entries[pos]; }
const T& operator[](const size_t pos) const { return entries[pos]; }
T* find(const index_t index, const bool isbinary = true) const {
if (_nnz == 0)
return nullptr;
index_t* ptr;
if (isbinary)
ptr = SparseRREF::binary_search(indices, indices + _nnz, index);
else
ptr = std::find(indices, indices + _nnz, index);
if (ptr == indices + _nnz)
return nullptr;
return entries + (ptr - indices);
}
// conversion functions
template <typename U = T> requires std::is_integral_v<U> || std::is_same_v<U, int_t>
operator sparse_vec<rat_t, index_t>() {
sparse_vec<rat_t, index_t> result;
result.reserve(_nnz);
result.resize(_nnz);
for (size_t i = 0; i < _nnz; i++) {
result.indices[i] = indices[i];
result.entries[i] = entries[i];
}
return result;
}
template <typename U = T> requires std::is_integral_v<U>
operator sparse_vec<int_t, index_t>() {
sparse_vec<int_t, index_t> result;
result.reserve(_nnz);
result.resize(_nnz);
for (size_t i = 0; i < _nnz; i++) {
result.indices[i] = indices[i];
result.entries[i] = entries[i];
}
return result;
}
template <typename U = T> requires std::is_same_v<U, rat_t>
sparse_vec<ulong, index_t> operator%(const nmod_t mod) const {
sparse_vec<ulong, index_t> result;
result.reserve(_nnz);
result.resize(_nnz);
for (size_t i = 0; i < _nnz; i++) {
result.indices[i] = indices[i];
result.entries[i] = entries[i] % mod;
}
return result;
}
template <typename U = T> requires std::is_same_v<U, rat_t>
sparse_vec<ulong, index_t> operator%(const ulong p) const {
sparse_vec<ulong, index_t> result;
nmod_t mod;
nmod_init(&mod, p);
result.reserve(_nnz);
result.resize(_nnz);
for (size_t i = 0; i < _nnz; i++) {
result.indices[i] = indices[i];
result.entries[i] = entries[i] % mod;
}
return result;
}
void canonicalize() {
size_t new_nnz = 0;
for (size_t i = 0; i < _nnz; i++) {
if (entries[i] != 0) {
if (new_nnz != i) {
indices[new_nnz] = indices[i];
entries[new_nnz] = entries[i];
}
new_nnz++;
}
}
_nnz = new_nnz;
}
void sort_indices() {
if (_nnz <= 1 || std::is_sorted(indices, indices + _nnz))
return;
auto perm = perm_init(_nnz);
std::ranges::sort(perm, [&](index_t a, index_t b) {
return indices[a] < indices[b];
});
permute(perm, indices);
permute(perm, entries);
}
void compress() {
canonicalize();
sort_indices();
reserve(_nnz);
}
};
template <Flint::builtin_integral index_t> struct sparse_vec<bool, index_t> {
index_t* indices = nullptr;
size_t _nnz = 0;
size_t _alloc = 0;
auto index_span() const { return std::span<index_t>(indices, _nnz); }
sparse_vec() {
indices = nullptr;
_nnz = 0;
_alloc = 0;
}
void clear() {
if (_alloc != 0)
s_free(indices);
_alloc = 0;
_nnz = 0;
}
~sparse_vec() { clear(); }
void reserve(size_t n) {
if (n == _alloc)
return;
if (n == 0) {
clear();
return;
}
if (_alloc == 0) {
indices = s_malloc<index_t>(n);
_alloc = n;
return;
}
indices = s_realloc(indices, n);
_alloc = n;
}
void resize(size_t n) { _nnz = n; }
inline void copy(const sparse_vec& l) {
if (this == &l)
return;
if (_alloc < l._nnz)
reserve(l._nnz);
for (size_t i = 0; i < l._nnz; i++) {
indices[i] = l.indices[i];
}
_nnz = l._nnz;
}
sparse_vec(const sparse_vec& l) { copy(l); }
size_t nnz() const { return _nnz; }
size_t alloc() const { return _alloc; }
sparse_vec(sparse_vec&& l) noexcept {
indices = l.indices;
_nnz = l._nnz;
_alloc = l._alloc;
l.indices = nullptr;
l._nnz = 0;
l._alloc = 0;
}
sparse_vec& operator=(const sparse_vec& l) {
if (this == &l)
return *this;
copy(l);
return *this;
}
sparse_vec& operator=(sparse_vec&& l) noexcept {
if (this == &l)
return *this;
clear();
indices = l.indices;
_nnz = l._nnz;
_alloc = l._alloc;
l.indices = nullptr;
l._nnz = 0;
l._alloc = 0;
return *this;
}
void push_back(const index_t index, const bool val = true) {
if (_nnz + 1 > _alloc)
reserve((1 + _alloc) * 2); // +1 to avoid _alloc = 0
indices[_nnz] = index;
_nnz++;
}
void pop_back() {
if (_nnz != 0)
_nnz--;
}
index_t& operator()(const size_t pos) { return indices[pos]; }
const index_t& operator()(const size_t pos) const { return indices[pos]; }
void zero() { _nnz = 0; }
void sort_indices() { std::sort(indices, indices + _nnz); }
void canonicalize() {}
void compress() { sort_indices(); }
};
template <typename T, Flint::builtin_integral index_t = int> struct sparse_mat {
size_t nrow = 0;
size_t ncol = 0;
std::vector<sparse_vec<T, index_t>> rows;
void init(size_t r, size_t c) {
nrow = r;
ncol = c;
rows = std::vector<sparse_vec<T, index_t>>(r);
}
sparse_mat() { nrow = 0; ncol = 0; }
~sparse_mat() {}
sparse_mat(size_t r, size_t c) { init(r, c); }
sparse_vec<T, index_t>& operator[](size_t i) { return rows[i]; }
const sparse_vec<T, index_t>& operator[](size_t i) const { return rows[i]; }
T* find(const size_t row, const index_t col, const bool isbinary = true) const {
return rows[row].find(col, isbinary);
}
sparse_mat(const sparse_mat& l) {
init(l.nrow, l.ncol);
rows = l.rows;
}
sparse_mat(sparse_mat&& l) noexcept {
nrow = l.nrow;
ncol = l.ncol;
rows = std::move(l.rows);
l.nrow = 0;
}
sparse_mat& operator=(const sparse_mat& l) {
if (this == &l)
return *this;
nrow = l.nrow;
ncol = l.ncol;
rows = l.rows;
return *this;
}
sparse_mat& operator=(sparse_mat&& l) noexcept {
if (this == &l)
return *this;
nrow = l.nrow;
ncol = l.ncol;
rows = std::move(l.rows);
l.nrow = 0;
return *this;
}
void zero() {
for (size_t i = 0; i < nrow; i++)
rows[i].zero();
}
void clear() {
for (size_t i = 0; i < nrow; i++) {
rows[i].clear();
}
std::vector<sparse_vec<T, index_t>> tmp;
rows.swap(tmp); // clear the vector and free memory
nrow = 0;
ncol = 0;
}
size_t nnz() const {
size_t n = 0;
for (size_t i = 0; i < nrow; i++)
n += rows[i].nnz();
return n;
}
size_t alloc() const {
size_t n = 0;
for (size_t i = 0; i < nrow; i++)
n += rows[i].alloc();
return n;
}
void compress(thread_pool* pool = nullptr) {
if (pool == nullptr) {
for (size_t i = 0; i < nrow; i++) {
rows[i].compress();
}
}
else {
pool->detach_loop(0, nrow, [&](size_t i) {
rows[i].compress();
});
pool->wait();
}
}
void clear_zero_row() {
size_t new_nrow = 0;
for (size_t i = 0; i < nrow; i++) {
if (rows[i].nnz() != 0) {
std::swap(rows[new_nrow], rows[i]);
new_nrow++;
}
}
nrow = new_nrow;
rows.resize(nrow);
rows.shrink_to_fit();
}
sparse_mat<T, index_t> transpose() {
sparse_mat<T, index_t> res(ncol, nrow);
for (size_t i = 0; i < ncol; i++)
res[i].zero();
for (size_t i = 0; i < nrow; i++) {
for (size_t j = 0; j < rows[i].nnz(); j++) {
res[rows[i](j)].push_back(i, rows[i][j]);
}
}
return res;
}
// take a span of rows
// sparse_mat.take({start, end}) returns a sparse_mat with rows indexed in [start, end)
sparse_mat<T, index_t> take(const std::pair<size_t, size_t>& span, thread_pool* pool = nullptr) const {
if (span.second > nrow) {
throw std::out_of_range("sparse_mat.take: [start, end) out of [0, nrow).");
}
if (span.first > span.second) {
throw std::invalid_argument("sparse_mat.take: invalid span.");
}
sparse_mat<T, index_t> res(span.second - span.first, ncol);
if (pool == nullptr) {
res.rows.assign(rows.begin() + span.first, rows.begin() + span.second);
}
else {
pool->detach_loop(span.first, span.second, [&](size_t i) {
res[i - span.first] = rows[i];
});
pool->wait();
}
return res;
}
// take a span of elements
// sparse_mat.take(levelspec, {start, end}) returns a sparse_mat whose elements have the levelspec-th index in range [start, end)
// elements in the resulting sparse_mat have their levelspec-th indices reindexed in [0, end - start)
sparse_mat<T, index_t> take(const size_t levelspec, const std::pair<index_t, index_t>& span, thread_pool* pool = nullptr) const {
if (span.first < 0 || span.second < 0)
throw std::invalid_argument("sparse_mat.take: expect non-negative indices.");
if (span.first > span.second)
throw std::invalid_argument("sparse_mat.take: invalid span.");
if (levelspec > 1)
throw std::invalid_argument("sparse_mat.take: levelspec must be 0 or 1.");
if (levelspec == 0)
return take(span, pool);
// then levelspec == 1
if (span.second > ncol)
throw std::out_of_range("sparse_mat.take: [start, end) out of [0, ncol).");
sparse_mat<T, index_t> res(nrow, span.second - span.first);
if (pool == nullptr) {
for (size_t i = 0; i < nrow; i++) {
res.rows[i] = rows[i].take(span);
}
}
else {
pool->detach_loop(0, nrow, [&](size_t i) {
res.rows[i] = rows[i].take(span);
});
pool->wait();
}
return res;
}
// if is_binary is true, we assume that the column indices are sorted in each row
sparse_mat<T, index_t> submat(const std::pair<size_t, size_t>& rowspan,
const std::pair<size_t, size_t>& colspan, const bool is_binary = true) const {
if (colspan.second < colspan.first || rowspan.second < rowspan.first) {
std::cerr << "sparse_mat.submat: invalid span." << std::endl;
return sparse_mat<T, index_t>();
}
sparse_mat<T, index_t> res(rowspan.second - rowspan.first, colspan.second - colspan.first);
if (is_binary) {
for (size_t i = rowspan.first; i < rowspan.second; i++) {
// binary search for the starting and end position
auto indptr = rows[i].indices;
auto it_start = std::lower_bound(indptr, indptr + rows[i].nnz(), colspan.first);
auto it_end = std::lower_bound(it_start, indptr + rows[i].nnz(), colspan.second);
res[i - rowspan.first].reserve(it_end - it_start);
for (auto it = it_start; it != it_end; it++) {
auto pos = it - indptr;
res[i - rowspan.first].push_back(indptr[pos] - colspan.first, rows[i].entries[pos]);
}
}
}
else {
for (size_t i = rowspan.first; i < rowspan.second; i++) {
// use more memory but simpler
res[i - rowspan.first].reserve(rows[i].nnz());
for (size_t j = 0; j < rows[i].nnz(); j++) {
auto col_index = rows[i](j);
if (col_index >= colspan.first && col_index < colspan.second)
res[i - rowspan.first].push_back(col_index - colspan.first, rows[i][j]);
}
}
}
return res;
}
// append other sparse_mat to this one
void append(const sparse_mat<T, index_t>& other, thread_pool* pool = nullptr) {
ncol = std::max(ncol, other.ncol);
if (pool == nullptr) {
rows.insert(rows.end(), other.rows.begin(), other.rows.end());
nrow += other.nrow;
return;
}
rows.reserve(nrow + other.nrow);
pool->detach_loop(0, other.nrow, [&](size_t i) {
rows.emplace_back(other.rows[i]);
});
pool->wait();
nrow += other.nrow;
}
void append(sparse_mat<T, index_t>&& other) {
ncol = std::max(ncol, other.ncol);
rows.insert(rows.end(), std::make_move_iterator(other.rows.begin()), std::make_move_iterator(other.rows.end()));
nrow += other.nrow;
other.clear(); // clear the other matrix
}
// sort rows by nnz
void sort_rows_by_nnz() {
std::ranges::stable_sort(rows, std::less{}, &sparse_vec<T, index_t>::_nnz);
}
template <typename U = T> requires std::is_same_v<U, rat_t>
sparse_mat<ulong> operator%(const nmod_t mod) const {
sparse_mat<ulong> result(nrow, ncol);
for (size_t i = 0; i < nrow; i++) {
result[i] = rows[i] % mod;
}
return result;
}
template <typename U = T> requires std::is_same_v<U, rat_t>
ulong height_bits() const {
ulong max_height = 0;
for (size_t i = 0; i < nrow; i++) {
for (size_t j = 0; j < rows[i].nnz(); j++) {
auto rr = rows[i][j].height_bits();
if (rr > max_height)
max_height = rr;
}
}
return max_height;
}
};
// CSR format for sparse tensor
template <typename T, typename index_t> struct sparse_tensor_struct {
size_t rank;
size_t alloc;
index_t* colptr;
T* valptr;
std::vector<size_t> dims;
std::vector<size_t> rowptr;
using index_v = std::vector<index_t>;
using index_p = index_t*;
using const_index_p = const index_t*;
using interval_t = std::pair<index_t, index_t>;
// empty constructor
sparse_tensor_struct() {
rank = 0;
alloc = 0;
colptr = nullptr;
valptr = nullptr;
}
// Constructor with dimensions
// we require that rank >= 2
void init(const std::vector<size_t>& l, size_t aoc = 8) {
dims = l;
rank = l.size();
rowptr = std::vector<size_t>(l[0] + 1, 0);
alloc = aoc;
colptr = s_malloc<index_t>((rank - 1) * alloc);
valptr = s_malloc<T>(alloc);
for (size_t i = 0; i < alloc; i++)
new (valptr + i) T();
}
sparse_tensor_struct(const std::vector<size_t>& l, size_t aoc = 8) {
init(l, aoc);
}
// Copy constructor
sparse_tensor_struct(const sparse_tensor_struct& l) {
init(l.dims, l.alloc);
std::copy(l.rowptr.begin(), l.rowptr.end(), rowptr.begin());
std::copy(l.colptr, l.colptr + alloc * (rank - 1), colptr);
for (size_t i = 0; i < alloc; i++)
valptr[i] = l.valptr[i];
}
// Move constructor
sparse_tensor_struct(sparse_tensor_struct&& l) noexcept {
dims = l.dims;
rank = l.rank;
rowptr = l.rowptr;
alloc = l.alloc;
colptr = l.colptr;
l.colptr = nullptr;
valptr = l.valptr;
l.valptr = nullptr;
l.alloc = 0; // important for no repeating clear
}
void clear() {
if (alloc == 0)
return;
for (size_t i = 0; i < alloc; i++)
valptr[i].~T();
s_free(valptr);
s_free(colptr);
valptr = nullptr;
colptr = nullptr;
alloc = 0;
}
~sparse_tensor_struct() {
clear();
}
void reserve(size_t size) {
if (size == alloc)
return;
if (size == 0) {
clear();
return;
}
if (alloc == 0) {
alloc = size;
colptr = s_malloc<index_t>(size * (rank - 1));
valptr = s_malloc<T>(size);
for (size_t i = 0; i < size; i++)
new (valptr + i) T();
return;
}
colptr = s_realloc<index_t>(colptr, size * (rank - 1));
if (size > alloc) {
valptr = s_realloc<T>(valptr, size);
for (size_t i = alloc; i < size; i++)
new (valptr + i) T();
}
else if (size < alloc) {
for (size_t i = size; i < alloc; i++)
valptr[i].~T();
valptr = s_realloc<T>(valptr, size);
}
alloc = size;
}
void zero() {
if (rank != 0)
std::fill(rowptr.begin(), rowptr.end(), 0);
}
inline size_t nnz() const {
return rowptr[dims[0]];
}
// Copy assignment
sparse_tensor_struct& operator=(const sparse_tensor_struct& l) {
if (this == &l)
return *this;
auto nz = l.nnz();
if (alloc == 0) {
init(l.dims, nz);
std::copy(l.rowptr.begin(), l.rowptr.end(), rowptr.begin());
std::copy(l.colptr, l.colptr + nz * (rank - 1), colptr);
std::copy(l.valptr, l.valptr + nz, valptr);
return *this;
}
dims = l.dims;
rank = l.rank;
rowptr = l.rowptr;
if (alloc < nz)
reserve(nz);
std::copy(l.colptr, l.colptr + nz * (rank - 1), colptr);
std::copy(l.valptr, l.valptr + nz, valptr);
return *this;
}
// Move assignment
sparse_tensor_struct& operator=(sparse_tensor_struct&& l) noexcept {
if (this == &l)
return *this;
clear();
dims = l.dims;
rank = l.rank;
rowptr = l.rowptr;
alloc = l.alloc;
colptr = l.colptr;
l.colptr = nullptr;
valptr = l.valptr;
l.valptr = nullptr;
l.alloc = 0; // important for no repeating clear
return *this;
}
std::vector<size_t> row_nums() const {
return SparseRREF::difference(rowptr);
}
// nnz in the i-th row
size_t row_nnz(size_t i) const {
return rowptr[i + 1] - rowptr[i];
}
// row index of the i-th entry
size_t row_index(size_t i) const {
auto it = std::upper_bound(rowptr.begin(), rowptr.end(), i);
return std::distance(rowptr.begin(), it) - 1;
}
// remove zero entries, double pointer
void canonicalize() {
size_t nnz_now = nnz();
size_t index = 0;
std::vector<size_t> newrowptr(dims[0] + 1);
newrowptr[0] = 0;
for (size_t i = 0; i < dims[0]; i++) {
for (size_t j = rowptr[i]; j < rowptr[i + 1]; j++) {
if (valptr[j] != 0) {
s_copy(colptr + index * (rank - 1), colptr + j * (rank - 1), rank - 1);
valptr[index] = valptr[j];
index++;
}
}
newrowptr[i + 1] = index;
}
rowptr = newrowptr;
}
std::pair<index_p, T*> row(size_t i) {
return std::make_pair(colptr + rowptr[i] * (rank - 1), valptr + rowptr[i]);
}
index_p entry_lower_bound(const_index_p l) {
auto begin = row(l[0]).first;
auto end = row(l[0] + 1).first;
if (begin == end)
return end;
return SparseRREF::lower_bound(begin, end, l + 1, rank - 1);
}
index_p entry_lower_bound(const index_v& l) {
return entry_lower_bound(l.data());
}
index_p entry_ptr(index_p l) {
auto ptr = entry_lower_bound(l);
auto end = row(l[0] + 1).first;
if (ptr == end || std::equal(ptr, ptr + rank - 1, l + 1))
return ptr;
else
return end;
}
index_p entry_ptr(const index_v& l) {
return entry_ptr(l.data());
}
// unordered, push back on the end of the row
void push_back(const index_v& l, const T& val) {
index_t row = l[0];
size_t nnz = this->nnz();
if (nnz + 1 > alloc)
reserve((alloc + 1) * 2);
size_t index = rowptr[row + 1];
for (size_t i = nnz; i > index; i--) {