leibniz/lib/expr.ml
2026-01-19 03:37:26 +00:00

85 lines
2.8 KiB
OCaml

type sym_const = Pi | E
type expr =
| Const of float
| SymConst of sym_const
| Var of string
| Add of expr * expr
| Sub of expr * expr
| Mul of expr * expr
| Div of expr * expr
| Pow of expr * expr
| Neg of expr
| Sin of expr
| Cos of expr
| Tan of expr
| Sinh of expr
| Cosh of expr
| Tanh of expr
| Asin of expr
| Acos of expr
| Atan of expr
| Atan2 of expr * expr
| Exp of expr
| Ln of expr
| Log of expr * expr
| Sqrt of expr
| Abs of expr
let rec to_string = function
| Const f ->
if Float.is_integer f then
string_of_int (int_of_float f)
else
string_of_float f
| SymConst Pi -> "π"
| SymConst E -> "e"
| Var s -> s
| Add (e1, e2) -> to_string_add e1 ^ " + " ^ to_string_add e2
| Sub (e1, e2) -> to_string e1 ^ " - " ^ to_string_paren e2
| Mul (e1, e2) -> to_string_mul e1 ^ "*" ^ to_string_mul e2
| Div (e1, e2) -> to_string_div e1 ^ "/" ^ to_string_paren e2
| Pow (e1, e2) -> to_string_atom e1 ^ "^" ^ to_string_pow_exp e2
| Neg e -> "-" ^ to_string_atom e
| Sin e -> "sin(" ^ to_string e ^ ")"
| Cos e -> "cos(" ^ to_string e ^ ")"
| Tan e -> "tan(" ^ to_string e ^ ")"
| Sinh e -> "sinh(" ^ to_string e ^ ")"
| Cosh e -> "cosh(" ^ to_string e ^ ")"
| Tanh e -> "tanh(" ^ to_string e ^ ")"
| Asin e -> "asin(" ^ to_string e ^ ")"
| Acos e -> "acos(" ^ to_string e ^ ")"
| Atan e -> "atan(" ^ to_string e ^ ")"
| Atan2 (e1, e2) -> "atan2(" ^ to_string e1 ^ ", " ^ to_string e2 ^ ")"
| Exp e -> "e^" ^ to_string_pow_exp e
| Ln e -> "ln(" ^ to_string e ^ ")"
| Log (base_e, arg) -> "log(" ^ to_string base_e ^ ", " ^ to_string arg ^ ")"
| Sqrt e -> "sqrt(" ^ to_string e ^ ")"
| Abs e -> "abs(" ^ to_string e ^ ")"
and to_string_atom = function
| (Const _ | SymConst _ | Var _ | Sin _ | Cos _ | Tan _ | Sinh _ | Cosh _ | Tanh _
| Asin _ | Acos _ | Atan _ | Atan2 _ | Exp _ | Ln _ | Log _ | Sqrt _ | Abs _) as e -> to_string e
| e -> "(" ^ to_string e ^ ")"
and to_string_mul = function
| (Const _ | SymConst _ | Var _ | Pow _ | Sin _ | Cos _ | Tan _ | Sinh _ | Cosh _ | Tanh _
| Asin _ | Acos _ | Atan _ | Atan2 _ | Exp _ | Ln _ | Log _ | Sqrt _ | Abs _ | Mul _) as e -> to_string e
| e -> "(" ^ to_string e ^ ")"
and to_string_div = function
| (Const _ | SymConst _ | Var _ | Pow _ | Sin _ | Cos _ | Tan _ | Sinh _ | Cosh _ | Tanh _
| Asin _ | Acos _ | Atan _ | Atan2 _ | Exp _ | Ln _ | Log _ | Sqrt _ | Abs _ | Mul _) as e -> to_string e
| e -> "(" ^ to_string e ^ ")"
and to_string_add = function
| (Add _ | Sub _) as e -> to_string e
| e -> to_string e
and to_string_pow_exp = function
| (Const _ | SymConst _ | Var _ | Pow _) as e -> to_string e
| e -> "(" ^ to_string e ^ ")"
and to_string_paren = function
| (Add _ | Sub _) as e -> "(" ^ to_string e ^ ")"
| e -> to_string e