Taylor and Laurent expansions
taylor gives the polynomial that matches a function and its derivatives at a point, with every coefficient exact. At a pole it gives the expansion with negative powers.
Using it
import { taylor } from "@ratmath/symbolic";
const t = taylor("exp(x)", "x", 0, 4); // expression, variable, point, highest power
String(t.polynomial); // 1 + x + 1/2*x^2 + 1/6*x^3 + 1/24*x^4
t.coefficients; // [1, 1, 1/2, 1/6, 1/24] as expressions
t.lowest; // 0: the power the first coefficient belongs to
String(taylor("tan(x)", "x", 0, 7).polynomial); // x + 1/3*x^3 + 2/15*x^5 + 17/315*x^7
String(taylor("log(x)", "x", 1, 3).polynomial); // x - 1 - 1/2*(x - 1)^2 + 1/3*(x - 1)^3
String(taylor("sqrt(1 + x)", "x", 0, 3).polynomial); // 1 + 1/2*x - 1/8*x^2 + 1/16*x^3
String(taylor("exp(a*x)", "x", 0, 2).polynomial); // 1 + a*x + a^2/2*x^2
The Taylor polynomial
If has derivatives at , its Taylor polynomial of order there is
It is the only polynomial of degree at most whose value and first derivatives at agree with those of . The coefficients here are found exactly that way: the expression is differentiated symbolically times, the point is substituted, and the result is put in normal form. No rounding is involved, so is .
What the polynomial does not tell you is how close it is to away from . That is the business of the remainder, for some between and . For a certified bound on a function over an interval, use the Taylor models in analytic, which carry the remainder with them.
At a pole
Where the function is a quotient whose denominator vanishes at the point, the two expansions are divided as power series. If the denominator starts at , the quotient starts at or later; that is a Laurent expansion, and lowest is negative.
const l = taylor("1/(x*(1 - x))", "x", 0, 2);
String(l.polynomial); // x^-1 + 1 + x + x^2
l.lowest; // -1
String(taylor("1/sin(x)", "x", 0, 3).polynomial); // x^-1 + 1/6*x + 7/360*x^3
The coefficient of is the residue, the number that the integral of the function around the pole depends on.
Where there is no expansion
has no Taylor polynomial at 0 beyond order zero, because its derivative is unbounded there, and has none at all. taylor refuses in those cases and says which point is the trouble.
taylor("sqrt(x)", "x", 0, 2);
// refuses: sqrt(x) cannot be expanded at x = 0: a derivative is undefined there
Expansions are also the engine of limit: the leading terms of numerator and denominator decide a limit of the form 0/0.
@ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.