Crate cas_compute

Source
Expand description

Numerical and symbolic evaluation for CalcScript.

This crate provides an evaluator for CalcScript. It is split into two main submodules, numerical and symbolic, which handle numerical and symbolic evaluation respectively. Currently, symbolic manipulation is limited to simplification, but this will be extended in the future.

§Usage

§Numerical evaluation

The numerical module implements an evaluator for CalcScript expressions. Expressions are evaluated using the Eval trait, optionally with a custom Ctxt context, used to specify the variables and functions in scope, and the trigonometric mode to use for trigonometric functions.

The default Ctxt::default() includes some useful constants in the consts module and every builtin function in the funcs module.

See its module-level documentation for more information.

use cas_compute::numerical::eval::Eval;
use cas_parser::parser::{ast::Expr, Parser};

let mut parser = Parser::new("5sin(pi/2) * 6!");
let ast_expr = parser.try_parse_full::<Expr>().unwrap();
let result = ast_expr.eval_default().unwrap();

// 5sin(pi/2) * 6!
// = 5 * 1 * 720 = 3600
assert_eq!(result, 3600.into());

§Simplification

The symbolic module currently provides simplify to simplify expressions symbolically.

See its module-level documentation for more information.

use cas_compute::primitive::int;
use cas_compute::symbolic::{expr::{Expr, Primary}, simplify};
use cas_parser::parser::{ast::Expr as AstExpr, Parser};

let mut parser = Parser::new("x + x + x");
let ast_expr = parser.try_parse_full::<AstExpr>().unwrap();

let simplified = simplify(&ast_expr.into());

// `x + x + x = 3x`
assert_eq!(simplified, Expr::Mul(vec![
    Expr::Primary(Primary::Integer(int(3))),
    Expr::Primary(Primary::Symbol("x".to_string())),
]));

§Feature flags

  • mysql: Derives mysql_common traits for various types provided by this crate.
  • serde: Derives Serialize and Deserialize for various types provided by this crate.

Modules§

approx
consts
Builtin constants used throughout cas-rs.
funcs
All built-in functions provided by the numerical and symbolic libraries.
numerical
Numerical evaluation of CalcScript expressions, like a simple handheld calculator.
primitive
Functions to construct Integers, Floats, and Complex numbers from various types.
symbolic
Algebraic manipulation of expressions.

Macros§

eval_break
Helper macro to call Eval::eval, then check if the loop should be broken. Errors will also be propogated automatically with the ? operator.