symbolic
stableA symbolic math plugin for building, manipulating, and evaluating mathematical expression trees, including differentiation, integration, Taylor series, and polynomial analysis.
use plugin symbolic::{parse, constant, variable, …} Functions (20)
- parse Parse an expression string into an Expr handle
- constant Create a constant-value expression
- variable Create a variable expression by name
- roots_quadratic Solve ax²+bx+c=0, returns complex roots
- to_string Convert expression back to a string
- evaluate Evaluate at a single x value
- evaluate_with Evaluate with a named-variable table
- derivative Differentiate with respect to x
- partial_derivative Differentiate with respect to any variable
- gradient Partial derivatives for a list of variables
- integrate Symbolic integration with respect to a variable
- taylor_series Taylor/Maclaurin series approximation
- simplify Apply algebraic simplification rules
- expand Distribute multiplication over addition
- factor Factor simple expressions (e.g. difference of squares)
- substitute Replace a variable with another expression
- contains_var Check if expression references a variable
- degree Polynomial degree with respect to a variable
- coefficients Polynomial coefficients as a table
- is_polynomial Check if expression is a polynomial
Parse an expression string into an Expr handle
Parses a mathematical expression string into an Expr handle. Supports +, -, *, /, ^, unary -, sin(), cos(), and named variables.
use plugin symbolic::{parse, variable, constant}
let expr = parse("x^2 + 2*x + 1")
print(expr.to_string(expr))
Create a constant-value expression
Creates a leaf expression holding a numeric constant.
use plugin symbolic::{constant}
let pi = constant(3.14159)
Create a variable expression by name
Creates a named variable expression. Any identifier is valid as a variable name.
use plugin symbolic::{variable, parse}
let x = variable("x")
let y = variable("y")
Solve ax²+bx+c=0, returns complex roots
Solves ax² + bx + c = 0 and returns a table of two roots, each as {real, imag}. Works for both real and complex roots.
use plugin symbolic::{roots_quadratic}
let roots = roots_quadratic(1.0, -3.0, 2.0)
print("r1={roots[1]["real"]}, r2={roots[2]["real"]}")
let complex_roots = roots_quadratic(1.0, 0.0, 1.0)
print("real={complex_roots[1]["real"]}, imag={complex_roots[1]["imag"]}")
Convert expression back to a string
Converts the expression tree back to a human-readable string with parentheses showing structure.
use plugin symbolic::{parse}
let expr = parse("x^2 + 2*x + 1")
let deriv = expr.derivative(expr)
print(deriv.to_string(deriv))
Evaluate at a single x value
Evaluates the expression by substituting x with the given numeric value.
use plugin symbolic::{parse}
let f = parse("x^2 + 1")
print(f.evaluate(f, 3.0))
Evaluate with a named-variable table
Evaluates a multi-variable expression using a table mapping variable names to values.
use plugin symbolic::{parse}
let f = parse("x^2 + y^2")
let result = f.evaluate_with(f, #{"x": 3.0, "y": 4.0})
print(result)
Differentiate with respect to x
Differentiates the expression symbolically with respect to x. Applies product rule, quotient rule, chain rule, and trig identities.
use plugin symbolic::{parse}
let f = parse("x^3 + 2*x^2 + x")
let df = f.derivative(f)
print(df.to_string(df))
Differentiate with respect to any variable
Differentiates with respect to the named variable, treating all other variables as constants.
use plugin symbolic::{parse}
let f = parse("x^2 + x*y + y^2")
let df_dx = f.partial_derivative(f, "x")
let df_dy = f.partial_derivative(f, "y")
print(df_dx.to_string(df_dx))
Partial derivatives for a list of variables
Returns a table of partial derivatives, one for each variable name in the list. Useful for optimization.
use plugin symbolic::{parse}
let f = parse("x^2 + x*y + y^2")
let grad = f.gradient(f, ["x", "y"])
Symbolic integration with respect to a variable
Symbolically integrates the expression with respect to variable. Supports polynomials, constants, and simple trig (sin/cos of the integration variable directly).
use plugin symbolic::{parse}
let f = parse("3*x^2 + 2*x")
let F = f.integrate(f, "x")
print(F.to_string(F))
Taylor/Maclaurin series approximation
Computes the Taylor series approximation around center to the given number of terms. Use center=0 for a Maclaurin series.
use plugin symbolic::{parse}
let sinx = parse("sin(x)")
let approx = sinx.taylor_series(sinx, "x", 0.0, 5)
print(approx.to_string(approx))
Apply algebraic simplification rules
Applies algebraic simplification rules up to 5 passes: collapses constants, removes zero additions, removes one multiplications, and evaluates constant subexpressions.
use plugin symbolic::{parse}
let expr = parse("x*1 + 0 + x*0")
let simple = expr.simplify(expr)
print(simple.to_string(simple))
Distribute multiplication over addition
Distributes multiplication over addition and subtraction, and expands integer powers (up to 6) into repeated multiplication.
use plugin symbolic::{parse}
let expr = parse("(x+1)^2")
let expanded = expr.expand(expr)
print(expanded.to_string(expanded))
Factor simple expressions (e.g. difference of squares)
Recognizes and factors simple patterns such as a² - b² = (a+b)(a-b). Falls back to simplification for unsupported forms.
use plugin symbolic::{parse}
let expr = parse("x^2 - 4")
let factored = expr.factor(expr)
print(factored.to_string(factored))
Replace a variable with another expression
Replaces all occurrences of variable in the expression tree with replacement_expr.
use plugin symbolic::{parse}
let f = parse("x^2 + x")
let shifted = f.substitute(f, "x", parse("x + 1"))
print(shifted.simplify(shifted).to_string(shifted))
Check if expression references a variable
Returns true if the expression contains a reference to the named variable.
use plugin symbolic::{parse}
let f = parse("x^2 + y")
print(f.contains_var(f, "x"))
print(f.contains_var(f, "z"))
Polynomial degree with respect to a variable
Returns the polynomial degree of the expression with respect to variable. Errors if the expression is not a polynomial.
use plugin symbolic::{parse}
let f = parse("x^3 + 2*x + 1")
print(f.degree(f, "x"))
Polynomial coefficients as a table
Returns the polynomial coefficients as a table indexed from 1 (constant term) upward.
use plugin symbolic::{parse}
let f = parse("2*x^2 + 3*x + 5")
let coeffs = f.coefficients(f, "x")
Check if expression is a polynomial
Returns true if the expression is a polynomial in the given variable (no trig, no non-integer powers).
use plugin symbolic::{parse}
let poly = parse("x^2 + 1")
let trig = parse("sin(x)")
print(poly.is_polynomial(poly, "x"))
print(trig.is_polynomial(trig, "x"))