-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPoly.cpp
190 lines (169 loc) · 3.89 KB
/
Poly.cpp
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
/*
* Poly.cpp
*
* Created on: Dec 19, 2009
* Author: bhess
*/
#include "Poly.h"
Poly::Poly(int _d) {
coeffs = vector<mpz_class>(_d + 1, 0);
degree = _d;
}
Poly::Poly(mpz_class bin) {
int size = mpz_sizeinbase(bin.get_mpz_t(), 2);
coeffs = vector<mpz_class>(size);
degree = size - 1;
for (int i = 0; i < size; ++i) {
coeffs[i] = (bin >> i) & 1;
}
}
void Poly::set_coeff(int d, mpz_class val) {
coeffs[d] = val;
}
Poly Poly::operator+(const Poly& other) {
Poly res = Poly(max(degree, other.degree));
for (int i = 0; i < degree + 1 || i < other.degree + 1; ++i) {
res.coeffs[i] = (i <= degree ? coeffs[i] : 0) + (i <= other.degree ? other.coeffs[i] : 0);
}
return res;
}
Poly Poly::operator-(const Poly& other) {
Poly res = Poly(max(degree, other.degree));
for (int i = 0; i < degree + 1 || i < other.degree + 1; ++i) {
res.coeffs[i] = (i <= degree ? coeffs[i] : 0) - (i <= other.degree ? other.coeffs[i] : 0);
}
return res;
}
void Poly::operator*=(int s) {
for (int i = 0; i < degree + 1; ++i) {
coeffs[i] = coeffs[i] * s;
}
}
void Poly::operator/=(int s) {
for (int i = 0; i < degree + 1; ++i) {
coeffs[i] = coeffs[i] / s;
}
}
Poly Poly::operator*(const Poly& other) {
Poly res = Poly(degree + other.degree);
for (int i = 0; i < degree + 1; ++i) {
for (int j = 0; j < other.degree + 1; ++j) {
res.coeffs[i+j] += (coeffs[i] * other.coeffs[j]);
}
}
return res;
}
/*
void Poly::operator%=(mpz_class m) {
for (int i = 0; i < degree + 1; ++i) {
mpz_mod(coeffs[i].get_mpz_t(), coeffs[i].get_mpz_t(), m.get_mpz_t());
//int aa = coeffs[i] % m;
//coeffs[i] = (aa < 0 ? m + aa : aa);
}
}
*/
void Poly::operator%=(int logm) {
mpz_class c1 = 1;
mpz_class m = c1 << logm;
for (int i = 0; i < degree + 1; ++i) {
mpz_mod(coeffs[i].get_mpz_t(), coeffs[i].get_mpz_t(), m.get_mpz_t());
}
}
Poly Poly::operator>>(int m) {
Poly res = Poly(degree - m);
for (int i = m; i < degree + 1; ++i) {
res.set_coeff(i - m, coeffs[i]);
}
return res;
}
Poly Poly::operator<<(int m) {
Poly res = Poly(degree + m);
for (int i = 0; i < degree + 1; ++i) {
res.set_coeff(i + m, coeffs[i]);
}
return res;
}
Poly Poly::operator-() {
Poly res(0);
return res - (*this);
}
Poly Poly::PXpPmX() {
Poly res = Poly(degree);
for (int i = 0; i < degree + 1; i += 2) {
res.coeffs[i] = coeffs[i] * 2;
}
for (int i = 1; i < degree + 1; i += 2) {
res.coeffs[i] = 0;
}
return res;
}
bool Poly::operator==(const Poly& other) {
for (int i = 0; i < (int)coeffs.size() && i < (int)other.coeffs.size(); ++i) {
if (coeffs[i] != other.coeffs[i]) {
return false;
}
}
if (coeffs.size() > other.coeffs.size()) {
for (int i = (int)other.coeffs.size(); i < (int)coeffs.size(); ++i) {
if (coeffs[i] != 0) {
return false;
}
}
} else if (other.coeffs.size() > coeffs.size()) {
for (int i = (int)coeffs.size(); i < (int)other.coeffs.size(); ++i) {
if (other.coeffs[i] != 0) {
return false;
}
}
}
return true;
}
Poly Poly::PXmPmX() {
Poly res = Poly(degree);
res.coeffs[0] = coeffs[0] * 2;
for (int i = 1; i < degree + 1; i += 2) {
res.coeffs[i] = coeffs[i] * 2;
}
for (int i = 2; i < degree + 1; i += 2) {
res.coeffs[i] = 0;
}
return res;
}
Poly Poly::sqSubst() {
Poly res = Poly(degree / 2);
res.set_coeff(0, coeffs[0]);
for (int i = 1; 2*i <= degree; ++i) {
res.set_coeff(i, coeffs[2 * i]);
}
return res;
}
Poly Poly::reverse() {
Poly res = Poly(degree);
for (int i = 0; i < degree + 1; ++i) {
res.set_coeff(i, coeffs[degree - i]);
}
return res;
}
Poly Poly::modXpowm(int m) {
Poly res(m - 1);
for (int i = 0; i < res.degree + 1; ++i) {
res.set_coeff(i, coeffs[i]);
}
return res;
}
mpz_class Poly::to_gfe_el() {
mpz_class res = 0;
for (int i = 0; i < degree + 1; ++i) {
if (coeffs[i] % 2 == 1) {
res |= (1 << i);
}
}
return res;
}
void Poly::print() {
//cout << degree << endl;
for (int i = degree; i >= 0; --i) {
cout << coeffs[i] << ",";
}
cout << endl;
}