NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
gauss_legendre.hpp
Go to the documentation of this file.
1 #pragma once
33 
35 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/Utils/sqr.hpp"
37 
38 #include <cmath>
39 #include <functional>
40 #include <vector>
41 
42 namespace nc
43 {
44  namespace integrate
45  {
46  //============================================================================
47  // Class Description:
51  {
52  public:
53  //============================================================================
54  // Method Description:
59  explicit LegendrePolynomial(const uint32 numIterations) noexcept :
60  numIterations_(numIterations),
61  weight_(numIterations + 1),
62  root_(numIterations + 1)
63  {
64  calculateWeightAndRoot();
65  }
66 
67  //============================================================================
68  // Method Description:
73  const std::vector<double>& getWeight() const noexcept
74  {
75  return weight_;
76  }
77 
78  //============================================================================
79  // Method Description:
84  const std::vector<double>& getRoot() const noexcept
85  {
86  return root_;
87  }
88 
89  private:
90  //============================================================================
91  // Class Description:
94  struct Result
95  {
96  double value{ 0.0 };
97  double derivative{ 0.0 };
98 
99  //============================================================================
100  // Method Description:
106  Result(const double val, const double deriv) noexcept :
107  value(val),
108  derivative(deriv)
109  {}
110  };
111 
112  //============================================================================
113  // Method Description:
116  void calculateWeightAndRoot() noexcept
117  {
118  const auto numIterationsDouble = static_cast<double>(numIterations_);
119  for (uint32 step = 0; step <= numIterations_; ++step)
120  {
121  double root = std::cos(constants::pi * (static_cast<double>(step) - 0.25) / (numIterationsDouble + 0.5));
122  Result result = calculatePolynomialValueAndDerivative(root);
123 
124  double newtonRaphsonRatio;
125  do
126  {
127  newtonRaphsonRatio = result.value / result.derivative;
128  root -= newtonRaphsonRatio;
129  result = calculatePolynomialValueAndDerivative(root);
130  } while (std::fabs(newtonRaphsonRatio) > EPSILON);
131 
132  root_[step] = root;
133  weight_[step] = 2.0 / ((1.0 - utils::sqr(root)) * result.derivative * result.derivative);
134  }
135  }
136 
137  //============================================================================
138  // Method Description:
144  Result calculatePolynomialValueAndDerivative(const double x) noexcept
145  {
146  Result result(x, 0.0);
147 
148  double value_minus_1 = 1.0;
149  const double f = 1.0 / (utils::sqr(x) - 1.0);
150  for (uint32 step = 2; step <= numIterations_; ++step)
151  {
152  const auto stepDouble = static_cast<double>(step);
153  const double value = ((2.0 * stepDouble - 1.0) * x * result.value - (stepDouble - 1.0) * value_minus_1) / stepDouble;
154  result.derivative = stepDouble * f * (x * value - result.value);
155 
156  value_minus_1 = result.value;
157  result.value = value;
158  }
159 
160  return result;
161  }
162 
163  //===================================Attributes==============================
164  const double EPSILON{ 1e-15 };
165 
166  const uint32 numIterations_;
167  std::vector<double> weight_;
168  std::vector<double> root_;
169  };
170 
171  //============================================================================
172  // Method Description:
182  inline double gauss_legendre(const double low, const double high, const uint32 n,
183  const std::function<double(double)>& f)
184  {
185  const LegendrePolynomial legendrePolynomial(n);
186  const std::vector<double>& weight = legendrePolynomial.getWeight();
187  const std::vector<double>& root = legendrePolynomial.getRoot();
188 
189  const double width = 0.5 * (high - low);
190  const double mean = 0.5 * (low + high);
191 
192  double gaussLegendre = 0.0;
193  for (uint32 step = 1; step <= n; ++step)
194  {
195  gaussLegendre += weight[step] * f(width * root[step] + mean);
196  }
197 
198  return gaussLegendre * width;
199  }
200  } // namespace integrate
201 } // namespace nc
Definition: gauss_legendre.hpp:51
LegendrePolynomial(const uint32 numIterations) noexcept
Definition: gauss_legendre.hpp:59
const std::vector< double > & getRoot() const noexcept
Definition: gauss_legendre.hpp:84
const std::vector< double > & getWeight() const noexcept
Definition: gauss_legendre.hpp:73
constexpr double pi
Pi.
Definition: Constants.hpp:43
constexpr double e
eulers number
Definition: Constants.hpp:41
double gauss_legendre(const double low, const double high, const uint32 n, const std::function< double(double)> &f)
Definition: gauss_legendre.hpp:182
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:56
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Definition: Coordinate.hpp:45
auto cos(dtype inValue) noexcept
Definition: cos.hpp:51
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:53
std::uint32_t uint32
Definition: Types.hpp:40