FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
Quadrature.h
1#pragma once
2
8#include <array>
9#include <cmath>
10#include <numbers>
11
12namespace environment::field::aggregation
13{
14
16template <int N>
18{
19 std::array<double, N> node {};
20 std::array<double, N> weight {};
21
23 {
24 for (int i = 0; i < N; ++i) {
25 double x = std::cos(std::numbers::pi * (i + 0.75) / (N + 0.5));
26 double derivative = 1.0;
27 for (int iteration = 0; iteration < 100; ++iteration) {
28 double p0 = 1.0;
29 double p1 = x;
30 for (int k = 2; k <= N; ++k) {
31 const double p2 = ((2.0 * k - 1.0) * x * p1 - (k - 1.0) * p0) / k;
32 p0 = p1;
33 p1 = p2;
34 }
35 derivative = N * (x * p1 - p0) / (x * x - 1.0);
36 const double step = p1 / derivative;
37 x -= step;
38 if (std::abs(step) < 1e-15) {
39 break;
40 }
41 }
42 node[static_cast<std::size_t>(i)] = x;
43 weight[static_cast<std::size_t>(i)] = 2.0 / ((1.0 - x * x) * derivative * derivative);
44 }
45 }
46};
47
49template <int N>
50const GaussLegendre<N>& Rule()
51{
52 static const GaussLegendre<N> rule;
53 return rule;
54}
55
56} // namespace environment::field::aggregation
Nodes and weights of the N-point Gauss-Legendre rule on [-1, 1].
Definition Quadrature.h:18