-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyold.h
More file actions
302 lines (255 loc) · 8.93 KB
/
Copy pathpolyold.h
File metadata and controls
302 lines (255 loc) · 8.93 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
#ifndef POLY_H_
#define POLY_H_
#include <cstddef>
#include <array>
#include <type_traits>
#include <utility>
#include <tuple>
template<typename T, std::size_t N = 0>
class poly;
template<typename T>
poly(T) -> poly<T, 1>;
template<typename T, typename... Args>
poly(T, Args...) -> poly<T, sizeof...(Args) + 1>;
template<typename T, std::size_t N>
class poly {
public:
// Constructors
constexpr poly() : coefficients{} {}
template<typename U, std::size_t M, typename = std::enable_if_t<(M <= N) && std::is_convertible_v<U, T>>>
constexpr poly(const poly<U, M>& other) {
for (std::size_t i = 0; i < M; ++i) {
coefficients[i] = other[i];
}
}
template<typename U, std::size_t M, typename = std::enable_if_t<(M <= N) && std::is_convertible_v<U, T>>>
constexpr poly(poly<U, M>&& other) {
for (std::size_t i = 0; i < M; ++i) {
coefficients[i] = std::move(other[i]);
}
}
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
constexpr poly(U&& value) {
coefficients[0] = std::forward<U>(value);
}
template<typename... Args, typename = std::enable_if_t<(sizeof...(Args) <= N) && (std::conjunction_v<std::is_convertible<Args, T>...>)>>
constexpr poly(Args&&... args) : coefficients{std::forward<Args>(args)...} {}
// Assignment operators
template<typename U, std::size_t M, typename = std::enable_if_t<(M <= N) && std::is_convertible_v<U, T>>>
constexpr poly& operator=(const poly<U, M>& other) {
if (this != &other) {
for (std::size_t i = 0; i < M; ++i) {
coefficients[i] = other[i];
}
}
return *this;
}
template<typename U, std::size_t M, typename = std::enable_if_t<(M <= N) && std::is_convertible_v<U, T>>>
constexpr poly& operator=(poly<U, M>&& other) {
if (this != &other) {
for (std::size_t i = 0; i < M; ++i) {
coefficients[i] = std::move(other[i]);
}
}
return *this;
}
// Compound assignment operators
template<typename U, std::size_t M, typename = std::enable_if_t<(M <= N) && std::is_convertible_v<U, T>>>
constexpr poly& operator+=(const poly<U, M>& other) {
for (std::size_t i = 0; i < M; ++i) {
coefficients[i] += other[i];
}
return *this;
}
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
constexpr poly& operator+=(const U& value) {
coefficients[0] += value;
return *this;
}
template<typename U, std::size_t M, typename = std::enable_if_t<(M <= N) && std::is_convertible_v<U, T>>>
constexpr poly& operator-=(const poly<U, M>& other) {
for (std::size_t i = 0; i < M; ++i) {
coefficients[i] -= other[i];
}
return *this;
}
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
constexpr poly& operator-=(const U& value) {
coefficients[0] -= value;
return *this;
}
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
constexpr poly& operator*=(const U& value) {
for (auto& coeff : coefficients) {
coeff *= value;
}
return *this;
}
template<typename U, std::size_t M, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
constexpr poly& operator*=(const poly<U, M>& other) {
*this = *this * other;
return *this;
}
// Indexing operator
constexpr T& operator[](std::size_t index) {
return coefficients[index];
}
constexpr const T& operator[](std::size_t index) const {
return coefficients[index];
}
// Metoda at.
template<typename... Args>
constexpr auto at(Args &&... args) const {
if constexpr (sizeof...(Args) == 0) {
return *this;
} else {
auto result = T(); //TODO dlaczego dajemy wszystkie argumenty rekurencyjnie?
for (std::size_t i = 0; i < N; ++i) {
result = result * std::get<0>(std::forward_as_tuple(args...)) + coefficients[i];
}
return result.at(std::forward<Args>(args)...);
}
}
template<typename U, std::size_t K>
constexpr auto at(const std::array<U, K> &arr) const {
if constexpr (K == 0) {
return *this;
} else {
auto result = T();
for (std::size_t i = 0; i < N; ++i) {
result = result * arr[0] + coefficients[i];
}
std::array<U, K - 1> sub_arr;
std::copy(arr.begin() + 1, arr.end(), sub_arr.begin());
return result.at(sub_arr);
}
}
// Size method
constexpr std::size_t size() const {
return N;
}
private:
std::array<T, N> coefficients;
};
// Common type
namespace std {
template<typename T1, std::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, std::size_t N2>
struct common_type<poly<T1, N2>, T2> {
using type = poly<std::common_type_t<T1, T2>, N2>;
};
template<typename T1, std::size_t N1, typename T2, std::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)>;
};
}
// Funkcja cross
template<typename T1, std::size_t N1, typename T2, std::size_t N2>
auto constexpr cross(const poly<T1, N1> &p, const poly<T2, N2> &q) {
using ResultType = std::common_type_t<T1, T2>;
poly<ResultType, N1 + N2> result;
for (std::size_t i = 0; i < N1; ++i) {
for (std::size_t j = 0; j < N2; ++j) {
result[i + j] += p[i] * q[j];
}
}
return result;
}
template<typename T, std::size_t N>
poly<poly<T, N>, 1> constexpr const_poly(const poly<T, N> &p) {
return poly<poly<T, N>, 1>{p};
}
// Binarne (+,-,*)
template<typename T, std::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, std::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, std::size_t N, typename U, std::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 std::size_t ResultSize = (N > M) ? N : M;
poly<ResultType, ResultSize> result;
for (std::size_t i = 0; i < N; ++i) {
result[i] += lhs[i];
}
for (std::size_t i = 0; i < M; ++i) {
result[i] += rhs[i];
}
return result;
}
template<typename T, std::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, std::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, std::size_t N, typename U, std::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 std::size_t ResultSize = (N > M) ? N : M;
poly<ResultType, ResultSize> result;
for (std::size_t i = 0; i < N; ++i) {
result[i] += lhs[i];
}
for (std::size_t i = 0; i < M; ++i) {
result[i] -= rhs[i];
}
return result;
}
template<typename T, std::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, std::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, std::size_t N, typename U, std::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 std::size_t ResultSize = (N + M > 0) ? (N + M - 1) : 0;
poly<ResultType, ResultSize> result;
for (std::size_t i = 0; i < N; ++i) {
for (std::size_t j = 0; j < M; ++j) {
result[i + j] += lhs[i] * rhs[j];
}
}
return result;
}
// Jednoargumentowy (-)
template<typename T, std::size_t N>
constexpr poly<T, N> operator-(const poly<T, N> &p) {
poly<T, N> result;
for (std::size_t i = 0; i < N; ++i) {
result[i] = -p[i];
}
return result;
}
#endif // POLY_H_