-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsparse_vec.h
More file actions
380 lines (325 loc) · 9.34 KB
/
sparse_vec.h
File metadata and controls
380 lines (325 loc) · 9.34 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
/*
Copyright (C) 2024-2025 Zhenjie Li (Li, Zhenjie)
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_VEC_H
#define SPARSE_VEC_H
#include "scalar.h"
#include "sparse_type.h"
namespace SparseRREF {
template <typename index_t> using snmod_vec = sparse_vec<ulong, index_t>;
template <typename index_t> using sfmpq_vec = sparse_vec<rat_t, index_t>;
template <typename index_t, typename T>
inline void sparse_vec_rescale(sparse_vec<T, index_t>& vec, const T& scalar, const field_t& F) {
if (scalar == 1)
return;
if constexpr (std::is_same_v<T, ulong>) {
ulong e_pr = n_mulmod_precomp_shoup(scalar, F.mod.n);
for (size_t i = 0; i < vec.nnz(); i++)
vec.entries[i] = n_mulmod_shoup(scalar, vec.entries[i], e_pr, F.mod.n);
}
else if constexpr (Flint::IsOneOf<T, int_t, rat_t>) {
for (size_t i = 0; i < vec.nnz(); i++)
vec.entries[i] *= scalar;
}
}
template <typename index_t>
void sparse_vec_rescale(sparse_vec<bool, index_t>& vec, const bool scalar, const field_t& F) {
if (scalar == false)
vec.zero();
}
// we assume that vec and src are sorted, and the result is also sorted
template <typename index_t>
int snmod_vec_add_mul(
snmod_vec<index_t>& vec, const snmod_vec<index_t>& src,
const ulong a, const field_t& F) {
if (src.nnz() == 0)
return 0;
auto p = F.mod;
if (vec.nnz() == 0) {
vec = src;
sparse_vec_rescale(vec, a, F);
return 0;
}
ulong na = a;
ulong na_pr = n_mulmod_precomp_shoup(na, p.n);
size_t ptr1 = vec.nnz();
size_t ptr2 = src.nnz();
size_t ptr = vec.nnz() + src.nnz();
if (vec.alloc() < ptr)
vec.reserve(ptr);
vec.resize(ptr);
while (ptr1 > 0 && ptr2 > 0) {
if (vec(ptr1 - 1) == src(ptr2 - 1)) {
auto entry =
_nmod_add(vec[ptr1 - 1],
n_mulmod_shoup(na, src[ptr2 - 1], na_pr, p.n), p);
if (entry != 0) {
vec(ptr - 1) = vec(ptr1 - 1);
vec[ptr - 1] = entry;
ptr--;
}
ptr1--;
ptr2--;
}
else if (vec(ptr1 - 1) < src(ptr2 - 1)) {
vec(ptr - 1) = src(ptr2 - 1);
vec[ptr - 1] = n_mulmod_shoup(na, src[ptr2 - 1], na_pr, p.n);
ptr2--;
ptr--;
}
else {
vec(ptr - 1) = vec(ptr1 - 1);
vec[ptr - 1] = vec[ptr1 - 1];
ptr1--;
ptr--;
}
}
while (ptr2 > 0) {
vec(ptr - 1) = src(ptr2 - 1);
vec[ptr - 1] = n_mulmod_shoup(na, src[ptr2 - 1], na_pr, p.n);
ptr2--;
ptr--;
}
// if ptr1 > 0, and ptr > 0
for (size_t i = ptr1; i < ptr; i++) {
vec[i] = 0;
}
vec.canonicalize();
if (vec.alloc() > 4 * vec.nnz())
vec.reserve(2 * vec.nnz());
return 0;
}
template <typename index_t, bool dir>
int sfmpq_vec_addsub_mul(sfmpq_vec<index_t>& vec, const sfmpq_vec<index_t>& src, const rat_t& a) {
if (src.nnz() == 0)
return 0;
if (vec.nnz() == 0) {
vec = src;
sparse_vec_rescale(vec, a, field_t());
return 0;
}
rat_t na, entry;
if constexpr (dir) {
na = a;
}
else {
na = -a;
}
size_t ptr1 = vec.nnz();
size_t ptr2 = src.nnz();
size_t ptr = vec.nnz() + src.nnz();
if (vec._alloc < ptr)
vec.reserve(ptr);
vec.resize(ptr);
while (ptr1 > 0 && ptr2 > 0) {
if (vec(ptr1 - 1) == src(ptr2 - 1)) {
entry = na * src[ptr2 - 1];
entry += vec[ptr1 - 1];
if (entry != 0) {
vec(ptr - 1) = vec(ptr1 - 1);
vec[ptr - 1] = entry;
ptr--;
}
ptr1--;
ptr2--;
}
else if (vec(ptr1 - 1) < src(ptr2 - 1)) {
entry = na * src[ptr2 - 1];
vec(ptr - 1) = src(ptr2 - 1);
vec[ptr - 1] = entry;
ptr2--;
ptr--;
}
else {
vec(ptr - 1) = vec(ptr1 - 1);
vec[ptr - 1] = vec[ptr1 - 1];
ptr1--;
ptr--;
}
}
while (ptr2 > 0) {
entry = na * src[ptr2 - 1];
vec(ptr - 1) = src(ptr2 - 1);
vec[ptr - 1] = entry;
ptr2--;
ptr--;
}
// if ptr1 > 0, and ptr > 0
for (size_t i = ptr1; i < ptr; i++) {
vec[i] = 0;
}
vec.canonicalize();
if (vec._alloc > 4 * vec.nnz())
vec.reserve(2 * vec.nnz());
return 0;
}
// it is just symmetric difference for bool vectors
template <typename index_t>
int sparse_vec_add(sparse_vec<bool, index_t>& vec, sparse_vec<bool, index_t>& src, const field_t& F) {
if (src.nnz() == 0)
return 0;
if (vec.nnz() == 0) {
vec = src;
return 0;
}
auto a = vec.indices;
auto a_end = vec.indices + vec.nnz();
auto b = src.indices;
auto b_end = src.indices + src.nnz();
index_t* out = s_malloc<index_t>(vec.nnz() + src.nnz());
auto out_start = out;
auto out_alloc = vec.nnz() + src.nnz();
while (a != a_end && b != b_end) {
if (*a < *b)
*out++ = *a++;
else if (*b < *a)
*out++ = *b++;
else {
++a; ++b;
}
}
// remaining
while (a != a_end) *out++ = *a++;
while (b != b_end) *out++ = *b++;
size_t new_nnz = out - out_start;
s_free(vec.indices);
vec.indices = out_start;
vec._nnz = new_nnz;
vec._alloc = out_alloc;
return 0;
}
template <typename index_t>
inline int sparse_vec_add(snmod_vec<index_t>& vec, const snmod_vec<index_t>& src, const field_t& F) {
return snmod_vec_add_mul(vec, src, 1, F);
}
template <typename index_t>
inline int sparse_vec_sub(snmod_vec<index_t>& vec, const snmod_vec<index_t>& src, const field_t& F) {
return snmod_vec_add_mul(vec, src, F.mod.n - 1, F);
}
template <typename index_t>
inline int sparse_vec_add_mul(snmod_vec<index_t>& vec, const snmod_vec<index_t>& src, const ulong a, const field_t& F) {
return snmod_vec_add_mul(vec, src, a, F);
}
template <typename index_t>
inline int sparse_vec_add_mul(sfmpq_vec<index_t>& vec, const sfmpq_vec<index_t>& src, const rat_t& a, const field_t& F) {
return sfmpq_vec_addsub_mul<index_t, true>(vec, src, a);
}
template <typename index_t>
inline int sparse_vec_sub_mul(snmod_vec<index_t>& vec, const snmod_vec<index_t>& src, const ulong a, const field_t& F) {
return snmod_vec_add_mul(vec, src, F.mod.n - a, F);
}
template <typename index_t>
inline int sparse_vec_sub_mul(sfmpq_vec<index_t>& vec, const sfmpq_vec<index_t>& src, const rat_t& a, const field_t& F) {
return sfmpq_vec_addsub_mul<index_t, false>(vec, src, a);
}
// dot product
template <typename index_t, typename T>
T sparse_vec_dot(const sparse_vec<T, index_t>& v1, const sparse_vec<T, index_t>& v2, const field_t& F) {
if (v1.nnz() == 0 || v2.nnz() == 0) {
return T(0);
}
size_t ptr1 = 0, ptr2 = 0;
T result = 0;
auto e1 = v1.indices + v1.nnz();
auto e2 = v2.indices + v2.nnz();
while (ptr1 < v1.nnz() && ptr2 < v2.nnz()) {
if (v1(ptr1) == v2(ptr2)) {
result = scalar_add(result, scalar_mul(v1[ptr1], v2[ptr2], F), F);
ptr1++;
ptr2++;
}
else if (v1(ptr1) < v2(ptr2)) {
ptr1 = std::lower_bound(v1.indices + ptr1, e1, v2(ptr2)) - v1.indices;
}
else {
ptr2 = std::lower_bound(v2.indices + ptr2, e2, v1(ptr1)) - v2.indices;
}
}
return result;
}
template <typename index_t>
bool sparse_vec_dot(const sparse_vec<bool, index_t>& v1, const sparse_vec<bool, index_t>& v2, const field_t& F) {
if (v1.nnz() == 0 || v2.nnz() == 0) {
return false;
}
size_t ptr1 = 0, ptr2 = 0;
bool result = false;
auto e1 = v1.indices + v1.nnz();
auto e2 = v2.indices + v2.nnz();
while (ptr1 < v1.nnz() && ptr2 < v2.nnz()) {
if (v1(ptr1) == v2(ptr2)) {
// add one in GF(2), 0 to 1 and 1 to 0, so it is !result
result = !result;
ptr1++;
ptr2++;
}
else if (v1(ptr1) < v2(ptr2)) {
ptr1 = std::lower_bound(v1.indices + ptr1, e1, v2(ptr2)) - v1.indices;
}
else {
ptr2 = std::lower_bound(v2.indices + ptr2, e2, v1(ptr1)) - v2.indices;
}
}
return result;
}
// we do not check the boundry of v2, so it is not safe to use this function,
// be careful
template <typename index_t, typename T>
T sparse_vec_dot_dense_vec(const sparse_vec<T, index_t>& v1, const T* v2, const field_t& F) {
if (v1.nnz() == 0) {
return T(0);
}
T result = 0;
for (size_t i = 0; i < v1.nnz(); i++) {
result = scalar_add(result, scalar_mul(v1[i], v2[v1(i)], F), F);
}
return result;
}
template <typename index_t>
bool sparse_vec_dot_dense_vec(const sparse_vec<bool, index_t>& v1, const bool* v2, const field_t& F) {
if (v1.nnz() == 0)
return bool(false);
bool result = false;
for (size_t i = 0; i < v1.nnz(); i++)
result ^= v1[i] && v2[v1(i)];
return result;
}
// the following two functions are used in sparse_mat_rref_reconstruct
template <typename index_t>
int_t sparse_vec_denominator_lcm(const sparse_vec<rat_t, index_t>& vec) {
int_t d = 1;
for (size_t i = 0; i < vec.nnz(); i++) {
d = Flint::LCM(d, vec[i].den());
}
return d;
}
template <typename index_t>
int_t sparse_vec_height(const sparse_vec<rat_t, index_t>& vec) {
if (vec.nnz() == 0)
return 1;
int_t d = sparse_vec_denominator_lcm(vec);
int_t h = (vec[0] * d).height();
for (size_t i = 1; i < vec.nnz(); i++) {
int_t hi = (vec[i] * d).height();
if (hi > h)
h = hi;
}
return h;
}
// debug only, not used to the large vector
template <typename index_t, typename T> void print_vec_info(const sparse_vec<T, index_t>& vec) {
std::cout << "-------------------" << std::endl;
std::cout << "nnz: " << vec.nnz() << std::endl;
std::cout << "indices: ";
for (size_t i = 0; i < vec.nnz(); i++)
std::cout << vec(i) << " ";
std::cout << "\nentries: ";
for (size_t i = 0; i < vec.nnz(); i++)
std::cout << scalar_to_str(vec[i]) << " ";
std::cout << std::endl;
}
} // namespace SparseRREF
#endif