Limits and improper integrals
A limit at infinity cannot be found by plugging in a large number, and an improper integral cannot be found by integrating a long way out. Both need a statement about all large x. The library gets one from a small calculus of growth rates.
fn("(2x^2+1)/(x^2-3)").limit("inf"); // exactly 2
fn("(1+1/x)^x").limit("inf"); // e, to 20 digits
fn("1/x").limit(0, { side: "right" }); // Infinity
fn("exp(-x^2)").improperIntegral("-inf", "inf").exact(); // √π
fn("1/x").improperIntegral(1, "inf").exact(); // Infinity: proven to diverge
fn("1/sqrt(x)").improperIntegral(0, 1).exact(); // 2
The growth calculus
On a ray [X, ∞) every subexpression is written as
with exact rational exponents, a constant ℓ known as an interval, and a remainder ρ whose bound decays. Sums, products, quotients, powers and the elementary functions keep this form. In a sum the term of larger order leads and the other joins the remainder; exp of a growing negative argument is bounded by an exponential; sin of something small is that thing times 1 + ρ. Where no constant leads, as in sin x, the node only carries a range. Rational parts are expanded exactly first, so leading terms that cancel are cancelled.
fn(...).asymptotic() shows the result: fn("(3x^2+1)/(x-1)").asymptotic() has term: "x" and coefficient 3.
Limits
If the term decays, the limit is 0. If the term is constant and ℓ is a true constant, the limit is ℓ, exactly: no large X is ever substituted. If the term grows and the sign of ℓ + ρ is settled, the limit is ±∞. If only a range is known and it does not decay, the answer is an AnalyticError with code no-limit. That covers sin x, but also cases this calculus cannot settle, such as √(x² + x) − x, where the leading terms cancel inside a square root.
A one-sided limit at a point c is the limit at infinity of f(c ± 1/t). limit(c) without a side first tries the removable case (sin(x)/x at 0), and otherwise compares the two sides.
Improper integrals
The ray is cut at some X. Up to X the integral is ordinary, done on doubling pieces [1, 2], [2, 4], … by Taylor series in fixed point. Beyond X the growth form decides:
- Converges, with a closed form for the tail. For x^p, e^(kx), x^n·e^(kx) and x^p·log(x)^n the tail is written down exactly, times ℓ, plus a bound for the remainder. So ∫₁^∞ dx/x² needs no large X at all.
- Converges, with a bound. Otherwise the tail is bounded by an integrable envelope, and X is raised until the bound is small. For an oscillating integrand like sin(x)/x² this is slow, and the answer may be wider than asked; the
vialine says so. - Diverges. If the integrand keeps one sign and is at least a constant times a term that is not integrable (x^p with p ≥ −1, anything growing), the exact value is
Infinityor-Infinity. This is the p-test, with proof. - Undecided. For sin(x)/x, which converges only by cancellation, the calculus has no answer and refuses with code
undecided.
An end where f is undefined is moved to infinity by x = c ± 1/t, so 1/√x on (0, 1] becomes t^(−3/2) on [1, ∞). A singular point strictly inside the interval must be named: improperIntegral(-1, 1, { singular: [0] }). Both ends of the real line are handled separately, so ∫ x dx over the whole line is reported as divergent (the value is NaN: the two ends diverge with opposite signs) rather than as the principal value 0.
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.