Equal, different, or undecided
Asking whether two expressions are equal has no general algorithm. The honest reply has three values, and two of them come with a proof.
The three answers
import { equal } from "@ratmath/symbolic";
const a = equal("(x + 1)^2", "x^2 + 2x + 1");
a.verdict; // "equal"
a.method; // "normal form"
const b = equal("sqrt(x^2)", "x");
b.verdict; // "different"
b.witness; // { point: { x: -2/3 }, left: [0.666…], right: [-0.666…] } exact point, certified enclosures
const c = equal("atan(1)", "pi/4");
c.verdict; // "undecided"
c.agreedAt; // 1 test point where the enclosures overlapped, which proves nothing
c.value; // null (true / false / null for the three verdicts)
Equal is a proof by normal form: the difference of the two expressions reduces to zero using only relations that hold wherever the expressions are defined. It means equal wherever both sides are defined, under the assumptions given.
Different is a proof by witness: at an exact rational point that satisfies the assumptions, certified interval evaluation of the two sides gives intervals that do not meet. For expressions without variables the precision is raised (to about 4,000 bits) before giving up, so against , or against a decimal, is settled quickly.
Undecided means neither. The normal form does not know the identity and no test point separated the two sides. The library reports how many points agreed, and does not turn that into a claim.
Why not just compare numerically?
Because agreement to many digits is not equality. The expressions
agree to twelve decimal places after the point and are different numbers. With certified arithmetic the difference is found once enough precision is used, and it is a proof. In the other direction, no amount of numerical agreement can prove an identity, so the library does not try.
There is also a theorem behind the third answer. Richardson showed in 1968 that for expressions built from the rationals, , , a variable, , and , no algorithm decides whether an expression is identically zero. A system that always answers yes or no must sometimes be wrong.
Assumptions change the answer
String(equal("log(a*b)", "log(a) + log(b)"));
// "undecided …": they agree wherever both are defined, but the rule is not valid in general
String(equal("log(a*b)", "log(a) + log(b)", { assume: { a: "positive", b: "positive" } }));
// "equal (normal form, after the rules the assumptions allow)"
String(equal("(a^b)^c", "a^(b*c)", { assume: { a: "positive" } })); // equal
With assumptions, equal first applies the rules whose conditions it can prove (powers, logarithms, angle sums) and then compares normal forms. For hyperbolic functions it also tries writing them through the exponential, which is how is proven.
Simplifying
simplify uses the same machinery and returns an expression that is proven equal to its input wherever the input is defined: the smallest of the normal form, the factored form, and the normal form after the provable rules.
String(simplify("(x^2 - 1)/(x - 1)")); // "x + 1"
String(simplify("sqrt(a^2*b)", { assume: { a: "positive", b: "positive" } })); // "a*sqrt(b)"
String(simplify("sin(x)^4 - cos(x)^4")); // "1 - 2*cos(x)^2"@ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.