Expansions

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 ff has nn derivatives at aa, its Taylor polynomial of order nn there is

Tn(x)=∑k=0nf(k)(a)k! (x−a)k. T_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!}\,(x-a)^k .

It is the only polynomial of degree at most nn whose value and first nn derivatives at aa agree with those of ff. The coefficients here are found exactly that way: the expression is differentiated symbolically kk times, the point is substituted, and the result is put in normal form. No rounding is involved, so 17/31517/315 is 17/31517/315.

What the polynomial does not tell you is how close it is to ff away from aa. That is the business of the remainder, f(x)−Tn(x)=f(n+1)(ξ)(n+1)!(x−a)n+1f(x) - T_n(x) = \dfrac{f^{(n+1)}(\xi)}{(n+1)!}(x-a)^{n+1} for some ξ\xi between aa and xx. 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 (x−a)q(x-a)^q, the quotient starts at (x−a)−q(x-a)^{-q} 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 (x−a)−1(x-a)^{-1} is the residue, the number that the integral of the function around the pole depends on.

Where there is no expansion

x\sqrt{x} has no Taylor polynomial at 0 beyond order zero, because its derivative is unbounded there, and log⁡x\log x 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.

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