-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblatt12.cpp
More file actions
356 lines (289 loc) · 7.84 KB
/
blatt12.cpp
File metadata and controls
356 lines (289 loc) · 7.84 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <iostream>
#include <iomanip>
#include <list>
#include <vector>
//#include <stack>
#include <sstream>
#include <initializer_list>
using namespace std;
template <unsigned p>
class Z
{
private:
unsigned x;
constexpr
Z(unsigned x, bool) : x(x) { }
Z inv()
{
Z one = Z(1, true);
for (unsigned i = 1; i < p; ++i)
{
Z zi = Z(i, true);
if (zi * x == one) return zi;
}
return 0;
}
public:
constexpr
Z(unsigned x = 0u) : x(x % p) { }
constexpr
Z(signed x) : x( x < 0 ? (p-x) % p : x % p ) { }
friend constexpr
bool operator==(Z z1, Z z2) { return z1.x == z2.x; }
friend constexpr
bool operator!=(Z z1, Z z2) { return z1.x != z2.x; }
constexpr
Z operator+() { return Z(x, true); }
constexpr
Z operator-() { return Z(p-x, true); }
friend constexpr
Z operator+(Z z1, Z z2) { return Z(z1.x + z2.x); }
friend constexpr
Z operator-(Z z1, Z z2) { return Z(z1.x - z2.x); }
friend constexpr
Z operator*(Z z1, Z z2) { return Z(z1.x * z2.x); }
friend // constexpr
Z operator/(Z z1, Z z2) { return Z(z1.x * z2.inv().x); }
//constexpr
Z & operator+=(Z const & z) { return *this = *this + z; }
//constexpr
Z & operator-=(Z const & z) { return *this = *this - z; }
//constexpr
Z & operator*=(Z const & z) { return *this = *this * z; }
//constexpr
Z & operator/=(Z const & z) { return *this = *this / z; }
friend
ostream & operator<<(ostream & os, Z z) { return os << z.x << '[' << p << ']'; }
};
template <typename T>
class Monom
{
public:
T coeff;
unsigned deg;
Monom (T coeff = 0, unsigned deg = 0) : coeff(coeff), deg(deg) { }
Monom operator-() const { return Monom(-coeff, deg); }
friend
unsigned grad(Monom const & m) { return m.deg; }
friend
bool operator< (Monom m1, Monom m2) { return m1.deg < m2.deg ? true : m1.coeff < m2.coeff; }
friend
bool operator==(Monom m1, Monom m2) { return m1.deg == m2.deg && m1.coeff == m2.coeff; }
friend
Monom operator*(T const & t, Monom const & m) { return Monom(t * m.coeff, m.deg); }
friend
Monom operator*(Monom const & m, T const & t) { return Monom(t * m.coeff, m.deg); }
friend
Monom operator*(Monom const & m1, Monom const & m2)
{ return Monom(m1.coeff * m2.coeff, m1.deg + m2.deg); }
Monom & operator*=(Monom const & m)
{
coeff *= m.coeff;
deg += m.deg;
return *this;
}
template <typename S, S zero = S(), S one = S(1)> inline
S operator()(S const & value)
{
if (deg == 0) return one;
if (value == zero) return zero;
unsigned hiBit = ~((~0u) >> 1u);
while ((hiBit & deg) == 0) hiBit >>= 1u;
// hiBit maskiert erstes gesetztes Bit von deg (ist != 0, da deg != 0);
S erg = value;
while (hiBit >>= 1u)
{
erg *= erg;
if (hiBit & deg) erg *= value;
}
return erg;
}
friend ostream & operator<<(ostream & os, Monom const & m)
{
return os << m.coeff << "x^" << m.deg;
}
};
template <typename T>
class Polynom
{
private:
list<Monom<T>> coeffs;
struct is_zero
{
bool operator(Monom<T> const & m) { return m.coeff == T(); }
};
struct is_near_zero
{
T eps;
is_near_zero(T const & eps) : eps(eps) { }
bool operator(Monom<T> const & m) { return -eps < m.coeff && m.coeff < eps; }
};
public:
Polynom() { }
Polynom(T const & t) : coeffs(1, Monom<T>(t, 0)) { }
Polynom(Monom<T> const & monom) : coeffs(1, monom) { }
void eliminateZeros()
{
coeffs.remove_if(is_zero);
}
void eliminateZeros(T eps)
{
coeffs.remove_if(is_near_zero(eps));
}
Polynom operator-() const
{
Polynom ret = *this;
for (auto it = ret.coeffs.begin();
it != ret.coeffs.end();
++it)
{
it->coeff = -(it->coeff);
}
return ret;
}
friend Polynom operator+(Polynom const & pol1, Polynom const & pol2)
{
Polynom ret = pol1;
auto it2 = pol2.coeffs.begin();
for (auto it1 = ret.coeffs.begin();
it1 != ret.coeffs.end() && it2 != pol2.coeffs.end();
++it1)
{
if (it1->deg > it2->deg)
{
auto it2range = it2;
while (++it2range != pol2.coeffs.end() && it1->deg > it2range->deg);
ret.coeffs.insert(it1, it2, it2range);
it2 = it2range;
}
else if (it1->deg == it2->deg)
{
it1->coeff += it2->coeff;
++it2;
}
}
// now it1 == ret.coeffs.end() or it2 == pol2.coeffs.end()
// second one doesn't actually matter at all:
// assuming second implies the following command to do nothing,
// so we only care for first one. Then it1 == ret.coeffs.end()
ret.coeffs.insert(ret.coeffs.end() , it2, pol2.coeffs.end());
return ret;
}
Polynom & operator+=(Polynom const & pol) { return *this = *this + pol; }
friend
Polynom operator-(Polynom const & pol1, Polynom const & pol2) { return pol1 + (-pol2); }
Polynom & operator-=(Polynom const & pol) { return *this = *this - pol; }
Polynom & operator*=(Monom<T> const & mon)
{
for (auto it = coeffs.begin();
it != coeffs.end();
++it)
{
it->coeff *= mon.coeff;
it->deg += mon.deg;
}
return *this;
}
Polynom operator*(Monom<T> const & mon) const
{
Polynom ret = *this;
ret *= mon;
return ret;
}
Polynom & operator*=(Polynom const & pol)
{
cout << "Mult Polynom " << *this << " with Polynom " << pol << endl;
Polynom summe;
for (auto it = pol.coeffs.begin();
it != pol.coeffs.end();
++it)
{
summe += *this * *it;
}
return *this = summe;
}
friend
Polynom operator*(Polynom const & pol1, Polynom const & pol2)
{
Polynom ret = pol1;
ret *= pol2;
return ret;
}
friend
Polynom operator+(T const & t, Polynom const & p) { return p + Monom<T>(t); }
friend
Polynom operator+(Polynom const & p, T const & t) { return p + Monom<T>(t); }
friend
Polynom operator*(T const & t, Polynom const & p) { return p * Monom<T>(t); }
friend
Polynom operator*(Polynom const & p, T const & t) { return p * Monom<T>(t); }
friend
ostream & operator<<(ostream & os, Polynom const & pol)
{
auto it = pol.coeffs.rbegin();
switch (it->deg)
{
case 0: os << it->coeff; break;
case 1: os << it->coeff << 'x'; break;
default: os << it->coeff << "x^" << it->deg;
}
while (++it != pol.coeffs.rend())
{
switch (it->deg)
{
case 0: os << " + " << it->coeff; break;
case 1: os << " + " << it->coeff << 'x'; break;
default: os << " + " << it->coeff << "x^" << it->deg;
}
}
return os;
}
};
template <typename T>
Polynom<T> fromCoeffVector(vector<T> const & coeffs)
{
Polynom<T> ret;
unsigned deg = 0;
for (auto it = coeffs.begin(); it != coeffs.end(); ++it)
{
ret += Monom<T>(*it, deg);
++deg;
}
ret.eliminateZeros();
return ret;
}
template <unsigned p>
void increment(vector<Z<p>> & v)
{
for (auto & z : v)
if ((z += 1) != 0)
return;
}
int main()
{
constexpr unsigned p = 2;
constexpr unsigned n = 10;
typedef Z<p> K;
Polynom<K> x = Monom<K>(K(1), 1);
vector<K> coeffs(n);
Polynom<K> pol = fromCoeffVector<K>(coeffs);
cout << pol << endl;
/*
list< Polynom<K> > lst;
// konstante Polynome sind uninteressant.
vector<K> maxcoeffs(n, K(p-1));
vector<K> coeffs(n);
while (coeffs != maxcoeffs)
{
}
// Polynome vom Grad 1 sind immer irreduzibel (über Körpern).
cout << "Irreduzible Polynom in Z/(" << p << "):" << endl;
while (!stc.empty())
{
auto p = move(stc.top());
cout << p << endl;
stc.pop();
}
*/
return 0;
}