1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Tools for parsing and manipulating mathematical expressions
//!
//! Math-engine is a set of tools for manipulating mathematical expressions.
//! These expressions may contain various operations and variables. The crate
//! defines tools for building, evaluating, and operating on such expressions.
//!
//! ## Example
//!
//! Math-engine allows you to build variable expressions and evaluate them
//! within a context, i.e. a set of values for each variable.
//! ```
//! let expr = Expression::parse("1.0 + 3.0 * (4.0 - x)").unwrap();
//! let ctx = Context:new().with_variable("x", 3.0);
//!
//! let eval = expr.eval_with_context(&ctx).unwrap();
//!
//! assert!(eval, 4.0);
//! ```
//!
//! Basic operators are implemented for allowing easy to use building:
//! ```
//! let e1 = Expression::constant(3.0);
//! let e2 = Expression::variable("x");
//! let e = e1 + e2;
//!
//! println!("{}", e);
//! ```
//!
//! Also, some useful operations are available for manipulating expressions,
//! such as derivation and simplification by constant propagation:
//! ```
//! let e1 = Expression("1.0 * y + 2.0 * x").unwrap();
//! let deriv = e1.derivate("x");
//! println!("{}", deriv);
//!
//! let simp = deriv.constant_propagation().unwrap();
//! println!("{}", simp);
//! ```