-
Notifications
You must be signed in to change notification settings - Fork 3
/
evalPolynomial.hpp
48 lines (40 loc) · 1.2 KB
/
evalPolynomial.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
#pragma once
#include "../dfgDefs.hpp"
#include "pow.hpp"
DFG_ROOT_NS_BEGIN{ DFG_SUB_NS(math) {
// Polynomial evaluation using Horner's method (http://en.wikipedia.org/wiki/Horner_scheme)
// TODO: verify and test the method.
// coeff is collection of a_i values in formula a_0 * x^0 + a_1 * x^1 + ... + a_n * x^n
// Related code:
// -GSL polynomial evaluation (http://www.gnu.org/software/gsl/manual/html_node/Polynomial-Evaluation.html#Polynomial-Evaluation)
template <class T, class CoEffIterable_T>
T evalPolynomial(const T x, const CoEffIterable_T& coeff)
{
const auto iterBegin = std::begin(coeff);
auto iter = std::end(coeff);
if (x == 0)
return (iterBegin != iter) ? *iterBegin : T(0);
if (iterBegin == iter)
return 0;
--iter;
T bk = *iter; // == a_n
for (; iter != iterBegin;)
{
--iter;
T ak = *iter;
bk = (bk != 0) ? ak + bk * x : ak; // Testing for bk != 0 is to avoid possible evaluation of 0*infinity leading to IND.
}
return bk;
/*
// Very basic implementation
T sum = 0;
size_t n = 0;
for (; iter != iterEnd; ++iter, ++n)
{
if (*iter != 0)
sum += *iter * powN(x, n);
}
return sum;
*/
}
} }