-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvec_int64.h
160 lines (127 loc) · 3.52 KB
/
vec_int64.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
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
// Copyright 2013-2017 Edgar Costa
// See LICENSE file for license details.
#ifndef VEC_INT64_H_
#define VEC_INT64_H_
#include <NTL/vector.h>
#include <NTL/matrix.h>
#include <cassert>
#include <cstdint>
// extending NTL::Vec<int64_t>
bool operator<(const NTL::Vec<int64_t>& a, const NTL::Vec<int64_t>& b);
typedef struct vec_int64_t_less {
bool
operator()(const NTL::Vec<int64_t>& a, const NTL::Vec<int64_t>& b)
const {
return a < b;
}
} vi64less;
inline bool
operator<(const NTL::Vec<int64_t>& a, const NTL::Vec<int64_t>& b) {
if ( a.length() != b.length())
NTL::Error("operator<: dimension mismatch");
for (int64_t i = 0; i < a.length() - 1; ++i) {
if ( a[i] > b[i] )
return false;
else if ( a[i] < b[i] )
return true;
}
return a[a.length() - 1] < b[b.length() - 1];
}
// res = c * v;
void inline
mul(NTL::Vec<int64_t> &res,
const int64_t c, const NTL::Vec<int64_t> &v) {
res.SetLength(v.length());
for (int64_t i = 0; i < v.length(); ++i)
res[i] = c * v[i];
}
// res = v + w
void inline
add(NTL::Vec<int64_t> &res,
const NTL::Vec<int64_t> &v, const NTL::Vec<int64_t> &w) {
assert(v.length() == w.length());
res.SetLength(v.length());
for (int64_t i = 0; i < v.length(); ++i)
res[i] = v[i] + w[i];
}
// res = v - w
void inline
sub(NTL::Vec<int64_t> &res,
const NTL::Vec<int64_t> &v, const NTL::Vec<int64_t> &w) {
assert(v.length() == w.length());
res.SetLength(v.length());
for (int64_t i = 0; i < v.length(); ++i)
res[i] = v[i] - w[i];
}
// res = v*w
void inline
mul(int64_t &res, const NTL::Vec<int64_t> &v, const NTL::Vec<int64_t> &w) {
assert(v.length() == w.length());
res = 0;
for (int64_t i = 0; i < v.length(); ++i)
res += v[i]*w[i];
}
// res = A*v
void inline
mul(NTL::Vec<int64_t> &res,
const NTL::Mat<int64_t> &A, const NTL::Vec<int64_t> &v) {
assert(A.NumCols() == v.length());
res.SetLength(A.NumRows());
for (int64_t i =0; i < A.NumRows(); ++i)
mul(res[i], A[i], v);
}
/*
* Operators
*/
inline NTL::Vec<int64_t>
operator+(const NTL::Vec<int64_t>& a, const NTL::Vec<int64_t>& b) {
NTL::Vec<int64_t> x;
add(x, a, b);
return x;
}
inline NTL::Vec<int64_t>
operator-(const NTL::Vec<int64_t>& a, const NTL::Vec<int64_t>& b) {
NTL::Vec<int64_t> x;
sub(x, a, b);
return x;
}
inline NTL::Vec<int64_t>
operator*(const int64_t c, const NTL::Vec<int64_t>& v) {
NTL::Vec<int64_t> x;
mul(x, c , v);
return x;
}
inline int64_t
operator*(const NTL::Vec<int64_t> &v, const NTL::Vec<int64_t> &w) {
int64_t x;
mul(x, v , w);
return x;
}
inline NTL::Vec<int64_t>
operator*(const NTL::Mat<int64_t> &A, const NTL::Vec<int64_t> &w) {
NTL::Vec<int64_t> x;
mul(x, A , w);
return x;
}
// converts NTL::Vec to array
inline void
conv(int64_t* result, const NTL::Vec<int64_t> &v) {
for (int64_t i = 0; i < v.length(); ++i)
result[i] = v[i];
}
// converts an array to NTL::Vec<int64_t>
inline void
conv(NTL::Vec<int64_t> &result, const int64_t array[], const int64_t size) {
result.SetLength(size);
for (int64_t i = 0; i < size ; ++i)
result[i] = array[i];
}
/*
* returns v - e_i
*/
NTL::Vec<int64_t> diff(const NTL::Vec<int64_t> v, const int64_t i);
// reduces an entry by 1
NTL::Vec<int64_t> tweak_step(const NTL::Vec<int64_t> v);
// applies tweak r times
NTL::Vec<int64_t> tweak(const NTL::Vec<int64_t> v, const int64_t r);
#endif // VEC_INT64_H_