symbolic mathematics engine in OCaml with differentiation, integration, simplification, and numerical methods
17

Configure Feed

Select the types of activity you want to include in your feed.

leibniz / bench / bench_suite.ml
3.3 kB 107 lines
1open Leibniz 2 3let time_operation name f = 4 let start = Unix.gettimeofday () in 5 let result = f () in 6 let elapsed = Unix.gettimeofday () -. start in 7 Printf.printf "%-40s: %.6f seconds\n" name elapsed; 8 result 9 10let () = 11 print_endline "leibniz benchmark suite\n"; 12 13 print_endline "parsing performance:"; 14 let _ = time_operation "parse simple expression" (fun () -> 15 Parser.parse "x + 1" 16 ) in 17 let _ = time_operation "parse complex expression" (fun () -> 18 Parser.parse "sin(x^2) * cos(y) + exp(z) / sqrt(w)" 19 ) in 20 21 print_endline "\nsimplification performance:"; 22 let simple_expr = Parser.parse "(x + 0) * 1" in 23 let _ = time_operation "simplify simple" (fun () -> 24 Simplify.simplify simple_expr 25 ) in 26 27 let complex_expr = Parser.parse "(x + 0) * (y + 0) + (x * 0) + (x * 1)" in 28 let _ = time_operation "simplify complex" (fun () -> 29 Simplify.simplify complex_expr 30 ) in 31 32 let nested = Parser.parse "((x + 0) * (y + 0)) * ((z + 0) * (w + 0))" in 33 let _ = time_operation "simplify deeply nested" (fun () -> 34 Simplify.simplify nested 35 ) in 36 37 print_endline "\ndifferentiation performance:"; 38 let poly = Parser.parse "x^5 + x^4 + x^3 + x^2 + x + 1" in 39 let _ = time_operation "diff polynomial" (fun () -> 40 Diff.diff "x" poly 41 ) in 42 43 let trig = Parser.parse "sin(x) * cos(x) * tan(x)" in 44 let _ = time_operation "diff trigonometric" (fun () -> 45 Diff.diff "x" trig 46 ) in 47 48 let composite = Parser.parse "sin(cos(sin(x)))" in 49 let _ = time_operation "diff nested functions" (fun () -> 50 Diff.diff "x" composite 51 ) in 52 53 print_endline "\nintegration performance:"; 54 let int_expr = Parser.parse "x^3" in 55 let _ = time_operation "integrate polynomial" (fun () -> 56 Integrate.integrate "x" int_expr 57 ) in 58 59 let int_trig = Parser.parse "sin(x)" in 60 let _ = time_operation "integrate sin(x)" (fun () -> 61 Integrate.integrate "x" int_trig 62 ) in 63 64 print_endline "\nnumerical methods performance:"; 65 let root_expr = Parser.parse "x^2 - 4" in 66 let _ = time_operation "newton-raphson root finding" (fun () -> 67 Numerical.newton_raphson root_expr "x" 1.0 0.0001 100 68 ) in 69 70 let _ = time_operation "bisection root finding" (fun () -> 71 Numerical.bisection root_expr "x" 0.0 3.0 0.0001 100 72 ) in 73 74 let quad_expr = Parser.parse "x^2" in 75 let _ = time_operation "trapezoidal integration" (fun () -> 76 Numerical.trapezoidal quad_expr "x" 0.0 1.0 1000 77 ) in 78 79 let _ = time_operation "simpson's integration" (fun () -> 80 Numerical.simpsons quad_expr "x" 0.0 1.0 1000 81 ) in 82 83 print_endline "\nmultivariate operations:"; 84 let mv_expr = Parser.parse "x^2 + y^2 + z^2" in 85 let _ = time_operation "compute gradient (3 vars)" (fun () -> 86 Multivariate.gradient ["x"; "y"; "z"] mv_expr 87 ) in 88 89 let _ = time_operation "compute hessian (3 vars)" (fun () -> 90 Multivariate.hessian ["x"; "y"; "z"] mv_expr 91 ) in 92 93 print_endline "\nformatting performance:"; 94 let fmt_expr = Parser.parse "sin(x^2) + cos(y^2)" in 95 let _ = time_operation "to_string" (fun () -> 96 Expr.to_string fmt_expr 97 ) in 98 99 let _ = time_operation "to_latex" (fun () -> 100 Format.to_latex fmt_expr 101 ) in 102 103 let _ = time_operation "to_graphviz" (fun () -> 104 Format.to_graphviz fmt_expr 105 ) in 106 107 print_endline "\nbenchmark complete."