107 lines
3.2 KiB
OCaml
107 lines
3.2 KiB
OCaml
open Leibniz
|
|
|
|
let time_operation name f =
|
|
let start = Unix.gettimeofday () in
|
|
let result = f () in
|
|
let elapsed = Unix.gettimeofday () -. start in
|
|
Printf.printf "%-40s: %.6f seconds\n" name elapsed;
|
|
result
|
|
|
|
let () =
|
|
print_endline "leibniz benchmark suite\n";
|
|
|
|
print_endline "parsing performance:";
|
|
let _ = time_operation "parse simple expression" (fun () ->
|
|
Parser.parse "x + 1"
|
|
) in
|
|
let _ = time_operation "parse complex expression" (fun () ->
|
|
Parser.parse "sin(x^2) * cos(y) + exp(z) / sqrt(w)"
|
|
) in
|
|
|
|
print_endline "\nsimplification performance:";
|
|
let simple_expr = Parser.parse "(x + 0) * 1" in
|
|
let _ = time_operation "simplify simple" (fun () ->
|
|
Simplify.simplify simple_expr
|
|
) in
|
|
|
|
let complex_expr = Parser.parse "(x + 0) * (y + 0) + (x * 0) + (x * 1)" in
|
|
let _ = time_operation "simplify complex" (fun () ->
|
|
Simplify.simplify complex_expr
|
|
) in
|
|
|
|
let nested = Parser.parse "((x + 0) * (y + 0)) * ((z + 0) * (w + 0))" in
|
|
let _ = time_operation "simplify deeply nested" (fun () ->
|
|
Simplify.simplify nested
|
|
) in
|
|
|
|
print_endline "\ndifferentiation performance:";
|
|
let poly = Parser.parse "x^5 + x^4 + x^3 + x^2 + x + 1" in
|
|
let _ = time_operation "diff polynomial" (fun () ->
|
|
Diff.diff "x" poly
|
|
) in
|
|
|
|
let trig = Parser.parse "sin(x) * cos(x) * tan(x)" in
|
|
let _ = time_operation "diff trigonometric" (fun () ->
|
|
Diff.diff "x" trig
|
|
) in
|
|
|
|
let composite = Parser.parse "sin(cos(sin(x)))" in
|
|
let _ = time_operation "diff nested functions" (fun () ->
|
|
Diff.diff "x" composite
|
|
) in
|
|
|
|
print_endline "\nintegration performance:";
|
|
let int_expr = Parser.parse "x^3" in
|
|
let _ = time_operation "integrate polynomial" (fun () ->
|
|
Integrate.integrate "x" int_expr
|
|
) in
|
|
|
|
let int_trig = Parser.parse "sin(x)" in
|
|
let _ = time_operation "integrate sin(x)" (fun () ->
|
|
Integrate.integrate "x" int_trig
|
|
) in
|
|
|
|
print_endline "\nnumerical methods performance:";
|
|
let root_expr = Parser.parse "x^2 - 4" in
|
|
let _ = time_operation "newton-raphson root finding" (fun () ->
|
|
Numerical.newton_raphson root_expr "x" 1.0 0.0001 100
|
|
) in
|
|
|
|
let _ = time_operation "bisection root finding" (fun () ->
|
|
Numerical.bisection root_expr "x" 0.0 3.0 0.0001 100
|
|
) in
|
|
|
|
let quad_expr = Parser.parse "x^2" in
|
|
let _ = time_operation "trapezoidal integration" (fun () ->
|
|
Numerical.trapezoidal quad_expr "x" 0.0 1.0 1000
|
|
) in
|
|
|
|
let _ = time_operation "simpson's integration" (fun () ->
|
|
Numerical.simpsons quad_expr "x" 0.0 1.0 1000
|
|
) in
|
|
|
|
print_endline "\nmultivariate operations:";
|
|
let mv_expr = Parser.parse "x^2 + y^2 + z^2" in
|
|
let _ = time_operation "compute gradient (3 vars)" (fun () ->
|
|
Multivariate.gradient ["x"; "y"; "z"] mv_expr
|
|
) in
|
|
|
|
let _ = time_operation "compute hessian (3 vars)" (fun () ->
|
|
Multivariate.hessian ["x"; "y"; "z"] mv_expr
|
|
) in
|
|
|
|
print_endline "\nformatting performance:";
|
|
let fmt_expr = Parser.parse "sin(x^2) + cos(y^2)" in
|
|
let _ = time_operation "to_string" (fun () ->
|
|
Expr.to_string fmt_expr
|
|
) in
|
|
|
|
let _ = time_operation "to_latex" (fun () ->
|
|
Format.to_latex fmt_expr
|
|
) in
|
|
|
|
let _ = time_operation "to_graphviz" (fun () ->
|
|
Format.to_graphviz fmt_expr
|
|
) in
|
|
|
|
print_endline "\nbenchmark complete."
|