2 unstable releases

0.2.0 May 21, 2025
0.1.0 Apr 17, 2025

#761 in Math

Download history

117 downloads per month

MIT and LGPL-3.0+

625KB
13K SLoC

cas-rs

cas-rs is an opinionated computer algebra system written in Rust, originally made for use with the Discord bot CalcBot. It is currently in a very early stage of development.

See below for a guide on the REPL and some code examples.

Features

  • Simplification of algebraic expressions, learn more here
  • Graphing calculator, learn more here
  • Robust expression parser and evaluator with very human-friendly output
  • Arbitrary precision arithmetic
  • Real and complex numbers
  • Radix notation (e.g. 2'1010 = 10)
  • 60+ built-in functions, covering many expected use cases (see here)
  • Powerful formatting options, including LaTeX code output
  • Builtin REPL
  • And more!

Quick start

Below are some routine examples of how cas-rs can be used.

REPL

cas-rs comes with a builtin scripting language called "CalcScript" along with a REPL to help you try out the library. CalcScript is a compiled, mostly imperative, expression-oriented language, and attempts to keep syntax and visual noise minimal, while still readable. See the examples/ directory for examples of basic programs written in CalcScript.

To install the REPL, run the following command (Rust needs to be installed):

cargo install cas-rs --locked

You can then run the REPL with:

cas-rs

Which will immediately drop you into a REPL session where you can run CalcScript code:

> x = (1 + sqrt(5))/2
1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175212663386222353693179318006077
> x == phi
true

To learn more about CalcScript and example REPL usage, see the cas-parser crate.

General expression evaluation

cas-rs can also be used as a library to evaluate mathematical expressions and run CalcScript code. Use cas-parser to parse an expression, and cas-vm to compile and execute it. Here is an example of evaluating the expression x^2 + 5x + 6 where x = 2:

use cas_parser::parser::Parser;
use cas_vm::Vm;

let mut parser = Parser::new("x = 2; x^2 + 5x + 6");
let stmts = parser.try_parse_full_many().unwrap();

let result = Vm::compile_program(stmts)
    .unwrap()
    .run()
    .unwrap();

assert_eq!(result, 20.into());

Learn more about this in the cas-vm crate.

Algebraic simplification

cas-rs includes a rudimentary algebraic simplifier that can simplify expressions using a variety of rules. Try it out with cas-compute:

use cas_compute::symbolic::simplify;
use cas_parser::parser::{ast::Expr, Parser};

let mut parser = Parser::new("6x + 11x + 4y - 2y");
let expr = parser.try_parse_full::<Expr>().unwrap();
println!("Original: {}", expr); // 6x+11x+4y-2y

let simplified = simplify(&expr.into());
println!("Simplified: {}", simplified); // 2 * y + 17 * x

## Graphing calculator

A customizable graphing calculator is provided with the `cas-graph` crate. You can create a graph of multiple functions and points, customize the appearance of the viewport, functions, and points, and render the graph to a PNG file (or any format supported by the [`cairo`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/cairo/) crate.

```rust
use cas_graph::Graph;
use std::fs::File;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let surface = Graph::default()
        .try_add_expr("x^2 + 5x + 6").unwrap()
        .add_point((-5.0, 6.0))
        .add_point((-4.0, 2.0))
        .add_point((-3.0, 0.0))
        .add_point((-2.0, 0.0))
        .add_point((-1.0, 2.0))
        .center_on_points()
        .draw()?;

    let mut file = File::create("parabola.png")?;
    surface.write_to_png(&mut file)?;

    Ok(())
}

Output (note: colors were randomly chosen; random color selection is not included in the example code):

Acknowledgements

  • Arbitrary precision arithmetic is implemented using the rug crate.
  • The design of cas-parser is heavily inspired by the Rust compiler, syn, and ariadne.

Dependencies

~8.5MB
~157K SLoC