Normal forms: expand, factor, collect
To decide whether two expressions are the same, bring both to a standard shape and compare. For polynomials and their fractions the standard shape is well known. This page describes how the library extends it to roots, exponentials, logarithms and trigonometric functions.
The operations
import { expand, factor, collect, apart, normal, simplify } from "@ratmath/symbolic";
String(expand("(x + 1)^3")); // "x^3 + 3*x^2 + 3*x + 1"
String(expand("(x + 1/x)^2")); // "(x^4 + 2*x^2 + 1)/x^2" one fraction, cancelled
String(factor("x^4 - 1")); // "(x + 1)*(x - 1)*(x^2 + 1)"
String(factor("(x^2 - y^2)/(2x + 2y)")); // "(x - y)/2"
String(collect("(a*x + b)^2 + x", "x")); // "a^2*x^2 + (2*a*b + 1)*x + b^2"
String(apart("(3x + 5)/(x^2 - 1)")); // "4/(x - 1) - 1/(x + 1)" partial fractions
String(simplify("(exp(2x) - 1)/(exp(x) - 1)")); // "exp(x) + 1"
expand (also exported as together and cancel) gives the normal form itself. factor factors its numerator and denominator over the rationals. simplify returns the smallest of the normal form, the factored form, and the normal form after the rules your assumptions allow.
Rational functions of kernels
A rational expression uses only numbers, variables and . Every rational expression equals a fraction of polynomials with no common factor, and that fraction is unique once the leading coefficient of is fixed. So two rational expressions are equal exactly when their reduced fractions are the same. The arithmetic is done by structures.
Other expressions contain pieces that are not rational operations: , , , , , . The library calls these kernels and treats each as one more variable. The expression is the polynomial in the kernel , and factors as .
Kernels are not independent, though, and the normal form uses the relations between them that are known exactly:
| Kernel | Relation used | Example |
|---|---|---|
sqrt(2)^2 is 2; 1/sqrt(2) is sqrt(2)/2 | ||
| roots of numbers | split into roots of primes | sqrt(8) + sqrt(2) is 3*sqrt(2); sqrt(2)*sqrt(3) - sqrt(6) is 0 |
| , and | abs(x)^2 is x^2 | |
| ; ; exact values at multiples of | sin(x)^2 + cos(x)^2 is 1 | |
| , | exp(2x)/exp(x) is exp(x) | |
| ; logs of numbers split over primes | log(8) - 3log(2) is 0 |
Each relation holds wherever the expressions are defined, so the conclusion is: if two expressions have the same normal form, they are equal wherever both are defined. The converse is not claimed. and are equal, but no relation above says so, and their normal forms differ.
Perfect powers under a root
is , and is . The library finds these by a square-free decomposition of what is under the root, and resolves the absolute value when the assumptions decide the sign.
String(expand("sqrt(x^2 + 2x + 1)")); // "abs(x + 1)"
String(expand("sqrt(x^2 + 2x + 1)", { assume: { x: "> -1" } })); // "x + 1"
String(expand("sqrt(x^2)", { assume: { x: "negative" } })); // "-x"
What was cancelled
and agree everywhere except at , where the first is undefined. Cancelling is still the right thing to do, but it should not be silent. normal returns the form together with what was divided out:
const n = normal("(x^2 - 1)/(x - 1) + 1/y");
String(n.expr); // "(x*y + y + 1)/y"
n.cancelled.map(String); // ["x - 1"] the result equals the input only where x − 1 ≠ 0
n.kernels.map(String); // [] (no kernels here)
Things with no real value are refused instead of being rewritten: sqrt(-4), log(-1), 1/(x - x) and 0^0 all throw a SymbolicError.
@ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.