60 numIterations_(numIterations),
61 weight_(numIterations + 1),
62 root_(numIterations + 1)
64 calculateWeightAndRoot();
73 const std::vector<double>&
getWeight() const noexcept
84 const std::vector<double>&
getRoot() const noexcept
97 double derivative{ 0.0 };
106 Result(
const double val,
const double deriv) noexcept :
117 void calculateWeightAndRoot() noexcept
119 const auto numIterationsDouble =
static_cast<double>(numIterations_);
120 for (
uint32 step = 0; step <= numIterations_; ++step)
124 Result result = calculatePolynomialValueAndDerivative(root);
126 double newtonRaphsonRatio;
129 newtonRaphsonRatio = result.value / result.derivative;
130 root -= newtonRaphsonRatio;
131 result = calculatePolynomialValueAndDerivative(root);
132 }
while (std::fabs(newtonRaphsonRatio) > EPSILON);
135 weight_[step] = 2.0 / ((1.0 -
utils::sqr(root)) * result.derivative * result.derivative);
146 Result calculatePolynomialValueAndDerivative(
const double x) noexcept
148 Result result(x, 0.0);
150 double value_minus_1 = 1.0;
152 for (
uint32 step = 2; step <= numIterations_; ++step)
154 const auto stepDouble =
static_cast<double>(step);
156 ((2.0 * stepDouble - 1.0) * x * result.value - (stepDouble - 1.0) * value_minus_1) / stepDouble;
157 result.derivative = stepDouble *
f * (x * value - result.value);
159 value_minus_1 = result.value;
160 result.value = value;
167 const double EPSILON{ 1
e-15 };
169 const uint32 numIterations_;
170 std::vector<double> weight_;
171 std::vector<double> root_;
189 const std::vector<double>& weight = legendrePolynomial.
getWeight();
190 const std::vector<double>& root = legendrePolynomial.
getRoot();
192 const double width = 0.5 * (high - low);
193 const double mean = 0.5 * (low + high);
195 double gaussLegendre = 0.0;
196 for (
uint32 step = 1; step <= n; ++step)
198 gaussLegendre += weight[step] *
f(width * root[step] +
mean);
201 return gaussLegendre * width;
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:186
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Definition: Coordinate.hpp:45
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40