symbolic mathematics engine in OCaml with differentiation, integration, simplification, and numerical methods
1type sym_const = Pi | E
2
3type expr =
4 | Const of float
5 | SymConst of sym_const
6 | Var of string
7 | Add of expr * expr
8 | Sub of expr * expr
9 | Mul of expr * expr
10 | Div of expr * expr
11 | Pow of expr * expr
12 | Neg of expr
13 | Sin of expr
14 | Cos of expr
15 | Tan of expr
16 | Sinh of expr
17 | Cosh of expr
18 | Tanh of expr
19 | Asin of expr
20 | Acos of expr
21 | Atan of expr
22 | Atan2 of expr * expr
23 | Exp of expr
24 | Ln of expr
25 | Log of expr * expr
26 | Sqrt of expr
27 | Abs of expr
28
29let rec to_string = function
30 | Const f ->
31 if Float.is_integer f then
32 string_of_int (int_of_float f)
33 else
34 string_of_float f
35 | SymConst Pi -> "π"
36 | SymConst E -> "e"
37 | Var s -> s
38 | Add (e1, e2) -> to_string_add e1 ^ " + " ^ to_string_add e2
39 | Sub (e1, e2) -> to_string e1 ^ " - " ^ to_string_paren e2
40 | Mul (e1, e2) -> to_string_mul e1 ^ "*" ^ to_string_mul e2
41 | Div (e1, e2) -> to_string_div e1 ^ "/" ^ to_string_paren e2
42 | Pow (e1, e2) -> to_string_atom e1 ^ "^" ^ to_string_pow_exp e2
43 | Neg e -> "-" ^ to_string_atom e
44 | Sin e -> "sin(" ^ to_string e ^ ")"
45 | Cos e -> "cos(" ^ to_string e ^ ")"
46 | Tan e -> "tan(" ^ to_string e ^ ")"
47 | Sinh e -> "sinh(" ^ to_string e ^ ")"
48 | Cosh e -> "cosh(" ^ to_string e ^ ")"
49 | Tanh e -> "tanh(" ^ to_string e ^ ")"
50 | Asin e -> "asin(" ^ to_string e ^ ")"
51 | Acos e -> "acos(" ^ to_string e ^ ")"
52 | Atan e -> "atan(" ^ to_string e ^ ")"
53 | Atan2 (e1, e2) -> "atan2(" ^ to_string e1 ^ ", " ^ to_string e2 ^ ")"
54 | Exp e -> "e^" ^ to_string_pow_exp e
55 | Ln e -> "ln(" ^ to_string e ^ ")"
56 | Log (base_e, arg) -> "log(" ^ to_string base_e ^ ", " ^ to_string arg ^ ")"
57 | Sqrt e -> "sqrt(" ^ to_string e ^ ")"
58 | Abs e -> "abs(" ^ to_string e ^ ")"
59
60and to_string_atom = function
61 | (Const _ | SymConst _ | Var _ | Sin _ | Cos _ | Tan _ | Sinh _ | Cosh _ | Tanh _
62 | Asin _ | Acos _ | Atan _ | Atan2 _ | Exp _ | Ln _ | Log _ | Sqrt _ | Abs _) as e -> to_string e
63 | e -> "(" ^ to_string e ^ ")"
64
65and to_string_mul = function
66 | (Const _ | SymConst _ | Var _ | Pow _ | Sin _ | Cos _ | Tan _ | Sinh _ | Cosh _ | Tanh _
67 | Asin _ | Acos _ | Atan _ | Atan2 _ | Exp _ | Ln _ | Log _ | Sqrt _ | Abs _ | Mul _) as e -> to_string e
68 | e -> "(" ^ to_string e ^ ")"
69
70and to_string_div = function
71 | (Const _ | SymConst _ | Var _ | Pow _ | Sin _ | Cos _ | Tan _ | Sinh _ | Cosh _ | Tanh _
72 | Asin _ | Acos _ | Atan _ | Atan2 _ | Exp _ | Ln _ | Log _ | Sqrt _ | Abs _ | Mul _) as e -> to_string e
73 | e -> "(" ^ to_string e ^ ")"
74
75and to_string_add = function
76 | (Add _ | Sub _) as e -> to_string e
77 | e -> to_string e
78
79and to_string_pow_exp = function
80 | (Const _ | SymConst _ | Var _ | Pow _) as e -> to_string e
81 | e -> "(" ^ to_string e ^ ")"
82
83and to_string_paren = function
84 | (Add _ | Sub _) as e -> "(" ^ to_string e ^ ")"
85 | e -> to_string e