Crate math_utils_lib
source ·Expand description
This crate provides a number of math utilities:
- parsing and evaluating expressions containing matrices and vectors
- solving equations and systems of equations
- exporting a LaTeX document from a collection of parsed expressions or solved equations (see StepType and export())
Precision (PREC):
Use feature high-prec for a precision of 13, standard precision is 8. The precision is used to solve equations at the set precision. The actual output precision for printing is the defined precision - 2.
Important Rules:
Vectors and matrices can be written out in an expression (e.g [3, 5, 7] or [[3, 4, 5], [3, 4,
5], [2, 6, 7]]). Matrices are in column major order.
Variable Names can only start with an alphabetic letter or a \ (see Variable)
See SimpleOpType and AdvancedOpType for all allowed operations and functions.
This crate has not hit 1.0.0 yet, breaking changes are bound to
happen!
§Examples
(if you want to use “?” for error handling in your application, have a look at MathLibError)
§Evaluations:
let res = quick_eval("3*3".to_string(), vec![])?;
assert_eq!(res, Value::Scalar(9.));
let x = Variable::new("x".to_string(), Value::Scalar(3.));
let res = quick_eval("3x".to_string(), vec![x])?;
assert_eq!(res, Value::Scalar(9.));
let a = Variable::new("A".to_string(), Value::Vector(vec![3., 5., 8.]));
let res = quick_eval("3A".to_string(), vec![a])?;
assert_eq!(res, Value::Vector(vec![9., 15., 24.]));
let a = Variable::new("A".to_string(), Value::Vector(vec![3., 5., 8.]));
let b = Variable::new("B".to_string(), Value::Matrix(vec![vec![2., 0., 0.], vec![0., 2., 0.],
vec![0., 0., 1.]]));
let res = quick_eval("B*A".to_string(), vec![a, b])?;
assert_eq!(res, Value::Vector(vec![6., 10., 8.]));
§Equations:
let equation = "x^2=9".to_string();
let res = quick_solve(equation, "x".to_string(), vec![])?;
let res_rounded = res.iter().map(|x| x.round(3)).collect::<Vec<Value>>();
assert_eq!(res_rounded, vec![Value::Scalar(3.), Value::Scalar(-3.)]);
let equation = "400-100g=600-100k, -600-100g=-400-100k, 1000-100g=100k".to_string();
let res = quick_solve(equation, vec![])?;
let res_rounded = res.iter().map(|x| x.round(3)).collect::<Vec<Value>>();
assert_eq!(res_rounded, vec![Value::Vector(vec![4., 6.])]);
§LaTeX:
let expression = "((25x^3-96x^2+512x+384)/(x^4+2x^3+90x^2-128x+1664)^(1.5))/(-sqrt(1-((32-x+x^2)/(((x-1)^2+25)(x^2+64)))^2))".to_string();
let parsed = parse(expression)?;
let vars = vec![Variable::new("x".to_string(), Value::Scalar(-0.655639))];
let result = eval(&parsed, &vars)?;
let var_assign = StepType::Calc((Binary::Value(Value::Scalar(-0.655639)), Value::Scalar(-0.655639), Some("x".to_string())));
let step = StepType::Calc((parsed, result, None));
export(vec![var_assign, step], "export".to_string(), ExportType::Png);
Output (export-1.png):
Re-exports§
pub use basetypes::Value;
pub use basetypes::Variable;
pub use latex_export::export;
pub use latex_export::ExportType;
pub use latex_export::StepType;
pub use parser::parse;
pub use parser::eval;
pub use solver::solve;
pub use errors::MathLibError;
Modules§
Constants§
- defines the precision used by the equation solver and the printing precision, which is PREC - 2.
Functions§
- evaluates a given expression using the given variables (e and pi are provided by the function). If you just want the Binary Tree, have a look at parse().
- solves an equation or a system of equations towards the variables not yet specified in vars. It can additionaly be provided with other variables. If you just want to solve equations with parsed left and right hand side binaries, have a look at solve().