-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynom.cpp
153 lines (134 loc) · 3.71 KB
/
Polynom.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
#include "Polynom.hpp"
template <uint8_t P>
[[nodiscard]]
std::vector<GF<P>>::size_type Polynom<P>::Deg(void) const noexcept {
return coef.size() == 0 ? 0 : coef.size() - 1;
}
template <uint8_t P>
void Polynom<P>::fixDeg(void) noexcept{
for (auto i = Deg(); i > 0; --i) {
if (coef[i] != 0) { break; }
coef.erase(coef.begin() + i);
}
if (coef.size() == 1 && coef[0] == 0) {
coef.erase(coef.begin());
}
}
template <uint8_t P>
Polynom<P>::Polynom(vector<GF<P>> coef) noexcept : coef(coef) {
fixDeg();
}
template <uint8_t P>
Polynom<P>::Polynom(const std::vector<GF<P>>::size_type n) noexcept :{
coef = std::vector<GF<P>>(n + 1, GF<P>(0));
coef[n] = 1;
}
template <uint8_t P>
Polynom<P> &Polynom<P>::operator+=(const Polynom<P> &p2) noexcept {
std::vector<GF<P>>::size_type size = coef.size() >= p2.coef.size() ? coef.size() : p2.coef.size();
coef.resize(size);
for (auto i = p2.coef.size() - 1; i > 0; --i) {
coef[i] += p2.coef[i];
}
if (size > 0) {
coef[0] += p2.coef[0];
}
fixdeg();
return *this;
}
template <uint8_t P>
Polynom<P> Polynom<P>::operator+(const Polynom<P> &p2) noexcept {
return Polynom<P>(coef) += p2;
}
template <uint8_t P>
Polynom<P>& Polynom<P>::operator-=(const Polynom<P> &p2) noexcept {
std::vector<GF<P>>::size_type size = coef.size() >= p2.coef.size() ? coef.size() : p2.coef.size();
coef.resize(size);
for (auto i = p2.coef.size() - 1; i > 0; --i) {
coef[i] -= p2.coef[i];
}
if (size > 0) {
coef[0] -= p2.coef[0];
}
fixdeg();
return *this;
}
template <uint8_t P>
Polynom<P> Polynom<P>::operator-(const Polynom<P> &p2) noexcept {
return Polynom<P>(coef) -= p2;
}
template <uint8_t P>
Polynom<P> Polynom<P>::operator*(const Polynom<P> &p2) noexcept {
if (p2.coef.size() == 0 || coef.size() == 0) {
return Polynom<P>({});
}
std::vector<GF<P>>::size_type rsize = Deg() + p2.Deg() + 1;
std::vector<GF<P>> rcoefs(rsize, GF<P>(0));
for (int i = 0; i <= coef.size(); ++i) {
for (int k = 0; k <= p2.coef.size(); ++k) {
rcoefs[i + k] += coefs[i] * p2.coefs[k];
}
}
return Polynom<P>(rcoefs);
}
template <uint8_t P>
Polynom<P>& Polynom<P>::operator*(const GF<P>& a) noexcept {
for (int i = 0; i <= coef.size(); i++) {
coef[i] = a * coef[i];
}
fixDeg();
return this;
}
template <uint8_t P>
Polynom<P>& Polynom<P>::operator/=(const Polynom<P>& p2) {
Polynom<P> rp({ GF(0) });
if (Deg() < p2.Deg()) {
return rp;
}
if (p2.Deg() == 0) {
if (p2.coef.size() == 0) {
throw new std::runtime_error("division by zero");
}
for (auto &c : coef) {
c /= p2[0];
}
}
Polynom rt(coef);
for (auto n = Deg() - p2.Deg() + 1; n > p2.Deg(); --n) {
rp = rp + Polynom<P>(n) * (rt[rt.Deg()] / p2.coef[p2.Deg()]);
rt = rt - ((Polynom<P>(n) * p2) * (rt[rt.Deg()] / p2.coef[p2.Deg()]));
}
return rp;
}
template <uint8_t P>
Polynom<P> Polynom<P>::operator/(const Polynom<P>& p2) {
return Polynom<P>(coef) /= p2;
}
template <uint8_t P>
Polynom<P>& Polynom<P>::operator%=(const Polynom<P>& p2) {
Polynom<P> rp({ GF(0) });
if (Deg() < p2.Deg()) {
return *this;
}
if (p2.Deg() == 0) {
if (p2.coef.size() == 0) {
throw new std::runtime_error("modulus by zero");
}
return Polynom<P>({});
}
/*
auto p2Monic = p2 / Polynom<P>({ p2[p2.Deg()] });
for (auto n = Deg(), m = p2Monic.Deg(); n >= m; n = Deg()) {
*this -= (p2Monic * coef[n]) * Polynom<P>(n - m);
}
*/
Polynom rt(coef);
for (auto n = Deg() - p2.Deg() + 1; n > p2.Deg(); --n) {
rt -= (Polynom<P>(n) * p2) * (rt[rt.Deg()] / p2.coef[p2.Deg()]);
}
return rt;
}
template <uint8_t P>
Polynom<P> Polynom<P>::operator%(const Polynom<P>& p2) {
return Polynom<P>(coef) %= p2;
}