85 lines
No EOL
1.4 KiB
Markdown
85 lines
No EOL
1.4 KiB
Markdown
# leibniz
|
|
|
|
symbolic mathematics engine in OCaml with differentiation, integration, simplification, and numerical methods
|
|
|
|
## usage
|
|
|
|
```ocaml
|
|
open Leibniz
|
|
|
|
let f = Parser.parse "x^3 * sin(x)"
|
|
let df = Diff.diff "x" f
|
|
let () = print_endline (Expr.to_string df)
|
|
|
|
let ddf = Diff.diff "x" df
|
|
let () = print_endline (Expr.to_string ddf)
|
|
|
|
let value = Eval.eval [("x", 2.0)] df
|
|
let () = print_endline (string_of_float value)
|
|
```
|
|
|
|
### implicit multiplication
|
|
|
|
```ocaml
|
|
let expr = Parser.parse "2x + 3sin(x)"
|
|
```
|
|
|
|
### symbolic integration
|
|
|
|
```ocaml
|
|
match Integrate.integrate "x" (Parser.parse "x^2") with
|
|
| Some antideriv -> print_endline (Expr.to_string antideriv)
|
|
| None -> print_endline "no closed form"
|
|
```
|
|
|
|
### taylor series
|
|
|
|
```ocaml
|
|
let series = Series.maclaurin "x" (Parser.parse "sin(x)") 5
|
|
```
|
|
|
|
### multivariate calc
|
|
|
|
```ocaml
|
|
let f = Parser.parse "x^2 + y^2"
|
|
let grad = Multivariate.gradient ["x"; "y"] f
|
|
```
|
|
|
|
### numerical methods
|
|
|
|
```ocaml
|
|
let root = Numerical.newton_raphson
|
|
(Parser.parse "x^2 - 4") "x" 1.0 0.0001 100
|
|
```
|
|
|
|
### pattern matching / rewriting
|
|
|
|
```ocaml
|
|
open Substitute
|
|
|
|
let pattern = POp("Add", [PVar "a"; PVar "a"])
|
|
let template = POp("Mul", [PConst 2.0; PVar "a"])
|
|
let expr = Parser.parse "x + x"
|
|
|
|
match rewrite pattern template expr with
|
|
| Some result -> print_endline (Expr.to_string result)
|
|
| None -> ()
|
|
```
|
|
|
|
## testing
|
|
|
|
```bash
|
|
make test
|
|
```
|
|
|
|
## examples
|
|
|
|
```bash
|
|
make examples
|
|
```
|
|
|
|
## benchmarks
|
|
|
|
```bash
|
|
make bench
|
|
``` |