-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly_and_ratio.cpp
More file actions
220 lines (188 loc) · 4.28 KB
/
poly_and_ratio.cpp
File metadata and controls
220 lines (188 loc) · 4.28 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
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include "modular_arithmetic.hpp"
#include "poly_and_ratio.hpp"
using std::unordered_map;
using std::string;
using std::vector;
inline void split(const string& str, uint_fast16_t* cont, string delims = " ")
{
std::size_t current = str.find(delims), end = str.size();
string s1 = str.substr(0, current), s2 = str.substr(current + 1, end - 1);
cont[0] = stoi(s1);
cont[1] = stoi(s2);
}
bool compare_degrees(const string& a, const string& b)
{
uint_fast16_t cont1[2], cont2[2];
split(a, cont1, ", ");
split(b, cont2, ", ");
return (cont1[0] + cont1[1]) > (cont2[0] + cont2[1]) && (!(cont1[0] + cont1[1]) == (cont2[0] + cont2[1])
|| (cont1[0] > cont2[0] || cont1[1] > cont2[1]));
}
Poly::Poly(unordered_map<string, Galois>& data, int_fast64_t mod)
{
this->mod = mod;
for (std::pair<const string, Galois>& el : data)
{
if (el.second.get_mod() != mod)
{
throw "mods must be equal";
}
}
this->data = data;
}
Poly::Poly(uint_fast32_t mod)
{
this->mod = mod;
data.emplace("0, 0", Galois(0, mod));
}
Poly Poly::operator+ (const Poly& other)
{
Poly res(*this);
Galois koef;
for (const std::pair<const string, Galois>& el : other.data)
{
if (res.data.find(el.first) != res.data.end())
{
koef = res.data[el.first] + el.second;
if (koef != 0)
{
res.data[el.first] = koef;
}
}
else
{
res.data[el.first] = el.second;
}
}
return res;
}
Poly Poly::operator* (const Galois& other)
{
Poly res(*this);
Galois koef;
for (const std::pair<const string, Galois>& el : res.data)
{
koef = res.data[el.first] * other;
if (koef != 0)
{
res.data[el.first] = koef;
}
}
return res;
}
Poly Poly::operator*(const Poly& other)
{
Poly res = Poly(this->mod);
uint_fast16_t sum1, sum2, degrees1[2], degrees2[2];
string key;
Galois koef;
for (const std::pair<const string, Galois>& el1 : other.data)
{
split(el1.first, degrees1, ", ");
for (std::pair<const string, Galois>& el2 : this->data)
{
split(el2.first, degrees2, ", ");
sum1 = degrees1[0] + degrees2[0];
sum2 = degrees1[1] + degrees2[1];
key = std::to_string(sum1) + ", " + std::to_string(sum2);
if (res.data.find(key) != res.data.end())
{
koef = el2.second * el1.second + res.data[key];
if (koef != 0)
{
res.data[key] = el2.second * el1.second;
}
}
else
{
koef = el2.second * el1.second;
if (koef != 0)
{
res.data[key] = koef;
}
}
}
}
return res;
}
Galois Poly::value(Galois& x, Galois& y)
{
Galois res = Galois(0, this->mod), x_pow, y_pow;
uint_fast16_t degrees[2];
for (std::pair<const string, Galois>& el : this->data)
{
split(el.first, degrees, ", ");
res += el.second * x.pow(degrees[0]) * y.pow(degrees[1]);
}
return res;
}
string Poly::get_str()
{
vector<string> keys;
keys.reserve(this->data.size());
for (std::pair<const string, Galois>& el : this->data)
{
keys.push_back(el.first);
}
std::sort(keys.begin(), keys.end(), compare_degrees);
string res = "";
uint_fast16_t degree[2];
for (string& key : keys)
{
split(key, degree, ", ");
res += std::to_string(this->data[key].get_value()) +
"*x^" + std::to_string(degree[0]) +
"*y^" + std::to_string(degree[1]) +
+" + ";
}
return res.substr(0, res.size() - 3);
}
int_fast16_t Poly::get_mod()
{
return this->mod;
}
unordered_map<string, Galois> Poly::get_data()
{
return this->data;
}
Ratio::Ratio(Poly& p1, Poly& p2)
{
if (p1.get_mod() != p2.get_mod())
{
throw "mods of polynomials must be equal";
}
this->numerator = p1;
this->denominator = p2;
}
Ratio::Ratio(uint_fast32_t mod)
{
this->numerator = Poly(mod);
this->denominator = Poly(mod);
}
Ratio Ratio::operator* (const Ratio& other)
{
Poly n = this->numerator * other.numerator, d = this->denominator * other.denominator;
return Ratio(n, d);
}
Ratio Ratio::operator* (const Poly& other)
{
Poly n = this->numerator * other;
return Ratio(n, this->denominator);
}
Ratio Ratio::operator* (const Galois& other)
{
Poly n = this->numerator * other;
return Ratio(n, this->denominator);
}
string Ratio::get_str()
{
return "(" + this->numerator.get_str() + ") / (" + this->denominator.get_str() + ")";
}
Galois Ratio::value(Galois& x, Galois& y)
{
return (this->numerator.value(x, y)) / (this->denominator.value(x, y));
}