58 numIterations_(numIterations),
59 weight_(numIterations + 1),
60 root_(numIterations + 1)
62 calculateWeightAndRoot();
71 [[nodiscard]]
const std::vector<double>&
getWeight() const noexcept
82 [[nodiscard]]
const std::vector<double>&
getRoot() const noexcept
95 double derivative{ 0. };
104 Result(
const double val,
const double deriv) noexcept :
115 void calculateWeightAndRoot() noexcept
117 const auto numIterationsDouble =
static_cast<double>(numIterations_);
118 for (
uint32 step = 0; step <= numIterations_; ++step)
122 Result result = calculatePolynomialValueAndDerivative(root);
124 double newtonRaphsonRatio;
127 newtonRaphsonRatio = result.value / result.derivative;
128 root -= newtonRaphsonRatio;
129 result = calculatePolynomialValueAndDerivative(root);
130 }
while (std::fabs(newtonRaphsonRatio) > EPSILON);
133 weight_[step] = 2. / ((1. -
utils::sqr(root)) * result.derivative * result.derivative);
144 Result calculatePolynomialValueAndDerivative(
const double x)
noexcept
146 Result result(x, 0.);
148 double value_minus_1 = 1.;
150 for (
uint32 step = 2; step <= numIterations_; ++step)
152 const auto stepDouble =
static_cast<double>(step);
154 ((2. * stepDouble - 1.) * x * result.value - (stepDouble - 1.) * value_minus_1) / stepDouble;
155 result.derivative = stepDouble *
f * (x * value - result.value);
157 value_minus_1 = result.value;
158 result.value = value;
165 const double EPSILON{ 1
e-15 };
167 const uint32 numIterations_{};
168 std::vector<double> weight_{};
169 std::vector<double> root_{};
187 const std::vector<double>& weight = legendrePolynomial.
getWeight();
188 const std::vector<double>& root = legendrePolynomial.
getRoot();
190 const double width = 0.5 * (high - low);
191 const double mean = 0.5 * (low + high);
193 double gaussLegendre = 0.;
194 for (
uint32 step = 1; step <= n; ++step)
196 gaussLegendre += weight[step] *
f(width * root[step] +
mean);
199 return gaussLegendre * width;
Definition: gauss_legendre.hpp:49
LegendrePolynomial(const uint32 numIterations) noexcept
Definition: gauss_legendre.hpp:57
const std::vector< double > & getRoot() const noexcept
Definition: gauss_legendre.hpp:82
const std::vector< double > & getWeight() const noexcept
Definition: gauss_legendre.hpp:71
constexpr double pi
Pi.
Definition: Core/Constants.hpp:39
constexpr double e
eulers number
Definition: Core/Constants.hpp:37
Definition: gauss_legendre.hpp:43
double gauss_legendre(const double low, const double high, const uint32 n, const std::function< double(double)> &f)
Definition: gauss_legendre.hpp:184
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:56
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:42
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:52
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
std::uint32_t uint32
Definition: Types.hpp:40