minicas_core/
lib.rs

1//! Internals of minicas, a small Computer Algebra System.
2//!
3//! In this crate, you will generally work on implementations of [ast::AstNode]. These are:
4//!
5//!  * The enum [ast::NodeInner], which concretely implements the different variants of the AST
6//!  * The wrapper [ast::Node], which wraps a [ast::NodeInner]
7//!  * The heap-allocated wrapper [ast::HN], which is a node on the heap.
8//!
9//! Creating an AST is easiest done by parsing it from a string:
10//!
11//! ```
12//! use minicas_core::ast::*;
13//! let mut n = Node::try_from("5x * 2x").unwrap();
14//! ```
15//! Though an AST may be constructed manually (see tests for examples of this).
16//!
17//! Once you have an AST, you can typically work with it by calling methods on it, such as [ast::AstNode::finite_eval]:
18//!
19//! ```
20//! # use minicas_core::ast::*;
21//! // EG: Evaluating an expression with variables
22//! assert_eq!(
23//! 	Node::try_from("{x if x > y; otherwise y}")
24//! 		.unwrap()
25//! 		.finite_eval(&vec![("x", 42.into()), ("y", 4.into())]),
26//! 	Ok(42.into()),
27//! );
28//!
29//! // EG: Interval arithmetic
30//! assert_eq!(
31//! 	Node::try_from("x - 2y")
32//!         .unwrap()
33//!         .eval_interval(&vec![
34//!             ("x", (1.into(), 2.into())),
35//!             ("y", (5.into(), 6.into()))
36//!         ])
37//!         .unwrap()
38//!         .collect::<Result<Vec<_>, _>>(),
39//!     Ok(vec![((-11).into(), (-8).into())]),
40//! );
41//! ```
42//!
43//! Theres also a number of algorithm modules in [ast]:
44//!
45//! ```
46//! use minicas_core::ast::*;
47//!
48//! // EG: Rearrange the equation
49//! let mut lhs = NodeInner::new_var("d");
50//! make_subject(
51//!     &mut lhs,
52//!     &Node::try_from("sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))").unwrap(),
53//!     &Node::try_from("x2").unwrap() // rearrange for x2
54//! );
55//! assert_eq!(
56//!     &lhs,
57//!     Node::try_from("0 ± sqrt(pow(d, 2) - pow(y2 - y1, 2)) + x1")
58//!         .unwrap()
59//!         .as_inner(),
60//! );
61//!
62//! // EG: Constant folding
63//! let mut n = Node::try_from("x + (3 * --2)").unwrap();
64//! assert_eq!(fold(&mut n), Ok(()),);
65//! assert_eq!(n, Node::try_from("x + 6").unwrap());
66//! ```
67
68pub mod ast;
69mod ty;
70pub use ty::{Ty, TyValue};
71
72pub mod pred;
73
74pub mod rules;
75
76mod path;
77pub use path::Path;