44 releases (28 stable)
3.2.1 | Oct 3, 2024 |
---|---|
3.2.0 | Apr 3, 2024 |
3.1.0 | Mar 23, 2024 |
3.0.4 | Jul 20, 2023 |
0.2.0 | Jun 18, 2020 |
#532 in Parser implementations
312 downloads per month
Used in 2 crates
320KB
8K
SLoC
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
- Operators:
+
,-
,*
,/
,!
- Groups:
()
,[]
,⌈ceil⌉
,⌊floor⌋
- Vectors: (x, y, z, ...)
- Matrices: [x, y, z; a, b, c; ...]
- Pre-defined functions and constants
- User-defined functions and variables.
f(x, y) = xy
,x = 5
- 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 - Understands fairly ambiguous syntax. Eg.
2sin50 + 2xy
- Sum function:
sum(start, to, expression)
Eg.sum(1, 3, 2n+1)
is the same as2*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 like1101_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
Rust Usage
use kalk::parser;
let mut parser_context = parser::Context::new();
let precision = 53;
let result = parser::eval(&mut parser_context, "5*3", precision).unwrap().unwrap();
assert_eq!(result.to_f64(), 15f64);
Using f64 instead of rug::Float
The cargo feature rug
enables rug, and is used by default. If you disable this, kalk will use f64
instead, making it more portable.
Compiling
Make sure you have diffutils
gcc
make
and m4
installed.
JavaScript Usage
const kalk = await import("@paddim8/kalk");
const context = new kalk.Context();
console.log(context.evaluate("2pi + 3").toScientificNotation().toString());
Dependencies
~4MB
~74K SLoC