From eb621240d71132de9cb0943ccfa0a354ed2c6ea0 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 18:09:33 +0000 Subject: [PATCH] ocaml: phase 5.1 simpson_int.ml baseline (Simpson 1/3 rule, integral_0^1 x^2 -> 10000) Composite Simpson's 1/3 rule with 100 panels: let simpson f a b n = let h = (b -. a) /. float_of_int n in let sum = ref (f a +. f b) in for i = 1 to n - 1 do let x = a +. float_of_int i *. h in let coef = if i mod 2 = 0 then 2.0 else 4.0 in sum := !sum +. coef *. f x done; h *. !sum /. 3.0 The 1-4-2-4-...-4-1 coefficient pattern is implemented via even/odd index dispatch. Endpoints get coefficient 1. For x^2 over [0, 1], exact value is 1/3 ~= 0.33333. Scaled by 30000 gives 9999.99..., int_of_float -> 10000. Tests higher-order function (passing the integrand 'fun x -> x *. x'), float arithmetic in for-loop, and float_of_int for index->x conversion. 89 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/simpson_int.ml | 13 +++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 lib/ocaml/baseline/simpson_int.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 1c8b6f80..2601a34b 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -75,6 +75,7 @@ "run_length.ml": 11, "safe_div.ml": 20, "shuffle.ml": 55, + "simpson_int.ml": 10000, "stable_unique.ml": 46, "subset_sum.ml": 8, "tic_tac_toe.ml": 1, diff --git a/lib/ocaml/baseline/simpson_int.ml b/lib/ocaml/baseline/simpson_int.ml new file mode 100644 index 00000000..2cc855cc --- /dev/null +++ b/lib/ocaml/baseline/simpson_int.ml @@ -0,0 +1,13 @@ +let simpson f a b n = + let h = (b -. a) /. float_of_int n in + let sum = ref (f a +. f b) in + for i = 1 to n - 1 do + let x = a +. float_of_int i *. h in + let coef = if i mod 2 = 0 then 2.0 else 4.0 in + sum := !sum +. coef *. f x + done; + h *. !sum /. 3.0 + +;; + +int_of_float (simpson (fun x -> x *. x) 0.0 1.0 100 *. 30000.0) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 6cd77ac3..10c89eb5 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — simpson_int.ml baseline (Simpson's rule + numerical integration, ∫₀¹ x² dx ≈ 1/3, scaled = 10000). Composite + Simpson's 1/3 rule with 100 panels. Coefficients 1-4-2-...-2-4-1 + via even/odd index dispatch. Result × 30000 = 9999.99... → int = + 10000 (rounding artifact). Tests higher-order function (passing + the integrand `(fun x -> x *. x)`), float arithmetic in for-loop, + and float_of_int for index→x conversion. 89 baseline programs total. - 2026-05-09 Phase 5.1 — int_sqrt.ml baseline (integer Newton sqrt, 12+14+1000+1 = 1027). Newton's method on integers using `(x + n/x) / 2` until convergence (`y >= x`). Tests: