Numerical integration/Adaptive Simpson's method: Difference between revisions

→‎{{header|ATS}}: There was needless recomputation of f. That is fixed now. Also more passing around of f than was necessary; also fixed.
(→‎{{header|ATS}}: There was needless recomputation of f. That is fixed now. Also more passing around of f than was necessary; also fixed.)
Line 239:
quadrature_by_adaptive_simpson (f, a, b, tol, depth) =
let
(* SomeA shorthandsshorthand. Sometimes you will see typedefs written instead as
instead as "stadef", which is a more general notation. *)
typedef real = g0float tk
typedef real_func = real -> real
 
(* In ATS, you can nest functions inside functions to whatever
depth you like. *)
fn
_quad_asr_simpsons (fa : real_funcreal,
a : real,
fa : real,
b : real,
Line 271 ⟶ 269:
val h = b - a
in
@(m, fm, (h / g0i2f 6) * (f(a)fa + (g0i2f 4 * fm) + f(b)fb))
end
 
Line 286 ⟶ 284:
run out of stack space, etc.). *)
.<depth>.
(fa : real_funcreal,
a : real,
fa : real,
b : real,
Line 302 ⟶ 299:
perhaps it is good as documentation. In some instances it
is REALLY what you want. *)
val @(lm, flm, left) = _quad_asr_simpsons (f, a, fa, m, fm)
and @(rm, frm, right) = _quad_asr_simpsons (f, m, fm, b, fb)
 
val delta = left + right - whole
Line 321 ⟶ 318:
else
let
val left_val = _quad_asr (f, a, fa, m, fm, tol_, left,
lm, flm, depth - 1)
and right_val = _quad_asr (f, m, fm, b, fb, tol_, right,
rm, frm, depth - 1)
in
Line 334 ⟶ 331:
val @(fa, fb) = @(f(a), f(b))
 
val @(m, fm, whole) = _quad_asr_simpsons (f, a, fa, b, fb)
in
_quad_asr (f, a, fa, b, fb, tol, whole, m, fm, depth)
end
 
1,448

edits