ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfe_poly.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file dfe_poly.hpp
1 // SPDX-License-Identifier: MIT
2 // Copyright 2018-2019 Moritz Kiehn
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 
26 
27 #include <array>
28 #include <iterator>
29 #include <type_traits>
30 #include <utility>
31 
32 #pragma once
33 
34 namespace dfe {
35 
46 template<typename T, typename Container>
47 constexpr T
48 polynomial_val(const T& x, const Container& coeffs) {
49  // Use Horner's method to evaluate polynomial, i.e. expand
50  // f(x) = c0 + c1*x + c2*x^2 + c3*x^3
51  // to the following form
52  // f(x) = c0 + x * (c1 + x * (c2 + x * c3))
53  // to allow iterative computation with minimal number of operations
54  T value = x; // make sure dynamic-sized types, e.g. std::valarray, work
55  value = 0;
56  for (auto c = std::rbegin(coeffs); c != std::rend(coeffs); ++c) {
57  value = *c + x * value;
58  }
59  return value;
60 }
61 
76 template<typename T, typename Container>
77 constexpr std::pair<T, T>
78 polynomial_valder(const T& x, const Container& coeffs) {
79  // Use Horner's method to evaluate polynomial and its derivative at the
80  // same time
81  T q = x; // make sure dynamic-sized types, e.g. std::valarray, work
82  T p = x;
83  q = 0;
84  p = 0;
85  for (auto c = std::rbegin(coeffs); c != std::rend(coeffs); ++c) {
86  q = p + x * q;
87  p = *c + x * p;
88  }
89  return {p, q};
90 }
91 
102 template<typename T, typename Container>
103 constexpr T
104 polynomial_der(const T& x, const Container& coeffs) {
105  return polynomial_valder(x, coeffs).second;
106 }
107 
109 template<typename T, typename U>
110 constexpr auto
111 polynomial_val(const T& x, std::initializer_list<U> coeffs) {
112  return polynomial_val<T, std::initializer_list<U>>(x, coeffs);
113 }
114 
116 template<typename T, typename U>
117 constexpr auto
118 polynomial_der(const T& x, std::initializer_list<U> coeffs) {
119  return polynomial_der<T, std::initializer_list<U>>(x, coeffs);
120 }
121 
123 template<typename T, typename U>
124 constexpr auto
125 polynomial_valder(const T& x, std::initializer_list<U> coeffs) {
126  return polynomial_valder<T, std::initializer_list<U>>(x, coeffs);
127 }
128 
129 } // namespace dfe