Anonymous user
Numerical integration/Gauss-Legendre Quadrature: Difference between revisions
Numerical integration/Gauss-Legendre Quadrature (view source)
Revision as of 22:01, 27 January 2012
, 12 years agoUpdated D version
(D code: the task asked for N=5) |
(Updated D version) |
||
Line 231:
<lang d>import std.stdio, std.math;
size_t NBITS=50) {
/*const*/ public double[N] lroots, weight;
this() pure nothrow {
legendreCoef(/*ref*/
}
static private void legendreCoef(
pure nothrow {
lcoef[0][0] = lcoef[1][1] = 1;
foreach (int n; 2 .. N + 1) { // n must be signed
Line 254 ⟶ 252:
}
private
static FP
in int n, in FP x) pure nothrow {
FP s =
s = s * x + lcoef[n][i - 1];
}▼
return s;
▲ }
return n * (x * legendreEval(lcoef, n, x) -
(x ^^ 2 - 1);
}
foreach (i; 1 .. N + 1) {
FP x = cos(PI * (i - 0.25) / (N + 0.5));
Line 274:
do {
x1 = x;
x -= legendreEval(lcoef, N, x) /
legendreDiff(lcoef, N, x);
} while (feqrel(x, x1) < NBITS);
lroots[i - 1] = x;
x1 = legendreDiff(lcoef, N, x);
weight[i - 1] = 2 / ((1 - x ^^ 2) * (x1 ^^ 2));
}
}
public FP integrate(in FP function(FP x) f,
in FP a, in FP b) const {
immutable FP c1 = (b - a) / 2,
|