-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly.h
More file actions
384 lines (325 loc) · 11 KB
/
Copy pathpoly.h
File metadata and controls
384 lines (325 loc) · 11 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#ifndef POLY_H_
#define POLY_H_
#include <array>
#include <cstddef>
#include <type_traits>
#include <utility>
template<typename T, size_t N = 0>
class poly;
namespace details {
template<typename T>
struct is_poly : std::false_type {
};
template<typename T, std::size_t N>
struct is_poly<poly<T, N> > : std::true_type {
using type = T;
};
template<typename T>
constexpr bool is_poly_v = is_poly<T>::value;
template<typename T1, typename T2>
constexpr auto shift(const T2 &q) {
if constexpr (is_poly_v<typename is_poly<T1>::type>) {
return shift<typename is_poly<T1>::type>(const_poly(q));
} else {
return const_poly(q);
}
}
template<typename T1, typename T2>
struct common_multiply {
using type = decltype(std::declval<T1>() * std::declval<T2>());
};
template<typename T1, std::size_t N1, typename T2>
struct common_multiply<poly<T1, N1>, T2> {
using type = poly<typename common_multiply<T1, T2>::type, N1>;
};
template<typename T1, typename T2, std::size_t N2>
struct common_multiply<T1, poly<T2, N2>> {
using type = poly<typename common_multiply<T1, T2>::type, N2>;
};
template<typename T1, std::size_t N1, typename T2, std::size_t N2>
struct common_multiply<poly<T1, N1>, poly<T2, N2>> {
static constexpr size_t result_size = (N1 && N2) ? N1 + N2 - 1 : 0;
using type = poly<typename common_multiply<T1, T2>::type, result_size>;
};
template<typename T, typename U>
using common_multiply_t = typename common_multiply<T, U>::type;
}
template<typename... Args>
poly(Args...) -> poly<std::common_type_t<Args...>, sizeof...(Args)>;
template<typename T, size_t N>
class poly {
private:
std::array<T, N> coefficients{};
template<typename U, std::size_t index, typename... Rest>
requires (index < N - 1 && details::is_poly_v<T>)
constexpr auto at_recursive(const U &polynom, [[maybe_unused]] const Rest &... rest) const {
return coefficients[index].at(rest...) + at_recursive<U, index + 1>(polynom, rest...) * polynom;
}
template<typename U, std::size_t index, typename... Rest>
requires (index == N - 1 && details::is_poly_v<T>)
constexpr auto at_recursive([[maybe_unused]] const U &, [[maybe_unused]] const Rest &... rest) const {
return coefficients[index].at(rest...);
}
template<typename U, std::size_t index, typename... Rest>
requires (index == N - 1 && !details::is_poly_v<T>)
constexpr auto at_recursive([[maybe_unused]] const U &value, [[maybe_unused]] const Rest &... rest) const {
return coefficients[index];
}
template<typename U, std::size_t index, typename... Rest>
requires (index < N - 1 && !details::is_poly_v<T>)
constexpr auto at_recursive(const U &polynom, const Rest &... rest) const {
return coefficients[index] + at_recursive<U, index + 1>(polynom, rest...) * polynom;
}
template<typename U, std::size_t K, std::size_t... Is>
constexpr auto at_array(const std::array<U, K> &arr, std::index_sequence<Is...>) const {
return at(arr[Is]...);
}
public:
// Constructors
constexpr poly() {
}
template<typename U, size_t M>
requires ((M <= N) && std::is_convertible_v<U, T>)
constexpr poly(const poly<U, M> &other) {
for (size_t i = 0; i < M; ++i) {
coefficients[i] = other[i];
}
}
template<typename U, size_t M>
requires ((M <= N) && std::is_convertible_v<U, T>)
constexpr poly(poly<U, M> &&other) {
for (size_t i = 0; i < M; ++i) {
coefficients[i] = std::move(other[i]);
}
}
template<typename U>
requires (std::is_convertible_v<U, T>)
constexpr poly(U &&value) {
coefficients[0] = std::forward<U>(value);
}
template<typename... Args>
requires ((2 <= sizeof...(Args)) && (sizeof...(Args) <= N) && (std::conjunction_v<std::is_convertible<Args, T>...>))
constexpr poly(Args &&... args) {
std::size_t iterator = 0;
((coefficients[iterator++] = std::forward<Args>(args)), ...);
}
// Assignment operators
template<typename U, size_t M>
requires ((M <= N) && std::is_convertible_v<U, T>)
constexpr poly &operator=(const poly<U, M> &other) {
for (size_t i = 0; i < M; ++i) {
coefficients[i] = other[i];
}
for (size_t i = M; i < N; ++i) {
coefficients[i] = T{};
}
return *this;
}
template<typename U, size_t M>
requires ((M <= N) && std::is_convertible_v<U, T>)
constexpr poly &operator=(poly<U, M> &&other) {
for (size_t i = 0; i < M; ++i) {
coefficients[i] = std::move(other[i]);
}
for (size_t i = M; i < N; ++i) {
coefficients[i] = T{};
}
return *this;
}
// Compound assignment operators
template<typename U, size_t M>
requires ((M <= N) && std::is_convertible_v<U, T>)
constexpr poly &operator+=(const poly<U, M> &other) {
for (size_t i = 0; i < M; ++i) {
coefficients[i] += other[i];
}
return *this;
}
template<typename U>
requires (std::is_convertible_v<U, T>)
constexpr poly &operator+=(const U &value) {
coefficients[0] += value;
return *this;
}
template<typename U, size_t M>
requires ((M <= N) && std::is_convertible_v<U, T>)
constexpr poly &operator-=(const poly<U, M> &other) {
for (size_t i = 0; i < M; ++i) {
coefficients[i] -= other[i];
}
return *this;
}
template<typename U>
requires (std::is_convertible_v<U, T>)
constexpr poly &operator-=(const U &value) {
coefficients[0] -= value;
return *this;
}
template<typename U>
requires (std::is_convertible_v<U, T>)
constexpr poly &operator*=(const U &value) {
for (auto &coeff: coefficients) {
coeff *= value;
}
return *this;
}
template<typename U>
requires ((1 <= N) && std::is_convertible_v<U, T>)
constexpr poly &operator*=(const poly<U, 1> &other) {
*this = *this * other;
return *this;
}
// Indexing operator
constexpr T &operator[](size_t index) {
return coefficients[index];
}
constexpr const T &operator[](size_t index) const {
return coefficients[index];
}
// At method
template<typename U, typename... Args>
constexpr auto at(const U &value, const Args &... args) const {
if constexpr (N == 0) {
return T{};
} else {
return at_recursive<U, 0, Args...>(value, args...);
}
}
constexpr auto at() const {
return *this;
}
template<typename U, std::size_t K>
constexpr auto at(const std::array<U, K> &arr) const {
if constexpr (K == 0) {
return *this;
} else {
return at_array(arr, std::make_index_sequence<K>{});
}
}
// Size method
constexpr size_t size() const {
return N;
}
};
// Common type
namespace std {
template<typename T1, size_t N1, typename T2>
struct common_type<T1, poly<T2, N1> > {
using type = poly<std::common_type_t<T1, T2>, N1>;
};
template<typename T1, typename T2, size_t N2>
struct common_type<poly<T1, N2>, T2> {
using type = poly<std::common_type_t<T1, T2>, N2>;
};
template<typename T1, size_t N1, typename T2, size_t N2>
struct common_type<poly<T1, N1>, poly<T2, N2> > {
using type = poly<std::common_type_t<T1, T2>, (N1 > N2 ? N1 : N2)>;
};
}
template<typename T, size_t N>
constexpr auto const_poly(const poly<T, N> &p) {
poly<poly<T, N>, 1> result;
result[0] = p;
return result;
}
// Cross function
template<typename T1, typename T2>
requires (details::is_poly_v<T1> && details::is_poly_v<T2>)
constexpr auto cross(const T1 &p, const T2 &q) {
return p * details::shift<T1>(q);
}
// Binary operators
template<typename T, size_t N, typename U>
constexpr auto operator+(const poly<T, N> &lhs, const U &rhs) {
using ResultType = std::common_type_t<T, U>;
poly<ResultType, N> result = lhs;
result += rhs;
return result;
}
template<typename T, typename U, size_t M>
constexpr auto operator+(const T &lhs, const poly<U, M> &rhs) {
using ResultType = std::common_type_t<T, U>;
poly<ResultType, M> result = rhs;
result += lhs;
return result;
}
template<typename T, size_t N, typename U, size_t M>
constexpr auto operator+(const poly<T, N> &lhs, const poly<U, M> &rhs) {
using ResultType = std::common_type_t<T, U>;
constexpr size_t ResultSize = (N > M) ? N : M;
poly<ResultType, ResultSize> result;
for (size_t i = 0; i < N; ++i) {
result[i] += lhs[i];
}
for (size_t i = 0; i < M; ++i) {
result[i] += rhs[i];
}
return result;
}
template<typename T, size_t N, typename U>
constexpr auto operator-(const poly<T, N> &lhs, const U &rhs) {
using ResultType = std::common_type_t<T, U>;
poly<ResultType, N> result = lhs;
result -= rhs;
return result;
}
template<typename T, typename U, size_t M>
constexpr auto operator-(const T &lhs, const poly<U, M> &rhs) {
using ResultType = std::common_type_t<T, U>;
poly<ResultType, M> result = -rhs;
result += lhs;
return result;
}
template<typename T, size_t N, typename U, size_t M>
constexpr auto operator-(const poly<T, N> &lhs, const poly<U, M> &rhs) {
using ResultType = std::common_type_t<T, U>;
constexpr size_t ResultSize = (N > M) ? N : M;
poly<ResultType, ResultSize> result;
for (size_t i = 0; i < N; ++i) {
result[i] += lhs[i];
}
for (size_t i = 0; i < M; ++i) {
result[i] -= rhs[i];
}
return result;
}
template<typename T, size_t N, typename U>
constexpr auto operator*(const poly<T, N> &lhs, const U &rhs) {
using ResultType = details::common_multiply_t<T, U>;
poly<ResultType, N> result = lhs;
result *= rhs;
return result;
}
template<typename T, typename U, size_t M>
constexpr auto operator*(const T &lhs, const poly<U, M> &rhs) {
using ResultType = details::common_multiply_t<T, U>;
poly<ResultType, M> result = rhs;
result *= lhs;
return result;
}
template<typename T, size_t N, typename U, size_t M>
constexpr auto operator*(const poly<T, N> &lhs, const poly<U, M> &rhs) {
using ResultType = details::common_multiply_t<T, U>;
if constexpr (N == 0 || M == 0) {
return poly<ResultType, 0>{};
} else {
constexpr size_t ResultSize = N + M - 1;
poly<ResultType, ResultSize> result;
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < M; ++j) {
result[i + j] += lhs[i] * rhs[j];
}
}
return result;
}
}
// Unary operator
template<typename T, size_t N>
constexpr poly<T, N> operator-(const poly<T, N> &p) {
poly<T, N> result;
for (size_t i = 0; i < N; ++i) {
result[i] = -p[i];
}
return result;
}
#endif // POLY_H_