Rules that know their domains
Most errors in hand algebra come from using an identity outside the range where it holds. Here a rule carries its conditions, the conditions are proven before the rule is used, and every use is checked.
A rule
A rule is a pattern, a replacement, and the conditions under which the two are equal. Pattern variables end in an underscore and stand for any subexpression.
import { rule, rewrite, rules } from "@ratmath/symbolic";
const logProduct = rule("log(a_*b_)", "log(a_) + log(b_)", {
when: ["a_ > 0", "b_ > 0"],
name: "log of a product",
});
The condition matters. With the left side is and the right side is undefined; a system that applies the rule regardless will sooner or later prove something false.
Applying rules
// proven conditions: the rule is used
const r = rewrite("log(x^2*y)", rules.logs.expand, { assume: { x: "positive", y: "positive" } });
String(r.expr); // "2*log(x) + log(y)"
r.steps.map((s) => s.rule); // ["log of a product", "log of a power"]
// undecided conditions: the rule is not used, and the result says why
const b = rewrite("log(x*y)", rules.logs.expand);
String(b.expr); // "log(x*y)"
b.blocked[0].needs; // ["x > 0", "y > 0"]
// or ask for it anyway, and receive the conditions with the result
const c = rewrite("log(x*y)", rules.logs.expand, { conditional: true });
String(c.expr); // "log(x) + log(y)"
c.conditions; // ["x > 0", "y > 0"]
A condition that is proven false simply stops the rule. Rules are tried innermost first and repeatedly until none applies (or maxSteps, 200 by default, is reached). + and * match in either order.
Every use is checked
After a rule is applied, the two sides are evaluated at several exact rational points that satisfy the assumptions, with certified interval arithmetic. If the enclosures of the two sides are disjoint at any point, the rule is not an identity and rewrite throws, naming the point. Each step records how many points agreed in checkedAt.
rewrite("sqrt(x^2)", rule("sqrt(a_^2)", "a_"));
// SymbolicError: the rule "sqrt(a_^2) -> a_" is not an identity:
// sqrt(x^2) and x differ at x = -2/3
Agreement at test points is evidence and not a proof, and the library does not present it as one: it is a guard against wrong rules. Proofs of equality come from the normal form.
The rule sets that ship
| Set | Contents |
|---|---|
rules.powers | ; for ; for ; for ; for ; products and powers of exponentials |
rules.logs.expand | , , , taken apart for positive arguments; ; for |
rules.logs.combine | put together (valid wherever the left side is defined) |
rules.trig.expand | sums, differences, double and triple angles; |
rules.trig.reduce | power reduction: and its relatives |
rules.hyperbolic | , , through the exponential |
A rule set is a rule, a list, or an object of lists, nested as deep as you like; pass several as an array. A where function on a rule gives a test on the bindings that a condition cannot express.
@ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.