#evaluation #algebra #calculator #calcscript

cas-compute

Numerical and symbolic evaluation for CalcScript

1 unstable release

new 0.1.0 Apr 15, 2025

#645 in Math

Download history 77/week @ 2025-04-11

77 downloads per month
Used in 2 crates

MIT and LGPL-3.0+

500KB
11K SLoC

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.

Dependencies

~6–15MB
~163K SLoC