Complex analysis

Real integrals by residues

Some integrals over the real line are easiest in the complex plane: close the path with a large semicircle, show the semicircle contributes nothing, and add up residues. Two families are done here from start to finish, with each step of the argument written out and the value proved.

import { realLineIntegral, trigIntegral } from "@ratmath/complex";

const r = realLineIntegral("1/(x^2 + 1)^2");
r.real;                    // an interval around π/2 = 1.5707963267948966, 2.7e-20 wide
r.steps;                   // the argument, as { title, detail }

realLineIntegral("x/(x^2 + 1)", { fourier: 2 }).sin;   // ∫ x sin 2x/(x² + 1) dx = π/e²

trigIntegral("1/(2 + cos(t))").real;                   // 2π/√3 = 3.62759872847

Rational functions over the whole line

Let f=P/Qf = P/Q with deg⁡Q≥deg⁡P+2\deg Q \ge \deg P + 2 and no real root of QQ. Integrate round the segment [−R,R][-R, R] and the semicircle above it. On the semicircle ∣f(z)∣≤C/R2|f(z)| \le C/R^2, so that part is at most πC/R\pi C/R and vanishes as RR grows. What is left is

∫−∞∞f(x) dx=2πi∑Im⁡zk>0Res⁡zkf. \int_{-\infty}^{\infty} f(x)\,dx = 2\pi i \sum_{\operatorname{Im} z_k > 0} \operatorname{Res}_{z_k} f.

The steps the function takes:

  1. The formula is turned into exact polynomials PP and QQ and common factors are cancelled, so a removable singularity on the real axis does no harm.
  2. The degrees are compared; if the integral does not converge the call is refused and says why.
  3. Every root of QQ is found by zeros in a square that Cauchy's bound proves large enough, and the count is checked against the degree.
  4. Each root must be provably above or below the real axis. One that is on it, or too close to tell, is refused: the integral does not exist as it stands.
  5. At a simple pole the residue is P/Q′P/Q', evaluated on the root's enclosure. At a multiple pole it is a contour integral round a small circle.

Fourier integrals

With { fourier: a } the integrand is f(x)eiaxf(x)e^{iax}. For a>0a > 0 the factor eiaze^{iaz} decays in the upper half-plane, and Jordan's lemma makes the semicircle vanish as soon as deg⁡Q≥deg⁡P+1\deg Q \ge \deg P + 1. For a<0a < 0 the path closes below instead. When ff is real on the real axis the real and imaginary parts of the result are the cosine and sine integrals, returned as cos and sin.

Trigonometric integrals over a period

For a rational function RR of cos⁡t\cos t and sin⁡t\sin t, put z=eitz = e^{it}. Then cos⁡t=(z+1/z)/2\cos t = (z + 1/z)/2, sin⁡t=(z−1/z)/(2i)\sin t = (z - 1/z)/(2i), dt=dz/(iz)dt = dz/(iz), and as tt runs from 0 to 2π2\pi the point zz runs once round the unit circle:

∫02πR(cos⁡t,sin⁡t) dt=∮∣z∣=1g(z) dz=2πi∑∣zk∣<1Res⁡zkg. \int_0^{2\pi} R(\cos t, \sin t)\,dt = \oint_{|z|=1} g(z)\,dz = 2\pi i \sum_{|z_k| < 1} \operatorname{Res}_{z_k} g.

trigIntegral makes the substitution, reduces gg to a quotient of polynomials, and sums the residues at the poles proved to be inside the circle. cos(2t) and the like are understood. A pole on the circle means the integrand is unbounded, and is refused.

callreturns
realLineIntegral(f, { fourier, bits, variable }){ value, real, cos, sin, poles, steps }. value is a Rect; real the interval for a real integrand; cos and sin with fourier.
trigIntegral(f, { bits, variable }){ value, real, poles, steps }
polesEvery pole: { enclosure, order, taken, residue }
stepsThe argument: [{ title, detail }]

The formula's variable is x (or z) for the line and t (or theta, or x) for the circle; name another with variable. Coefficients must be exact, which here means rational numbers and i.

Try itEdit the code and run it with @ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs