2020-06-12 00:06:15 +02:00
# kalk
Kalk is a math parser library that supports user-defined variables and functions. An example of what it can parse:
```
f(x, y) = sum(1, 3, (2sin4/x!)^y) + cos(n deg)
a = 3
f(a, 2)
```
`>> 1.1899401098014355`
## Features
2024-03-30 16:26:58 +01:00
* Operators: `+` , `-` , `*` , `/` , `!`
* Groups: `()` , `[]` , `⌈ceil⌉` , `⌊floor⌋`
* [Vectors ](https://kalker.xyz/#vectors ): (x, y, z, ...)
* [Matrices ](https://kalker.xyz/#matrices ): [x, y, z; a, b, c; ...]
* [Pre-defined functions and constants ](https://kalker.xyz/#functions )
2020-06-12 00:06:15 +02:00
* User-defined functions and variables. `f(x, y) = xy` , `x = 5`
2024-03-30 16:26:58 +01:00
* Root finding using Newton's method (eg. x^2 = 64). Note: estimation and limited to one root
* Derivative of functions (derivatives of noisy functions or of higher order can be a bit inaccurate). `f'(2)` , `sin'(-pi)`
* Integration. `∫(0, pi, sin(x) dx)` or `∫(0, π, sin(x) dx)` , maybe sometimes be slightly off
2020-06-12 00:06:15 +02:00
* Understands fairly ambiguous syntax. Eg. `2sin50 + 2xy`
2024-03-30 16:26:58 +01:00
* Sum function: `sum(start, to, expression)` Eg. `sum(1, 3, 2n+1)` is the same as `2*1+1 + 2*2+1 + 2*3+1` = `15`
* Piecewise functions: `f(x) = { f(x + 1) if x <= 1; x otherwise }` , pressing enter before typing the final `}` will make a new line without submitting
* Different number bases: Either with a format like `0b1101` , `0o5.3` , `0xff` or a format like `1101_2` . The latter does not support letters, as they would be interpreted as variables
* Misc: separate expressions by a semicolon to write them on the same line, use the `ans` variable to get the value of the previously calculated expression
2020-06-12 00:06:15 +02:00
2020-12-31 01:47:09 +01:00
## Rust Usage
2020-06-12 21:00:03 +02:00
```rust
2020-06-12 00:06:15 +02:00
use kalk::parser;
2020-06-12 21:00:03 +02:00
let mut parser_context = parser::Context::new();
let precision = 53;
2020-12-30 16:03:02 +01:00
let result = parser::eval(& mut parser_context, "5*3", precision).unwrap().unwrap();
assert_eq!(result.to_f64(), 15f64);
2020-06-12 00:06:15 +02:00
```
2020-12-31 01:47:09 +01:00
### Using f64 instead of rug::Float
2023-01-11 00:35:08 +01:00
The cargo feature `rug` enables rug, and is used by default. If you disable this, kalk will use `f64` instead, making it more portable.
2020-12-30 16:03:02 +01:00
2020-12-31 01:47:09 +01:00
### Compiling
2020-06-12 00:06:15 +02:00
Make sure you have `diffutils` `gcc` `make` and `m4` installed.
2020-12-31 01:47:09 +01:00
## JavaScript Usage
```js
const kalk = await import("@paddim8/kalk");
const context = new kalk.Context();
console.log(context.evaluate("2pi + 3").toScientificNotation().toString());
2023-01-11 00:35:08 +01:00
```