Skip to main content

Crate symcas

Crate symcas 

Source
Expand description

§symcas - Fast, deterministic computer algebra system in Rust

Core features: canonical expression representation (arena + hash-consing), deterministic total order, plain / LaTeX formatting, and roundtrip parsing. Polynomial kernel, differentiation, series expansion, and simplification.

§Example 1: Construction and Canonical Form

use symcas::prelude::*;

let ctx = Context::new();
let x = ctx.sym("x");
let y = ctx.sym("y");

// Operator overloading constructs canonical forms: flattening, ordering,
// exact arithmetic folding, and term combination happen immediately.
let e = (x.clone() + y.clone()).pow(3) - x.clone().pow(3) - y.clone().pow(3);
assert_eq!(symcas::plain(&ctx, &e), "(x + y)^3 - x^3 - y^3");

// 同一数学内容的两条构造路径落在同一节点(hash-consing)
let a = x.clone() + x.clone();
let b = ctx.int(2) * x.clone();
assert!(a == b);

§例 2:解析与往返

use symcas::prelude::*;

let ctx = Context::new();
let x = ctx.sym("x");

let e = symcas::parse(&ctx, "x + x + 1/2*x").unwrap();
assert_eq!(symcas::plain(&ctx, &e), "5/2*x");

// Plain output parses back to the exact same expression node (roundtrip guarantee)
let back = symcas::parse(&ctx, "5/2*x").unwrap();
assert!(back == e);

// sym! 宏批量声明符号
let (u, v) = sym!(&ctx, u, v);
let _ = u + v;

Modules§

prelude

Structs§

Context
表达式构造与求值的宿主。按问题作用域创建;Clone 得到共享同一 arena 的另一个入口(不复制数据)。
Expr
表达式句柄:arena 索引 + 共享上下文引用。
Integer
任意精度整数,规范形见模块文档。
ParseError
解析错误:pos 为字节偏移。
Rational
任意精度有理数,规范形见模块文档。

Functions§

latex
LaTeX 输出:面向阅读,无往返承诺。
parse
解析 plain 文法(见 cas-parse 文档)。
plain
plain 输出:往返格式,字节由确定性全序决定。