-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.cpp
399 lines (333 loc) · 9.55 KB
/
Polynomial.cpp
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
Name: Michael Lausdahl Fuglsang
email: mfugls11
Study No.: 20112699
Code also available at:
https://github.com/Malakahh/Ap-exam-part-2
*/
#include "Polynomial.h"
/*******
******** Private Members
********/
/*
Pimpl idiom used for move semantics.
Solves requirement 7.
*/
template <typename C> struct Polynomial<C>::PolynomialData
{
std::vector<C> coefficients;
/*
Integral cache used for requirement 6.
*/
std::unordered_map<int, C> integralData;
};
/*******
******** Constructors/Destructor
********/
/*
Default constructor
Creates a trivial Polynomial. Solves requirement 1a.
*/
template <typename C> Polynomial<C>::Polynomial(): Polynomial(0,0) {}
//Copy constructor
template <typename C> Polynomial<C>::Polynomial(const Polynomial<C>& p) : pImpl(std::make_unique<Polynomial<C>::PolynomialData>(*p.pImpl)) {}
//Move constructor
template <typename C> Polynomial<C>::Polynomial(Polynomial<C>&& p) : pImpl(std::make_unique<Polynomial<C>::PolynomialData>())
{
std::swap(this->pImpl, p.pImpl);
}
/*
Braced initialization
You use this for creating a polynomial with specific degree coefficients. Solves requirement 1b,
as well as the braced initializer support from requirement 5.
*/
template <typename C> Polynomial<C>::Polynomial(std::initializer_list<C> list) : pImpl(std::make_unique<Polynomial<C>::PolynomialData>())
{
this->SetCoefficientRange<std::initializer_list<C>>(list.begin(), list.end());
}
//Insert data, form: value * x^exponent
template <typename C> Polynomial<C>::Polynomial(const C value, const unsigned int exponent) : pImpl(std::make_unique<Polynomial<C>::PolynomialData>())
{
this->SetCoefficient(value, exponent);
}
template <typename C> Polynomial<C>::~Polynomial() = default;
/*******
******** Public Members
********/
//Sets a coefficient of the form: value * x^exponent
template <typename C> void Polynomial<C>::SetCoefficient(const C value, const unsigned int exponent)
{
//Make sure to clear cache before we alter the polynomial
std::lock_guard<std::mutex> lock(this->integralGuard);
this->pImpl->integralData.clear();
if (exponent < this->pImpl->coefficients.size()) //Alter value currently stored
{
this->pImpl->coefficients[exponent] = value;
}
else //exponent is higher than the currently highest
{
//Set new highest exponent, filling any in between with 0 values
for (auto i = this->pImpl->coefficients.size(); i < exponent + 1; i++)
{
if (i < exponent)
{
this->pImpl->coefficients.push_back(0);
}
else
{
this->pImpl->coefficients.push_back(value);
}
}
}
}
//Gets a coefficient for a specific exponent.
template <typename C> C Polynomial<C>::GetCoefficient(const unsigned int exponent) const
{
//Throw error if requested exponent is higher than what currently exists in this polynomial.
if (exponent >= this->pImpl->coefficients.size())
{
throw std::out_of_range("Index out of bounds");
}
return this->pImpl->coefficients[exponent];
}
//Gets the coefficient for the highest exponent.
template <typename C> C Polynomial<C>::GetHighestCoefficient() const
{
return this->pImpl->coefficients.size() - 1;
}
/*
Scales the polynomial by the given value.
Solves requirement 1c.
*/
template <typename C> void Polynomial<C>::Scale(const C scalar)
{
//Calculate scale for each term
for (auto i = 0; i <= this->GetHighestCoefficient(); i++)
{
this->SetCoefficient(this->GetCoefficient(i) * scalar, i);
}
}
/*
Adds a root to the polynomial (by multiplying with x - root).
Solves requirement 1d.
*/
template <typename C> void Polynomial<C>::AddRoot(const C root)
{
for (auto i = this->GetHighestCoefficient(); i >= 0; i--)
{
//Move value to higher exponent
if (i == this->GetHighestCoefficient())
{
//Higher exponent does not yet exist, so set its value
this->SetCoefficient(this->GetCoefficient(i), i + 1);
}
else
{
//Higher expoent exists, so add to its value
this->SetCoefficient(this->GetCoefficient(i) + this->GetCoefficient(i + 1), i + 1);
}
//Calculate current exponent value
this->SetCoefficient(this->GetCoefficient(i) * root * -1, i);
}
}
/*
Valuates the polynomial at a given point.
Solves requirement 1f.
*/
template <typename C> C Polynomial<C>::ValueAt(const C x) const
{
C res = 0;
//Calculate the value for the given x
for (auto i = 0; i <= this->GetHighestCoefficient(); i++)
{
res += this->GetCoefficient(i) * std::pow(x, i);
}
return res;
}
/*
Computes a polynomial which is a derivative of this polynomial.
Solves requirement 1g.
*/
template <typename C> Polynomial<C> Polynomial<C>::CalculateDerivative() const
{
//Prepare derivative polynomial by copying current one
Polynomial p(*this);
//Calculate derivative polynomial
for (auto i = 0; i < p.GetHighestCoefficient(); i++)
{
p.SetCoefficient(p.GetCoefficient(i + 1) * (i + 1), i);
}
//Erase highest exponent, as it is now invalidated
p.pImpl->coefficients.erase(p.pImpl->coefficients.end() - 1);
return p;
}
/*
Integral tag dispatch, used for requirement 8.
*/
template <typename C> C Polynomial<C>::CalculateIntegralDispatch(const C a, const C b, std::true_type) const
{
/*
Fail assertion for int types on integral data.
Used for requirement 8.
*/
std::cout << "Error: Integrals for integer types are not supported" << std::endl;
assert(false);
}
/*
Integral tag dispatch, used for requirement 8.
*/
template <typename C> C Polynomial<C>::CalculateIntegralDispatch(const C a, const C b, std::false_type) const
{
auto p = this;
/*
Lambda expression used to calculate one part of an integral.
Solves requirement 9.
In addition, I'm using auto here to deduce types.
This solves requirement 3.
Furthermore, it caches its results to minimize calculations. This is thread-safe.
Solves requirement 6.
*/
auto IntegralPart = [p](const auto n){
auto res = 0.;
if (p->pImpl->integralData.count(n) > 0) //key exists, set only once
{
std::cout << "Retrieving: " << n << " from integral cache..." << std::endl;
res = p->pImpl->integralData.find(n)->second;
}
else //key doesn't exist, Calculate...
{
//Lock cache, as we want to alter it
std::lock_guard<std::mutex> lock(p->integralGuard);
//Calculate integral
for (auto i = 0; i <= p->GetHighestCoefficient(); i++)
{
res += p->GetCoefficient(i) * std::pow(n, i + 1) / (i + 1);
}
//Store in cache
p->pImpl->integralData.insert({ n, res });
}
return res;
};
/*
Run the integral part lambda concurrently.
Solves requirement 10.
*/
auto partB = std::async(IntegralPart, b);
auto partA = std::async(IntegralPart, a);
//Get results from tasks
return partB.get() - partA.get();
}
/*
Computes an integral for the given interval bounds.
Solves requirement 1h.
*/
template <typename C> C Polynomial<C>::CalculateIntegral(const C a, const C b) const
{
/*
Integral tag dispatch using traits.
Solves requirement 8.
*/
typename std::is_integral<C>::type isIntergral;
return this->CalculateIntegralDispatch(a, b, isIntergral);
}
/*******
******** Operator overloads
********/
//Calculates the sum of this and a given polynomial
template <typename C> Polynomial<C>& Polynomial<C>::operator+=(const Polynomial<C>& rhs)
{
for (auto i = 0; i <= rhs.GetHighestCoefficient(); i++)
{
if (i <= this->GetHighestCoefficient())
{
this->SetCoefficient(rhs.GetCoefficient(i) + this->GetCoefficient(i), i);
}
else
{
this->SetCoefficient(rhs.GetCoefficient(i), i);
}
}
return *this;
}
//Calculates the product of this and a given polynomial
template <typename C> Polynomial<C>& Polynomial<C>::operator *=(const Polynomial<C>& rhs)
{
//Make sure to clear cache before we alter the polynomial
std::lock_guard<std::mutex> lock(this->integralGuard);
this->pImpl->integralData.clear();
//Prepare new list
auto res = std::vector<C>(this->GetHighestCoefficient() + rhs.GetHighestCoefficient() + 1, 0);
//Calculate product
for (auto i = this->GetHighestCoefficient(); i >= 0; i--)
{
for (auto j = rhs.GetHighestCoefficient(); j >= 0; j--)
{
res[i + j] += this->GetCoefficient(i) * rhs.GetCoefficient(j);
}
}
this->pImpl->coefficients = res;
return *this;
}
//Copy assignment
template <typename C> Polynomial<C>& Polynomial<C>::operator=(const Polynomial& p)
{
*this->pImpl = *p.pImpl;
return *this;
}
/*
Returns a polynomial equal to the sum of this and given polynomial.
Solves requirement 1i.
*/
template <typename C> Polynomial<C> Polynomial<C>::operator+(const Polynomial<C>& rhs)
{
Polynomial<C> p(*this);
p += rhs;
return p;
}
/*
Returns a polynomial equal to the product of this and a given polynomial.
Solves requirement 1j.
*/
template <typename C> Polynomial<C> Polynomial<C>::operator*(const Polynomial<C>& rhs)
{
Polynomial<C> p(*this);
p *= rhs;
return p;
}
//Pretty print
template <typename CO> std::ostream& operator<<(std::ostream& s, const Polynomial<CO>& p)
{
s << "P(x) = ";
for (auto i = p.GetHighestCoefficient(); i >= 0; i--)
{
s << p.GetCoefficient(i);
if (i > 0)
{
if (i == 1)
{
s << "x + ";
}
else
{
s << "x^" << i << " + ";
}
}
}
return s;
}
/*******
******** Generate specializations
********/
/*
Used for requirement 2
*/
//Integer types
template std::ostream& operator<< <int>(std::ostream&, const Polynomial<int>&);
template class Polynomial<int>;
//Floating point types
template std::ostream& operator<< <float>(std::ostream&, const Polynomial<float>&);
template class Polynomial<float>;
template std::ostream& operator<< <double>(std::ostream&, const Polynomial<double>&);
template class Polynomial<double>;
template std::ostream& operator<< <long double>(std::ostream&, const Polynomial<long double>&);
template class Polynomial<long double>;