source: src/base/polynomial.hpp@ 759a6a

Last change on this file since 759a6a was 759a6a, checked in by Julian Iseringhausen <isering@…>, 14 years ago

Unrolled loops for evaluating polynomials with Horner scheme.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1780 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 1.9 KB
Line 
1/**
2 * @file polynomial.hpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Mon Nov 21 13:26:56 2011
5 *
6 * @brief Class to represent and evaluate a polynomial.
7 *
8 */
9
10#ifndef POLYNOMIAL_HPP_
11#define POLYNOMIAL_HPP_
12
13#include <cstdarg>
14#include <vector>
15
16namespace VMG
17{
18
19class Polynomial
20{
21public:
22 Polynomial()
23 {}
24
25 Polynomial(const int& degree,...) :
26 coeff(degree+1)
27 {
28 va_list vl;
29 va_start(vl, degree);
30 for (int i=0; i<=degree; ++i)
31 coeff[i] = va_arg(vl, vmg_float);
32 va_end(vl);
33 }
34
35 Polynomial(const Polynomial& rhs) :
36 coeff(rhs.coeff.size())
37 {
38 for (unsigned int i=0; i<coeff.size(); ++i)
39 coeff[i] = rhs.coeff[i];
40 }
41
42 vmg_float operator()(const vmg_float& val) const
43 {
44 switch (coeff.size())
45 {
46 case 1:
47 return coeff[0];
48 case 2:
49 return coeff[1] * val + coeff[0];
50 case 3:
51 return (coeff[2] * val + coeff[1]) * val + coeff[0];
52 case 4:
53 return ((coeff[3] * val + coeff[2]) * val + coeff[1]) * val + coeff[0];
54 case 5:
55 return (((coeff[4] * val + coeff[3]) * val + coeff[2]) * val + coeff[1]) * val + coeff[0];
56 case 6:
57 return ((((coeff[5] * val + coeff[4]) * val + coeff[3]) * val + coeff[2]) * val + coeff[1]) * val + coeff[0];
58 case 7:
59 return (((((coeff[6]*val+coeff[5])*val+coeff[4])*val+coeff[3])*val+coeff[2])*val+coeff[1])*val+coeff[0];
60 case 8:
61 return ((((((coeff[7]*val+coeff[6])*val+coeff[5])*val+coeff[4])*val+coeff[3])*val+coeff[2])*val+coeff[1])*val+coeff[0];
62 default:
63 vmg_float result = coeff.back();
64 for (int i=coeff.size()-2; i>=0; --i)
65 result = coeff[i] + result * val;
66 return result;
67 }
68
69 }
70
71 Polynomial& operator=(const Polynomial& rhs)
72 {
73 coeff.resize(rhs.coeff.size());
74 for (unsigned int i=0; i<coeff.size(); ++i)
75 coeff[i] = rhs.coeff[i];
76
77 return *this;
78 }
79
80private:
81 std::vector<vmg_float> coeff;
82};
83
84}
85
86#endif /* POLYNOMIAL_HPP_ */
Note: See TracBrowser for help on using the repository browser.