-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
104 lines (83 loc) · 2.89 KB
/
utils.h
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
#ifndef UTILS_H
#define UTILS_H
#include "types.h"
void load_table(matrix_d &tab,
const char *filepath, int nrow, int ncol);
void load_table(matrix_z &tab,
const char *filepath, int nrow, int ncol);
void write_table(const matrix_z &tab, const char *filepath);
void write_table(const matrix_d &tab, const char *filepath);
void matrix_rand_2exp(matrix_z &mat, int l);
void mod_2exp(mpz_class &x, int n);
void mod_2exp(matrix_z &mat, int n);
void mod_double(matrix_d &mat, int n);
typedef enum{flat_column, flat_row} flatten_order_t;
template <typename MatrixT>
inline void matrix_flatten(const MatrixT &in, MatrixT &out, flatten_order_t order)
{
assert(in.size() <= out.size());
if (order == flat_column) {
for (int i = 0, ncols = in.cols(); i < ncols; ++i)
for (int j = 0, nrows = in.rows(); j < nrows; ++j)
out(i*nrows + j) = in(j, i);
}
else if (order == flat_row) {
for (int i = 0, nrows = in.rows(); i < nrows; ++i)
for (int j = 0, ncols = in.cols(); j < ncols; ++j)
out(i*ncols + j) = in(i, j);
}
else {
assert(false);
}
}
template<typename MatrixT>
inline void matrix_deflatten(const MatrixT & in, MatrixT & out, flatten_order_t order)
{
assert(in.size() == out.size());
if (order == flat_column) {
for (int i = 0, ncols = out.cols(); i < ncols; ++i)
for (int j = 0, nrows = out.rows(); j < nrows; ++j)
out(j, i) = in(i*nrows + j);
}
else if (order == flat_row) {
for (int i = 0, nrows = out.rows(); i < nrows; ++i)
for (int j = 0, ncols = out.cols(); j < ncols; ++j)
out(i, j) = in(i*ncols + j);
}
else {
assert(false);
}
}
inline matrix_d& matrix_z2d(const matrix_z &in, matrix_d &out)
{
assert(in.size() == out.size());
const mpz_class *z_data = in.data();
double *d_data = out.data();
for (int i = 0, size = in.size(); i < size; ++i)
*(d_data + i) = (z_data + i)->get_d();
return out;
}
//#include <fstream>
extern std::ofstream den_log;
extern mpz_class MOD_RING, MOD_HALFRING;
//#include <iostream>
inline void ring_neg_recover(mpz_class &v)
{
if (v >= MOD_HALFRING) {
//std::cout << "neg recover before " << v << std::endl;
v -= MOD_RING;
//std::cout << "neg recover after " << v << std::endl;
}
}
inline void matrix_neg_recover (matrix_z& mat)
{
mpz_class *data = mat.data();
for (int i = 0, size = mat.size(); i < size; ++i)
//if (*(data + i) >= MOD_HALFRING) {
// *(data + i) -= MOD_RING;
// //std::cout << "neg value found: " << *data << std::endl;
// //den_log << "neg value found: " << *(data + i) << std::endl;
//}
ring_neg_recover(*(data + i));
}
#endif