flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
horner.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef HORNER_H
20#define HORNER_H
21
22#include <complex>
23
24namespace flexiblesusy {
25
26template <typename T, int N>
27T horner(T x, const T (&c)[N]) noexcept
28{
29 T p = c[N - 1];
30 for (int i = N - 2; i >= 0; --i) {
31 p = p*x + c[i];
32 }
33 return p;
34}
35
36
37template <int Nstart, typename T, int N>
38std::complex<T> horner(const std::complex<T>& z, const T (&coeffs)[N]) noexcept
39{
40 static_assert(0 <= Nstart && Nstart < N && N >= 2, "invalid array bounds");
41
42 const T rz = std::real(z);
43 const T iz = std::imag(z);
44 const T r = rz + rz;
45 const T s = std::norm(z);
46 T a = coeffs[N - 1], b = coeffs[N - 2];
47
48 for (int i = N - 3; i >= Nstart; --i) {
49 const T t = a;
50 a = b + r*a;
51 b = coeffs[i] - s*t;
52 }
53
54 return { rz*a + b, iz*a };
55}
56
57} // namespace flexiblesusy
58
59#endif
T horner(T x, const T(&c)[N]) noexcept
Definition horner.hpp:27