@ratmath/symbolic

Algebra on expressions, with every step checked

Expand, factor and simplify expressions the way a computer algebra system does, with one difference: the exact and certified libraries sit underneath, so an answer says whether it is proven, refuted or undecided and is never just asserted.

What is here

Expressions, assumptions, rewriting and equality; solving equations and inequalities in one variable, with whole families of solutions for trigonometric equations; systems of equations; antiderivatives, limits, expansions and sums in closed form; and, for every operation, the checked steps that produced the answer.

A first program

import { sym, expand, factor, simplify, equal, rewrite, rules } from "@ratmath/symbolic";

String(expand("(x + 1)^3"));              // "x^3 + 3*x^2 + 3*x + 1"
String(factor("x^4 - 1"));                // "(x + 1)*(x - 1)*(x^2 + 1)"
String(simplify("(x^2 - 1)/(x - 1)"));    // "x + 1"

String(equal("sin(2x)", "2sin(x)cos(x)"));   // equal: a proof
String(equal("sqrt(x^2)", "x"));             // different at x = -2/3: also a proof
String(equal("sqrt(x^2)", "x", { assume: { x: "positive" } }));   // equal
String(equal("atan(1)", "pi/4"));            // undecided, and it says so

Expressions are written in calculator notation: 2x, x^2, sqrt(x), sin(x), log for the natural logarithm, pi and e. Every function takes text, an Expr or the plain tree that analytic and graphics share, so an expression can be simplified here and then plotted or integrated there.

The same habit carries into solving and calculus: an answer comes with how it was checked.

import { solve, solveInequality, integrate, limit, taylor } from "@ratmath/symbolic";

String(solve("sqrt(x + 3) = x - 3"));          // x = 6, with x = 1 rejected: it fails in the original equation
String(solve("2sin(x) = 1"));                  // x = pi/6 + 2*n*pi,  5*pi/6 + 2*n*pi
String(solveInequality("(x - 1)/(x + 2) <= 0"));   // (-2, 1]
String(integrate("x*exp(x)"));                 // x*exp(x) - exp(x) + C   (proven: its derivative is the integrand)
String(limit("(1 + 1/x)^x", "x", "inf").value);    // e
String(taylor("tan(x)", "x", 0, 5).polynomial);    // x + 1/3*x^3 + 2/15*x^5

Systems, sums and step records work the same way.

import { solveSystem, sum, explain } from "@ratmath/symbolic";

String(solveSystem(["a*x + y = 1", "x - y = 0"], ["x", "y"]));   // x = 1/(a + 1),  y = 1/(a + 1), provided a + 1 != 0
String(sum("k^3", "k", 1, "n").value);           // n^2*(n + 1)^2/4, proven by its antidifference
String(sum("1/(k*(k+1))", "k", 1, "inf").value); // 1
String(explain.factor("x^4 - 1"));               // each step, and how it was checked

Four ideas

Equality has three answers. Two expressions are proven equal when they reduce to the same normal form, proven different when a certified evaluation at an exact point separates them, and undecided otherwise. A numerical agreement is never reported as equality. See Equality.

Rules know their domains. x2\sqrt{x^2} is ∣x∣|x|, not xx, and log⁡(ab)=log⁡a+log⁡b\log(ab) = \log a + \log b needs aa and bb positive. A rule is applied only when its conditions are proven from the assumptions you state; otherwise it is left alone and reported, or applied with the conditions handed back. See Rules and Assumptions.

Answers are checked before they are returned. A solution is substituted into the original equation, and an extraneous root is reported as rejected. An antiderivative is differentiated and compared with the integrand. A limit is compared with a certified enclosure. See Solving, Antiderivatives and Limits.

Rewrites are checked. Each application of a rule is compared at exact test points with certified arithmetic, so a rule that is simply wrong is caught the first time it is used.

Phases

All five phases from the library roadmap. are built.

PhaseContentsStatus
1Expressions, assumptions, rewriting, three-way equalitybuilt
2Solving equations and inequalities in one variablebuilt
3Antiderivatives with verification; limits; expansionsbuilt
4Systems, trigonometric families, sumsbuilt
5Step records for every operationbuilt

It builds on Core, analytic (the expression tree, derivatives, enclosures), algebraic (factoring) and structures (polynomials in several variables).

Try itEdit the code and run it with @ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs