-
Notifications
You must be signed in to change notification settings - Fork 1
/
polynomials.hpp
215 lines (181 loc) · 4.25 KB
/
polynomials.hpp
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
#pragma once
#include <array>
#include <cassert>
#include <cstdint>
namespace cu
{
template <typename T, std::size_t N>
class Polynomial
{
public:
Polynomial( T val = 0 )
{
coeffs[0] = val;
}
template <typename U,
std::size_t M,
typename std::enable_if<
std::is_convertible<U,T>::value && M <= N,int>::type = 0>
Polynomial( const Polynomial<U,M> & other )
{
for ( std::size_t m = 0; m <= M; ++m )
coeffs[m] = other[m];
}
T & operator[]( std::size_t n )
{
assert( n <= N );
return coeffs[n];
}
T operator[]( std::size_t n ) const
{
assert( n <= N );
return coeffs[n];
}
T operator()( T x ) const
{
auto val = coeffs[N];
for ( std::size_t i = 1; i <= N; ++i )
{
val *= x;
val += coeffs[N-i];
}
return val;
}
private:
std::array<T,N+1> coeffs{};
};
template <typename T, std::size_t N>
Polynomial<T,N> & operator*=( Polynomial<T,N> & p, T val )
{
for ( std::size_t index = 0; index <= N; ++index )
p[index] *= val;
return p;
}
template <typename T, std::size_t N>
auto operator*( Polynomial<T,N> p, T val )
{
return std::move( p *= val );
}
template <typename T, std::size_t N>
auto operator*( T val, Polynomial<T,N> p )
{
return std::move( p *= val );
}
template <typename T, std::size_t M, std::size_t N>
Polynomial<T,M+N> operator*( const Polynomial<T,M> & p,
const Polynomial<T,N> & q )
{
Polynomial<T,M+N> pq;
for ( std::size_t k = 0; k <= M+N; ++k )
{
const std::size_t m_min = k <= N ? 0 : k-N;
const std::size_t m_max = k <= M ? k : M;
for ( std::size_t m = m_min; m <= m_max; ++m )
pq[k] += p[m]*q[k-m];
}
return pq;
}
template <typename T, std::size_t N>
Polynomial<T,N> & operator/=( Polynomial<T,N> & p, T val )
{
return p *= (1/val);
}
template <typename T, std::size_t N>
auto operator/( Polynomial<T,N> p, T val )
{
return std::move( p /= val );
}
template <typename T, std::size_t M, std::size_t N>
typename std::enable_if<(M>=N),Polynomial<T,M> >::type
operator+( const Polynomial<T,M> & p, const Polynomial<T,N> & q )
{
auto p_copy = p;
for ( std::size_t n = 0; n <= N; ++n )
p_copy[n] += q[n];
return std::move( p_copy );
}
template <typename T, std::size_t M, std::size_t N>
typename std::enable_if<(M<N),Polynomial<T,N> >::type
operator+( const Polynomial<T,M> & p, const Polynomial<T,N> & q )
{
return q + p;
}
template <typename T, std::size_t N>
Polynomial<T,N> operator+( Polynomial<T,N> p )
{
return std::move(p);
}
template <typename T, std::size_t N>
Polynomial<T,N> operator-( Polynomial<T,N> p )
{
for ( std::size_t index = 0; index <= N; ++index )
p[index] = -p[index];
return std::move(p);
}
template <typename T, std::size_t M, std::size_t N>
auto operator-( const Polynomial<T,M> & p, const Polynomial<T,N> & q )
{
return p + (-q);
}
template <typename T, std::size_t N>
Polynomial<T,N> & operator-=( Polynomial<T,N> & p, T val )
{
p[0] -= val;
return p;
}
template <typename T, std::size_t N>
auto operator-( Polynomial<T,N> p, T val )
{
return std::move( p -= val );
}
template <typename T, std::size_t N>
auto operator-( T val, const Polynomial<T,N> & p )
{
return val + (-p);
}
template <typename T, std::size_t N>
Polynomial<T,N> & operator+=( Polynomial<T,N> & p, T val )
{
p[0] += val;
return p;
}
template <typename T, std::size_t N>
auto operator+( Polynomial<T,N> p, T val )
{
return std::move( p += val );
}
template <typename T, std::size_t N>
auto operator+( T val, Polynomial<T,N> p )
{
return std::move( p += val );
}
namespace placeholders
{
template <std::size_t N>
struct XPower
{
template <typename T>
operator Polynomial<T,N>() const
{
Polynomial<T,N> result{};
result[N] = 1;
return result;
}
};
static const constexpr XPower<1> X{};
template <std::size_t M, std::size_t N>
auto operator*( XPower<M>, XPower<N> ) { return XPower<M+N>{}; }
template <std::size_t N, typename T>
auto operator*( XPower<N>, T val )
{
Polynomial<T,N> result{};
result[N] = val;
return result;
}
template <typename T, std::size_t N>
auto operator*( T val, XPower<N> xPower )
{
return xPower * val;
}
} // namespace placeholders
} // namespace cu