Topic

Sequences

A sequence is a formula in a whole number n, or a rule that makes each term from the one before. sequence and recursive give terms exactly, limits with a proof, and statements such as "decreasing from n = 3 on" that hold for every later term and not only the ones looked at.

const a = sequence("(1 + 1/n)^n");
a.terms(3);            // [2, 9/4, 64/27]   exact
a.limit();             // an interval of width 10^-20 around e
a.bounds();            // { lower, upper, bounded: true }: every term lies between them

const b = sequence("n/(n^2 + 10)");
b.monotone();          // { direction: "decreasing", from: 3, proven: "for every n ≥ 3" }
sequence("n^2/(n + 1)").limit();   // Infinity
sequence("2^n/n!").limit();        // 0, by the ratio of successive terms

Factorials are written n! or (2n)!. The index is n unless you pass { variable }, and starts at 1 unless you pass { from }.

What a limit means here

A sequence ana_n has limit LL when for every ε>0\varepsilon > 0 there is an NN with ∣an−L∣<ε|a_n - L| < \varepsilon for all n≥Nn \ge N. Computing many terms can suggest a limit and cannot prove one: the definition is about all the terms that were not computed.

The library proves limits from the formula. On a ray x≥Xx \ge X it writes the term as

a(x)=ekx xp (log⁡x)m (ℓ+ρ(x)),∣ρ(x)∣≤a bound that decreases to 0, a(x) = e^{kx}\,x^{p}\,(\log x)^{m}\,\bigl(\ell + \rho(x)\bigr), \qquad |\rho(x)| \le \text{a bound that decreases to } 0,

with exact exponents and an interval for ℓ\ell. The limit, or the fact that the sequence grows without bound, is read off the exponents, and the bound on ρ\rho holds for every x≥Xx \ge X at once. This is the same machinery as limits at infinity. Where it finds no such form, as for sin⁡n\sin n, limit refuses.

Monotone and bounded

A sequence that is monotone and bounded converges; that theorem is how many limits are shown to exist before they are found. monotone() proves the direction by showing that the derivative of the formula has one sign on a ray [N,∞)[N, \infty), then compares the terms before NN one by one and moves NN back as far as they agree. bounds() combines a bound valid on a ray with the finitely many terms before it.

If no proof is found the call refuses. For example the derivative of (1+1/n)n(1 + 1/n)^n is a difference of two terms that nearly cancel, and its sign is not settled by this method, although its limit and bounds are.

Two further routes cover cases where the derivative does not settle it. If the derivative HH tends to 0 and its derivative has one sign on a ray, then HH has the opposite sign there (a function that decreases to 0 is positive); this is applied up to four levels deep. And for a positive term such as (1+1/n)n(1 + 1/n)^n the logarithm is differentiated instead, which turns the power into a product. monotone() reports the route in reason.

Powers with a moving exponent, and the ratio test

A term f(n)g(n)f(n)^{g(n)} is rewritten as exp⁡(glog⁡f)\exp(g\log f), and log⁡f\log f for a ratio of polynomials tending to 1 is handled through the exact difference f−1f - 1, so that nothing cancels in floating point. That proves (1+1/n)n→e(1 + 1/n)^n \to e and limits such as nn/(n! 3n)→0n^n/(n!\,3^n) \to 0, where the ratio of successive terms tends to e/3<1e/3 < 1.

Oscillation: lim sup and lim inf

A factor such as (−1)n(-1)^n, cos⁡(πn)\cos(\pi n) or sin⁡(πn/2)\sin(\pi n/2) repeats with a whole period pp. Along n=pm+rn = pm + r it is a known constant, so each of the pp subsequences is an ordinary formula in mm with a limit. Those limits are the only accumulation points; the largest is lim sup⁡an\limsup a_n and the smallest lim inf⁡an\liminf a_n, and the sequence converges exactly when they agree.

const c = sequence("(-1)^n*(1 + 1/n)");
c.accumulation();   // { period: 2, subsequences: [{ along: "n = 2m", limit: 1 }, { along: "n = 2m + 1", limit: -1 }], … }
c.limsup();         // 1
c.liminf();         // -1
c.limit();          // refuses: "no limit: the terms oscillate, with lim sup 1 and lim inf -1 …"
c.supremum();       // { value: 3/2, attained: true, at: 2, reason }
sequence("n/(n + 1)").supremum();   // { value: 1, attained: false, … }: approached, never reached

supremum() and infimum() use the proven direction of each subsequence: a tail that rises has its limit as supremum, not attained; a tail that falls has its first term. The finitely many terms before the tail are compared one by one. For sin⁡n\sin n, which does not repeat on the whole numbers, all of these refuse and say so.

For the NN that goes with an ε\varepsilon, see ε, N and δ.

Recursive sequences

const heron = recursive("x/2 + 1/x", { start: 2 });   // x₀ = 2, x_{k+1} = x_k/2 + 1/x_k
heron.terms(5);              // [2, 3/2, 17/12, 577/408, 665857/470832]
heron.fixedPoints([1, 2]);   // [{ point: [1.41421…], multiplier: [≈ 0], kind: "attracting", unique: true }]
heron.limit([1, 2]);         // { limit: [1.41421…], enters: 1, rate: 0.696…, basin: [0.914…, 1.914…] }

If xk+1=g(xk)x_{k+1} = g(x_k) converges and gg is continuous, the limit is a fixed point, g(x∗)=x∗g(x^*) = x^*. The multiplier g′(x∗)g'(x^*) says what happens nearby: errors are multiplied by about g′(x∗)g'(x^*) each step, so the fixed point attracts when ∣g′(x∗)∣<1|g'(x^*)| < 1 and repels when ∣g′(x∗)∣>1|g'(x^*)| > 1.

limit turns that into a proof by the contraction principle. It finds an interval II around a fixed point such that g(I)⊂Ig(I) \subset I and ∣g′∣≤q<1|g'| \le q < 1 on II; then gg has exactly one fixed point in II and every sequence that enters II converges to it, with ∣xk+1−x∗∣≤q ∣xk−x∗∣|x_{k+1} - x^*| \le q\,|x_k - x^*|. It then follows the sequence with interval arithmetic until a term is proven to lie in II. The answer reports the step at which that happened and the rate qq.

Terms are exact fractions while they stay short (Heron's iteration doubles the number of digits each step) and certified intervals after that.

callreturns
sequence(term, { variable = "n", from = 1, params })Sequence
a.term(n), a.terms(count)Rational where the formula is rational at n, else RationalInterval
a.limit({ digits = 20 })RationalInterval, or ±Infinity; refuses with no-limit or undecided
a.monotone(){ direction, from, proven, reason }
a.accumulation(){ period, subsequences: [{ along, term, limit }], limsup, liminf, converges }
a.limsup(), a.liminf()RationalInterval or ±Infinity
a.supremum(), a.infimum(){ value, attained, at, reason }
a.nFor(ε){ N, least, reason, certificate }: see ε, N and δ
a.bounds(){ lower, upper, bounded }; null for a bound that does not exist
a.series()the series Σ a(n)
recursive(next, { start, variable = "x" })RecursiveSequence
r.terms(count, { maxDigits = 300 })Rationals, then RationalIntervals
r.fixedPoints([a, b])records { point, multiplier, kind, unique }
r.limit([a, b], { maxSteps = 400 }){ limit, enters, rate, basin }; refuses when no contraction is proven
Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs