Jpp 19.3.0
the software that should make you happy
Loading...
Searching...
No Matches
Horner.h
Go to the documentation of this file.
1/*
2 Implementation of the Horner method of polynomial evaluation
3 Copyright (C) 2011 Darko Veberic, darko.veberic@ijs.si
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
14*/
15
16#ifndef _utl_Horner_h_
17#define _utl_Horner_h_
18
19
20namespace utl {
21
22 template<typename Float, class Tag, unsigned int order>
23 struct Polynomial {
24 static inline Float Coeff();
25 static inline Float Coeff(const Float x);
26 };
27
28
29 template<typename Float, class Tag, unsigned int order>
30 struct Horner {
31 static Float Recurse(const Float term, const Float x)
33 static Float RecurseAlt(const Float term, const Float x)
35 static Float Eval(const Float x)
39 //
40 static Float Recurse(const Float term, const Float x, const Float y)
42 static Float Eval(const Float x, const Float y)
44 };
45
46
47 template<typename Float, class Tag>
48 struct Horner<Float, Tag, 0> {
49 static Float Recurse(const Float term, const Float x)
50 { return term*x + Polynomial<Float, Tag, 0>::Coeff(); }
51 static Float Eval(const Float x)
53 //
54 static Float Recurse(const Float term, const Float x, const Float y)
55 { return term*x + Polynomial<Float, Tag, 0>::Coeff(y); }
56 static Float Eval(const Float x, const Float y)
58 };
59
60
61#define HORNER_COEFF(_Tag_, _i_, _c_) \
62 template<typename Float> struct Polynomial<Float, _Tag_, _i_> { static Float Coeff() { return Float(_c_); } }
63#define HORNER_COEFF2(_Tag_, _i_, _c_y_) \
64 template<typename Float> struct Polynomial<Float, _Tag_, _i_> { static Float Coeff(const Float y) { return Float(_c_y_); } }
65
66
67#define HORNER0(F, x, c0) (F)(c0)
68#define HORNER1(F, x, c1, c0) HORNER0(F, x, (F)(c1)*(x) + (F)(c0) )
69#define HORNER2(F, x, c2, c1, c0) HORNER1(F, x, (F)(c2)*(x) + (F)(c1), c0)
70#define HORNER3(F, x, c3, c2, c1, c0) HORNER2(F, x, (F)(c3)*(x) + (F)(c2), c1, c0)
71#define HORNER4(F, x, c4, c3, c2, c1, c0) HORNER3(F, x, (F)(c4)*(x) + (F)(c3), c2, c1, c0)
72#define HORNER5(F, x, c5, c4, c3, c2, c1, c0) HORNER4(F, x, (F)(c5)*(x) + (F)(c4), c3, c2, c1, c0)
73#define HORNER6(F, x, c6, c5, c4, c3, c2, c1, c0) HORNER5(F, x, (F)(c6)*(x) + (F)(c5), c4, c3, c2, c1, c0)
74#define HORNER7(F, x, c7, c6, c5, c4, c3, c2, c1, c0) HORNER6(F, x, (F)(c7)*(x) + (F)(c6), c5, c4, c3, c2, c1, c0)
75#define HORNER8(F, x, c8, c7, c6, c5, c4, c3, c2, c1, c0) HORNER7(F, x, (F)(c8)*(x) + (F)(c7), c6, c5, c4, c3, c2, c1, c0)
76#define HORNER9(F, x, c9, c8, c7, c6, c5, c4, c3, c2, c1, c0) HORNER8(F, x, (F)(c9)*(x) + (F)(c8), c7, c6, c5, c4, c3, c2, c1, c0)
77
78}
79
80
81#endif
Definition Horner.h:20
static Float Eval(const Float x)
Definition Horner.h:51
static Float Eval(const Float x, const Float y)
Definition Horner.h:56
static Float Recurse(const Float term, const Float x)
Definition Horner.h:49
static Float Recurse(const Float term, const Float x, const Float y)
Definition Horner.h:54
static Float Recurse(const Float term, const Float x)
Definition Horner.h:31
static Float EvalAlt(const Float x)
Definition Horner.h:37
static Float RecurseAlt(const Float term, const Float x)
Definition Horner.h:33
static Float Eval(const Float x, const Float y)
Definition Horner.h:42
static Float Eval(const Float x)
Definition Horner.h:35
static Float Recurse(const Float term, const Float x, const Float y)
Definition Horner.h:40
static Float Coeff(const Float x)
static Float Coeff()