Numerical integration: Difference between revisions
Content added Content deleted
(Updated D versions) |
(Updated both D entries) |
||
Line 893: | Line 893: | ||
template integrate(alias method) { |
template integrate(alias method) { |
||
double integrate(F, Float)(F f, in Float a, |
double integrate(F, Float)(in F f, in Float a, |
||
in Float b, in int steps) { |
in Float b, in int steps) { |
||
double s = 0.0; |
double s = 0.0; |
||
immutable double h = (b - a) / steps; |
immutable double h = (b - a) / steps; |
||
foreach (i; 0 .. steps) |
foreach (i; 0 .. steps) |
||
s += method(f, a + h*i, h); |
s += method(f, a + h * i, h); |
||
return h * s; |
return h * s; |
||
} |
} |
||
Line 910: | Line 910: | ||
double rectangularMiddle(F, Float)(in F f, in Float x, in Float h) |
double rectangularMiddle(F, Float)(in F f, in Float x, in Float h) |
||
pure nothrow { |
pure nothrow { |
||
return f(x + h/2); |
return f(x + h / 2); |
||
} |
} |
||
Line 925: | Line 925: | ||
double simpson(F, Float)(in F f, in Float x, in Float h) |
double simpson(F, Float)(in F f, in Float x, in Float h) |
||
pure nothrow { |
pure nothrow { |
||
return (f(x) + 4*f(x + h/2) + f(x + h)) / 6; |
return (f(x) + 4 * f(x + h / 2) + f(x + h)) / 6; |
||
} |
} |
||
Line 988: | Line 988: | ||
immutable Float h = (b - a) / steps; |
immutable Float h = (b - a) / steps; |
||
foreach (i; 0 .. steps) |
foreach (i; 0 .. steps) |
||
s += method!(f, Float)(a + h*i, h); |
s += method!(f, Float)(a + h * i, h); |
||
return h * s; |
return h * s; |
||
} |
} |
||
Line 1,001: | Line 1,001: | ||
double rectangularMiddle(alias f, Float)(in Float x, in Float h) |
double rectangularMiddle(alias f, Float)(in Float x, in Float h) |
||
pure nothrow { |
pure nothrow { |
||
return f(x + h/2); |
return f(x + h / 2); |
||
} |
} |
||
Line 1,016: | Line 1,016: | ||
double simpson(alias f, Float)(in Float x, in Float h) |
double simpson(alias f, Float)(in Float x, in Float h) |
||
pure nothrow { |
pure nothrow { |
||
return (f(x) + 4*f(x + h/2) + f(x + h)) / 6; |
return (f(x) + 4 * f(x + h / 2) + f(x + h)) / 6; |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
void main() { |
void main() { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
alias TypeTuple!("rectangular left: ", |
alias TypeTuple!("rectangular left: ", |
||
"rectangular middle: ", |
"rectangular middle: ", |