-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPolynomial.h
305 lines (276 loc) · 6.69 KB
/
SPolynomial.h
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
#ifndef SPOLYNOMIAL_H
#define SPOLYNOMIAL_H
#include<valarray>
#include"SNumeric.h"
#include<algorithm>
//Calcultes the value of a polynomial on some number.
template <class iter, class numArea>
numArea polyCalc(iter first, iter last, numArea x)
{
--last;
numArea result=*last;
while(first!=last){
--last;
result=result*x+*last;
}
return result;
}
//Calulate the tangent of a polynomial,
// writing the coefficients into a container.
template <class inputIter, class OutputIter>
void polyTan(inputIter first, inputIter last, OutputIter res)
{
int i=1;
while(first!=last){
++first;
*res=i*(*first);
++res; ++i;
}
--res;
*res=0;
}
//Calulate the intergration of a polynomial,
// writing the coefficients into a container.
template <class inputIter, class OutputIter>
void polyInt(inputIter first, inputIter last, OutputIter res)
{
int i=1;
*res=0;
++res;
while(first!=last){
*res=(*first)/i;
++res; ++first; ++i;
}
}
/* A function to write the binary representation of a numbeer into a container.
* Programmed by SHEN Weihong.
*/
template <class OutputIter>
void getBin(int n, OutputIter res)
{
OutputIter next=res;
while(n!=0){
*next=n%2;
n/=2;
++next;
}
}
template<class T>
class polynomial{
private:
T* array;
size_t degree;
size_t size;
bool degUpdated;
void reDegree(size_t n){
int m=getDegree();
T* temp=new T[m+1];
std::copy(begin(),end(),temp);
delete [] array;
array=new T[n+1]; // Degree of n, and size of (n+1).
std::copy(temp,temp+m+1,begin());
for(int i=size;i<n+1;++i){
array[i]=0;
}
size=n+1;
degUpdated=false;
delete temp;
}
public:
polynomial():degree(0),size(1),degUpdated(true) {array=new T; *array=0;}
polynomial(T val);
polynomial(size_t n,const T val); // Create a (n-1) degree polynomial, all coefficients of val.
polynomial(const T* p, size_t n); // Create a (n-1) degree polynomial, coefficients from the T pointer starts and on.
polynomial(polynomial& poly); // Copy a polynomial.
~polynomial() {delete [] array;}
T operator[] (size_t n) const;
T& operator[] (size_t n);
polynomial<T>& operator=(polynomial<T>&& poly);
polynomial<T>& operator=(polynomial<T>& poly);
polynomial<T>& operator*=(const T arg) {std::for_each(begin(),end(),[arg] (T& s){s*=arg;});}
polynomial<T>& operator/=(const T arg) {std::for_each(begin(),end(),[arg] (T& s){s/=arg;});}
polynomial<T>& operator+=(const polynomial<T>& poly);
polynomial<T>& operator-=(const polynomial<T>& poly);
polynomial<T>& operator*=(const polynomial<T>& poly);
polynomial& operator/=(const polynomial<T>& poly);
polynomial& operator%=(const polynomial<T>& poly);
size_t getDegree();
size_t getSize() {return size;}
T* begin() {return array;}
T* end() {return array+size;}
T calc(T val) {return polyCalc(begin(),end(),val);}
};
template<class T>
size_t polynomial<T>::getDegree() // Checked
{
if(degUpdated==true){
return degree;
}
else{
T* p=end(); int i=0;
do{
--p;
++i;
}while(*p==(T)0);
i=size-i;
degUpdated=true;
return i;
}
}
template<class T> // Checked
polynomial<T>::polynomial(T val)
{
array=new T;
*array=val;
size=1; degree=0;
degUpdated=(val!=0);
}
template<class T>
polynomial<T>::polynomial(size_t n, const T val):
size(n), degree(n-1), degUpdated(val!=(T)0) // Checked
{
array=new T[n];
for(int i=0;i<n;++i){
array[i]=val;
}
}
template<class T> // Checked
polynomial<T>::polynomial(const T* p, size_t n):
size(n), degree(n-1), degUpdated(false)
{
array=new T[n];
for(int i=0;i<n;++i){
array[i]=p[i];
}
}
template<class T>
polynomial<T>::polynomial(polynomial& poly):
degUpdated(true) // Checked
{
size_t n=poly.getDegree()+1;
array=new T[n];
for(int i=0;i<n;++i){
array[i]=poly[i];
}
size=n;
degree=n-1;
}
template<class T>
T polynomial<T>::operator [](size_t n) const // Checked
{
if(n<size()){
return array[n];
}
else{
reDegree(n);
degUpdated=false;
return 0;
}
}
template<class T>
T& polynomial<T>::operator [](size_t n) // Checked
{
degUpdated=false;
if(n<size){
return array[n];
}
else{
reDegree(n);
return array[n];
}
}
template<class T>
polynomial<T>& polynomial<T>::operator+=(const polynomial& rhs) // Checked
{
int p=degree;
int q=rhs.degree;
if(p>q){
for(int i=0;i<=q;++i){
array[i]+=rhs.array[i];
}
}
if(p<q){
reDegree(q);
for(int i=0;i<=q;++i){
array[i]+=rhs.array[i];
}
}
if(p==q){
for(int i=0;i<=q;++i){
array[i]+=rhs.array[i];
}
degUpdated=false; // It's possible that the degree will decrease.
}
return *this;
}
template<class T>
polynomial<T>& polynomial<T>::operator-=(const polynomial& rhs) // Checked
{
int p=degree;
int q=rhs.degree;
if(p>q){
for(int i=0;i<=q;++i){
array[i]-=rhs.array[i];
}
}
if(p<q){
reDegree(q);
for(int i=0;i<=q;++i){
array[i]-=rhs.array[i];
}
}
if(p==q){
for(int i=0;i<=q;++i){
array[i]-=rhs.array[i];
}
degUpdated=false; // It's possible that the degree will decrease.
}
return *this;
}
template<class T>
polynomial<T>& polynomial<T>::operator=(polynomial<T>&& rhs)
{
int n=rhs.getDegree();
delete [] array;
array=new T[n+1];
std::copy(rhs.begin(),rhs.end(),array);
degree=n;
size=n+1;
degUpdated=true;
return *this;
}
template<class T>
polynomial<T>& polynomial<T>::operator=(polynomial<T>& rhs)
{
int n=rhs.getDegree();
delete [] array;
array=new T[n+1];
std::copy(rhs.begin(),rhs.end(),array);
degree=n;
size=n+1;
degUpdated=true;
return *this;
}
template<class T>
polynomial<T> operator *(polynomial<T>& lhs, polynomial<T>& rhs)
{
int p=lhs.getDegree();
int q=rhs.getDegree();
int n=p+q+1;
polynomial<T> temp(n,1.0);
convolution(lhs.begin(),lhs.begin()+p+1,rhs.begin(),rhs.begin()+q+1,temp.begin());
return temp;
}
template<class T>
polynomial<T> operator+(polynomial<T>& lhs, polynomial<T>& rhs)
{
polynomial temp(lhs);
return (lhs+=rhs);
}
template<class T>
polynomial<T> operator-(polynomial<T>& lhs, polynomial<T>& rhs)
{
polynomial temp(lhs);
return (lhs-=rhs);
}
#endif // SPOLYNOMIAL_H