complexanalysis

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package complexanalysis provides numerical tools for complex analysis using only the Go standard library.

It is organised into a few areas:

Every function that samples an analytic function does so by evaluating a user-supplied Function. Contour methods use the composite trapezoidal rule on a circular contour, which converges geometrically for functions that are analytic in an annulus, so modest sample counts (a few hundred points) give near machine-precision results.

The package is deterministic and depends only on math and math/cmplx.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(z complex128) float64

Abs returns the modulus (absolute value) |z|.

func Acos

func Acos(z complex128) complex128

Acos returns the principal inverse cosine of z.

func Acosh

func Acosh(z complex128) complex128

Acosh returns the principal inverse hyperbolic cosine of z.

func AnalyticContinuation

func AnalyticContinuation(f Function, from, to complex128, order int, radius float64, n int) complex128

AnalyticContinuation returns the value of f at the point to, reconstructed from its Taylor expansion about the point from. It computes the Taylor coefficients up to the given order on a circle of the given radius about from, then sums the truncated series at to. The step |to-from| should be smaller than the radius of convergence for the series to be accurate.

func AnalyticContinuationPath

func AnalyticContinuationPath(f Function, path []complex128, order int, radius float64, n int) complex128

AnalyticContinuationPath continues f numerically along the given path of centers and returns the reconstructed value at the final point. Starting from path[0], it repeatedly forms the Taylor expansion of f about the current center (using a circle of the given radius and n points, to the given order) and evaluates it at the next path point, which becomes the new center. This mirrors classical analytic continuation along a chain of overlapping disks. It returns 0 for an empty path and f(path[0]) for a single point.

func ApproxEqual

func ApproxEqual(a, b complex128, tol float64) bool

ApproxEqual reports whether |a-b| <= tol.

func Arg

func Arg(z complex128) float64

Arg returns the principal argument of z in the interval (-pi, pi].

func ArgumentPrinciple

func ArgumentPrinciple(f Function, center complex128, radius float64, n int) complex128

ArgumentPrinciple returns the value of 1/(2*pi*i) * integral of f'(z)/f(z) around the positively oriented circle of the given center and radius. By the argument principle this equals the number of zeros minus the number of poles of f inside the circle, counted with multiplicity. The derivative f' is approximated by central finite differences. The exact count is returned by CountZeros after rounding.

func Asin

func Asin(z complex128) complex128

Asin returns the principal inverse sine of z.

func Asinh

func Asinh(z complex128) complex128

Asinh returns the principal inverse hyperbolic sine of z.

func Atan

func Atan(z complex128) complex128

Atan returns the principal inverse tangent of z.

func Atanh

func Atanh(z complex128) complex128

Atanh returns the principal inverse hyperbolic tangent of z.

func Beta

func Beta(a, b complex128) complex128

Beta returns the Euler beta function B(a, b) = Gamma(a)Gamma(b)/Gamma(a+b), evaluated through LogGamma to remain stable for moderately large arguments.

func Binomial

func Binomial(z complex128, k int) complex128

Binomial returns the generalized binomial coefficient C(z, k) = Gamma(z+1)/(Gamma(k+1)Gamma(z-k+1)) for a non-negative integer k, evaluated as a falling-factorial product so it is exact for polynomial z.

func CauchyDerivative

func CauchyDerivative(f Function, z0, center complex128, k int, radius float64, n int) complex128

CauchyDerivative evaluates the k-th derivative of f at z0 using the Cauchy integral formula for derivatives, f^(k)(z0) = k!/(2*pi*i) * integral of f(z)/(z-z0)^(k+1) around the circle of the given center and radius. The point z0 must lie strictly inside the circle and f must be analytic there. k == 0 returns f(z0). It panics if k < 0.

func CauchyIntegralValue

func CauchyIntegralValue(f Function, z0, center complex128, radius float64, n int) complex128

CauchyIntegralValue evaluates f at the point z0 inside a circle of the given center and radius using the Cauchy integral formula, f(z0) = 1/(2*pi*i) * integral of f(z)/(z-z0) around the circle. The point z0 must lie strictly inside the circle and f must be analytic there.

func CayleyTransform

func CayleyTransform(z complex128) complex128

CayleyTransform returns the value of the Cayley transform (z-i)/(z+i), which maps the upper half-plane conformally onto the open unit disk.

func Cbrt

func Cbrt(z complex128) complex128

Cbrt returns the principal cube root of z, exp(Log(z)/3), which is real and positive for real positive z.

func Conj

func Conj(z complex128) complex128

Conj returns the complex conjugate of z.

func ContourIntegral

func ContourIntegral(f Function, gamma func(float64) complex128, n int) complex128

ContourIntegral approximates the integral of f along a general contour given by the parametrization gamma over t in [0, 1]. The derivative gamma'(t) is estimated by central finite differences, and the composite trapezoidal rule with n subintervals is used. If the contour is closed (gamma(0) == gamma(1)), the periodic trapezoidal rule is highly accurate. It panics if n <= 0.

func Cos

func Cos(z complex128) complex128

Cos returns the cosine of z.

func Cosh

func Cosh(z complex128) complex128

Cosh returns the hyperbolic cosine of z.

func Cot

func Cot(z complex128) complex128

Cot returns the cotangent of z, cos(z)/sin(z).

func Coth

func Coth(z complex128) complex128

Coth returns the hyperbolic cotangent of z, cosh(z)/sinh(z).

func CountZeros

func CountZeros(f Function, center complex128, radius float64, n int) int

CountZeros returns the number of zeros of an analytic function f inside the circle of the given center and radius, counted with multiplicity, by rounding the argument-principle integral. It assumes f has no poles inside the circle and no zeros on it.

func CrossRatio

func CrossRatio(z, z1, z2, z3 complex128) complex128

CrossRatio returns the cross-ratio (z-z1)(z2-z3) / ((z-z3)(z2-z1)) of the four points. The cross-ratio is invariant under every Mobius transformation.

func Csc

func Csc(z complex128) complex128

Csc returns the cosecant of z, 1/sin(z).

func Digamma

func Digamma(z complex128) complex128

Digamma returns the logarithmic derivative of the gamma function, psi(z) = Gamma'(z)/Gamma(z), using recurrence to raise the real part and then the standard asymptotic expansion.

func Erf

func Erf(z complex128) complex128

Erf returns the error function of z, 2/sqrt(pi) times the integral of exp(-t^2) from 0 to z, summed from its Taylor series. The series converges for every z but loses precision for large |z| (roughly |z| > 6); within that range it is accurate to about 1e-12.

func Erfc

func Erfc(z complex128) complex128

Erfc returns the complementary error function 1 - Erf(z).

func Exp

func Exp(z complex128) complex128

Exp returns e**z.

func Factorial

func Factorial(z complex128) complex128

Factorial returns z! defined as Gamma(z+1); for a non-negative integer z it equals the ordinary factorial.

func Gamma

func Gamma(z complex128) complex128

Gamma returns the value of the Euler gamma function at z, computed with the Lanczos approximation and the reflection formula for Re(z) < 0.5. It is accurate to roughly 1e-13 relative error away from the poles at the non-positive integers.

func Im

func Im(z complex128) float64

Im returns the imaginary part of z.

func IntegrateCircle

func IntegrateCircle(f Function, center complex128, radius float64, n int) complex128

IntegrateCircle approximates the contour integral of f around the positively oriented circle of the given center and radius, using the composite trapezoidal rule with n sample points. Because the integrand is periodic, this rule converges geometrically when f is analytic in an annulus around the circle. It panics if n <= 0 or radius <= 0.

func IntegratePolygon

func IntegratePolygon(f Function, vertices []complex128, nPerEdge int) complex128

IntegratePolygon approximates the integral of f along the closed polygonal contour through the given vertices (the last vertex is joined back to the first), applying IntegrateSegment with nPerEdge points on each edge. It returns 0 for fewer than two vertices.

func IntegrateSegment

func IntegrateSegment(f Function, a, b complex128, n int) complex128

IntegrateSegment approximates the integral of f along the straight line segment from a to b using the composite Simpson rule with n subintervals (n is rounded up to the next even number). It panics if n <= 0.

func InverseCayleyTransform

func InverseCayleyTransform(w complex128) complex128

InverseCayleyTransform returns i*(1+w)/(1-w), the inverse of the Cayley transform, mapping the unit disk back to the upper half-plane.

func IsZero

func IsZero(z complex128, tol float64) bool

IsZero reports whether |z| <= tol.

func JoukowskiMap

func JoukowskiMap(z complex128) complex128

JoukowskiMap returns the Joukowski map (z + 1/z)/2, which maps circles about the origin to confocal ellipses and is used in classical airfoil theory. It returns a complex infinity at z = 0.

func LaurentCoefficient

func LaurentCoefficient(f Function, center complex128, k int, radius float64, n int) complex128

LaurentCoefficient returns the k-th Laurent coefficient c_k of f expanded about center, computed from the contour integral c_k = 1/(2*pi*i) * integral of f(z)/(z-center)^(k+1) around the circle of the given radius with n sample points. The index k may be negative; c_{-1} is the residue. The circle must lie in an annulus of analyticity of f.

func LaurentCoefficients

func LaurentCoefficients(f Function, center complex128, lo, hi int, radius float64, n int) []complex128

LaurentCoefficients returns the Laurent coefficients c_k of f about center for k running from lo to hi inclusive, as a slice indexed so that result[k-lo] is c_k. It returns nil when hi < lo.

func Log

func Log(z complex128) complex128

Log returns the principal branch of the natural logarithm of z, with imaginary part (the argument) in (-pi, pi].

func LogBranch

func LogBranch(z complex128, k int) complex128

LogBranch returns the value of the natural logarithm of z on branch k, that is Log(z) + 2*pi*i*k. Branch 0 is the principal branch.

func LogGamma

func LogGamma(z complex128) complex128

LogGamma returns the principal branch of the logarithm of the gamma function at z. For Re(z) >= 0.5 it uses the Lanczos series directly, avoiding the overflow that Gamma would suffer for large arguments; elsewhere it falls back to Log(Gamma(z)).

func NthRoots

func NthRoots(z complex128, n int) []complex128

NthRoots returns all n distinct n-th roots of z, ordered by increasing argument starting from the principal root. It returns nil for n <= 0.

func Polar

func Polar(r, theta float64) complex128

Polar constructs a complex number from a modulus r and an argument theta, i.e. r*(cos(theta) + i*sin(theta)).

func Pow

func Pow(z, w complex128) complex128

Pow returns z raised to the power w using the principal branch, exp(w*Log(z)). Pow(0, 0) is defined to be 1.

func PowerSeriesEval

func PowerSeriesEval(coeffs []complex128, center, z complex128) complex128

PowerSeriesEval evaluates the power series with the given coefficients about center at the point z, returning sum_j coeffs[j] * (z-center)^j via Horner's method.

func Re

func Re(z complex128) float64

Re returns the real part of z.

func Reciprocal

func Reciprocal(z complex128) complex128

Reciprocal returns 1/z.

func Rect

func Rect(re, im float64) complex128

Rect constructs a complex number from its real and imaginary parts.

func Residue

func Residue(f Function, z0 complex128, radius float64, n int) complex128

Residue approximates the residue of f at the isolated singularity z0 by integrating f around a small circle of the given radius and dividing by 2*pi*i. The radius must enclose no singularity other than z0.

func ResidueOrderM

func ResidueOrderM(f Function, z0 complex128, m int, radius float64, n int) complex128

ResidueOrderM returns the residue of f at a pole z0 of order m using the derivative formula Res = lim_{z->z0} 1/(m-1)! d^{m-1}/dz^{m-1} [(z-z0)^m f(z)]. The (m-1)-th derivative is obtained from a Cauchy integral around a circle of the given radius, so f must be analytic on and inside that circle except at z0. It panics if m < 1.

func ResidueSimplePole

func ResidueSimplePole(f Function, z0 complex128, eps float64) complex128

ResidueSimplePole returns the residue of f at a simple pole z0, computed as the limit of (z-z0)f(z) as z approaches z0 via a small symmetric average of radius eps around z0.

func RisingFactorial

func RisingFactorial(z complex128, n int) complex128

RisingFactorial returns the Pochhammer symbol (z)_n = z(z+1)...(z+n-1), computed as Gamma(z+n)/Gamma(z). It returns 1 for n == 0.

func Sec

func Sec(z complex128) complex128

Sec returns the secant of z, 1/cos(z).

func Sign

func Sign(z complex128) complex128

Sign returns z/|z|, the unit complex number with the same argument as z, or 0 when z is 0.

func Sin

func Sin(z complex128) complex128

Sin returns the sine of z.

func Sinh

func Sinh(z complex128) complex128

Sinh returns the hyperbolic sine of z.

func Sqrt

func Sqrt(z complex128) complex128

Sqrt returns the principal square root of z, the branch whose result lies in the right half-plane (or on the non-negative imaginary axis).

func Tan

func Tan(z complex128) complex128

Tan returns the tangent of z.

func Tanh

func Tanh(z complex128) complex128

Tanh returns the hyperbolic tangent of z.

func TaylorCoefficient

func TaylorCoefficient(f Function, center complex128, k int, radius float64, n int) complex128

TaylorCoefficient returns the k-th Taylor coefficient of an analytic function f about center, a_k = f^(k)(center)/k!, computed from the same contour integral as LaurentCoefficient. It panics if k < 0.

func TaylorCoefficients

func TaylorCoefficients(f Function, center complex128, count int, radius float64, n int) []complex128

TaylorCoefficients returns the first count Taylor coefficients (orders 0 through count-1) of an analytic function f about center. It returns nil for count <= 0.

func WindingNumber

func WindingNumber(gamma func(float64) complex128, z0 complex128, n int) int

WindingNumber returns the winding number (index) of the closed curve gamma about the point z0, computed by accumulating the change in the argument of gamma(t)-z0 over t in [0, 1] with n samples and dividing by 2*pi. The result is rounded to the nearest integer. z0 must not lie on the curve.

func Zeta

func Zeta(s complex128) complex128

Zeta returns the Riemann zeta function at s via the Dirichlet eta function and analytic continuation, eta(s) = (1 - 2^(1-s)) zeta(s). It is valid across the complex plane except at the pole s = 1, and is accurate to about 1e-12 for moderate |Im(s)|.

Types

type Function

type Function func(complex128) complex128

Function is a complex-valued function of a single complex variable. It is the common argument type for the numerical routines in this package.

type Mobius

type Mobius struct {
	A, B, C, D complex128
}

Mobius represents a Mobius (linear fractional) transformation z -> (A*z + B) / (C*z + D). Such maps are the conformal automorphisms of the extended complex plane (the Riemann sphere).

func IdentityMobius

func IdentityMobius() Mobius

IdentityMobius returns the identity transformation z -> z.

func InversionMap

func InversionMap() Mobius

InversionMap returns the Mobius map z -> 1/z.

func MobiusFromPoints

func MobiusFromPoints(z1, z2, z3, w1, w2, w3 complex128) Mobius

MobiusFromPoints returns the unique Mobius transformation that sends the three distinct points z1, z2, z3 to w1, w2, w3 respectively. It is built by composing the standard map taking z1, z2, z3 to 0, 1, infinity with the inverse of the analogous map for the w points.

func NewMobius

func NewMobius(a, b, c, d complex128) Mobius

NewMobius constructs the Mobius transformation with the given coefficients.

func RotationMap

func RotationMap(theta float64) Mobius

RotationMap returns the rotation z -> e^{i*theta} * z about the origin.

func ScalingMap

func ScalingMap(a complex128) Mobius

ScalingMap returns the Mobius map z -> a*z. For |a| = 1 this is a pure rotation and RotationMap is a convenient alias.

func TranslationMap

func TranslationMap(b complex128) Mobius

TranslationMap returns the Mobius map z -> z + b.

func (Mobius) Apply

func (m Mobius) Apply(z complex128) complex128

Apply evaluates the transformation at z. When the denominator vanishes it returns a complex infinity, matching the point at infinity on the sphere.

func (Mobius) Compose

func (m Mobius) Compose(other Mobius) Mobius

Compose returns the transformation m(other(z)), the composition of m after other. Its coefficient matrix is the product of the two coefficient matrices.

func (Mobius) Determinant

func (m Mobius) Determinant() complex128

Determinant returns A*D - B*C. A Mobius map is invertible exactly when this is non-zero.

func (Mobius) FixedPoints

func (m Mobius) FixedPoints() []complex128

FixedPoints returns the fixed points of the transformation, the solutions of C*z^2 + (D-A)*z - B = 0. For a non-identity map this is one or two points; when C == 0 the point at infinity is a fixed point and only the finite ones are returned. The identity map (and any map fixing every point) returns nil.

func (Mobius) Inverse

func (m Mobius) Inverse() Mobius

Inverse returns the inverse transformation z -> (D*z - B) / (-C*z + A).

func (Mobius) Normalize

func (m Mobius) Normalize() Mobius

Normalize returns an equivalent transformation whose coefficient matrix has determinant 1, dividing all coefficients by a square root of the current determinant. A zero-determinant map is returned unchanged.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL