special

package module
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-numerics/special

special

Docs pkg.go.dev License

Special mathematical functions in pure Go (no cgo, no assembly): the gamma family, error functions, incomplete gamma and beta functions and their inverses, Bessel functions, zeta functions, combinatorics, logistic helpers and complete elliptic integrals — with an element-wise layer over go-ndarray.

🌐 Website · 📚 Documentation

It is the numerical foundation of the go-statistics library: every distribution's cdf and quantile function is computed with these functions, so their accuracy is the product. Each function is measured in ULPs against mpmath at high precision, over its whole domain — tails, zeros, poles, huge and tiny arguments, subnormals, NaN and ±Inf — and SciPy is measured on exactly the same points.

go get github.com/go-numerics/special
package main

import (
	"fmt"

	"github.com/go-numerics/special"
)

func main() {
	fmt.Println(special.Gamma(4.5))              // 11.631728396567448
	fmt.Println(special.Erfc(10))                // 2.088487583762545e-45
	fmt.Println(special.Ndtr(-30))               // 4.906713927148187e-198
	fmt.Println(special.Ndtri(1e-300))           // -37.0470962993612
	fmt.Println(special.GammaP(2.5, 1.5))        // regularised lower incomplete gamma
	fmt.Println(special.BetaIncInv(2, 3, 0.975)) // a beta quantile
	fmt.Println(special.Jv(2.5, 10))             // Bessel function of real order
}

Arrays (github.com/go-numerics/special/nd): any real dtype in, Float32 kept as Float32, everything else Float64; NumPy broadcasting; a call is split across cores once its estimated work (elements times a per-function cost class) passes about 40 µs, so a cheap function such as GammaSign needs about 13 000 elements, Binom about 330 and BetaInc about 80 (classes come from the measured cost of each function); a function you pass to Map1 keeps a threshold of 8000 elements; the goroutines claim blocks of elements from a shared counter, so slow cores and uneven elements do not stall the call. Results are bit-identical whatever the split (docs/perf.md).

x, _ := ndarray.Linspace(-5, 5, 1_000_001)
p, _ := nd.Ndtr(x)

Accuracy

Measured on about 250 000 points in 419 regions over 62 functions, this package's maximum error is at or below SciPy's in 418 regions (the exception, K1 at small x, is a tie at 1.05 ulp). Typical maxima:

this package SciPy 1.18.1
Gamma, LogGamma, Beta (all regions) 0.5–1 ulp 3–3e15 ulp
Digamma near its negative zeros 31 ulp 2.5e17 ulp
Erfc to the subnormal range 0.5 ulp 2e13 ulp
Ndtr lower tail, x in [-38.5, -10) 0.5 ulp 1.8e12 ulp
Ndtri, ErfInv, ErfcInv 0.5–0.6 ulp 1.4–4.4 ulp (wrong at subnormals)
GammaP / GammaQ, a in [1e4, 1e15] 0.6 ulp 8.9e15 ulp
BetaInc, a and b in [100, 1e4) 9 ulp 3487 ulp
J0 / Y0 next to their zeros 0.5 ulp 3e10 / 2e12 ulp
Jv at x in [1e4, 1e300] 1.7 ulp 5e18 ulp
Jv at x in [1e6, 1e15], v in {0, 0.3, 1, 2.7, 10, 50}, zeros included (1572 points, mpmath) 2.05 ulp not measured
Jv at half-integer orders next to a zero (v0.23.0) 0.5 ulp (was 14888) not measured
LogBeta where |log B| < 1/2 (v0.23.0) 0.5 ulp (was 78) 45 000 ulp
BetaIncInv / BetaIncCInv, a and b in [1e-8, 1e-3] (25416 points, mpmath) 4.2 ulp (was 5764) not measured

Since v0.23.0 the fitted tables are built on first use, not at program start: a program importing the package no longer pays 0.93 ms and 963 KB of initialisation (now 0.09 ms, 0 B), and the WebAssembly binary of a program calling Gamma alone is 27 % smaller; the first call of a function pays its table once (Erfc 74 us, Jv's tables 289 us, one run on a loaded host).

The area not solved: inverses of the incomplete beta function with a or b far below 1e-8 (both packages can be wrong there).

Full method, sources of every algorithm, negative control and limitations: docs/accuracy.md; every region: docs/accuracy-table.md.

Accuracy contract

Bound: for every function and every region, the maximum error in ulps of the result is at most the region's budget (explicit budgets, e.g. incbeta_test.go: the measured maximum times 1.3, rounded up; cases without one, budgets_test.go: 1.5 times the measured maximum, at least 1 ulp; typically 0.5 to 1 ulp, 15 ulp for BetaInc and BetaIncC at a and b in [100, 1e4), 48 ulp for Digamma next to its negative roots), and NaN, ±Inf and ±0 are returned where the function has them. Unbounded today (budget +Inf, the root is not determined by double-double evaluation of the function): BetaIncInv and BetaIncCInv for a, b < 1e-3 and for exactly one of a, b below 1e-3 (four regions of the accuracy fixtures); for a and b in [1e-8, 1e-3] a dedicated sweep against mpmath (25416 points) measures 4.2 ulp (stated bound 5), and the fixtures' budgets are to be bounded from it; below 1e-8 it remains open work (Ref #11, and "Beta inverses with a or b far below 1e-3" in docs/accuracy.md). Oracle: mpmath 1.4.1 at 192 and 320 bits (points where the two disagree by more than 2^-80 are dropped and counted), stored as a double-double hi + lo and compared exactly, so a correctly rounded result scores 0.5 ulp; SciPy is measured on the same points but is never the reference. Exact: the bit-exact build stays and is tested: every product is rounded explicitly (tools/fmalint), TestArchHash pins the output bits of every public function on 8 architectures, and the vector forms (nd, …To) equal the scalar ones element by element. A faster kernel (FMA, a reordered sum, another algorithm) lands when it is faster and within Bound; the golden hashes are then regenerated as an explicit step, never as a reason to keep a slower kernel.

The bound is asserted by TestAccuracyBound in accuracy_test.go over the fixtures of every function of the root package, on every CI lane; the complex functions (cmplx) keep their own gate, TestAccuracy in cmplx/harness_test.go, with its own budgets. Bit identity is how a kernel is ported and checked, not what the package promises: the promise is the accuracy table.

Speed

Accuracy has a price. Measured against SciPy 1.18.1 on one core (refbench v0.4.0 with only the bench module bumped, tag v0.20.0 at d415e46, 2026-11-16, darwin/arm64, 10 000-point calls, host load 4.0–8.0 (median 5.7), valid run (151 rows, 2 rounds retried above the gate); ratios are Go time over SciPy time). SciPy is the only reference timed: the Julia worker (SpecialFunctions.jl, Bessels.jl) and the worst-ulp columns were not run, because the bench of v0.20.0 has no wiring for them (the accuracy is in the tables above):

  • faster than SciPy: Gamma 0.83 (8.4 vs 10.0 ns/element; nd.Gamma 0.84), LogGamma 0.76, LogBeta 0.59, ErfInv 0.49, ErfcInv 0.62, Ndtri 0.57, LogNdtr 0.60, Ndtr 0.84, Erf 0.88, Digamma 0.80, Beta 0.83, Logit 0.89, Log1pExp 0.92, Zeta 0.65, Polygamma 0.05, Comb 0.86, Factorial2 0.03, LogSumExp 0.35, the order 0 and 1 Bessel functions (J0 0.75, I0 0.91, I0e 0.29, K0 0.93, K1 0.92, K1e 0.37), the elliptic integrals (0.37–0.46), the real orders Jv 0.47, Yv 0.54, Kv 0.60, Kve 0.58, Ive 0.37, and the integer orders Jn 0.18 (128 vs 722 ns/element), Kn 0.11 (40 vs 357 ns) and In 0.86;
  • ties (verdict tie, or within 3 percent): Erfc 1.00, Erfcx 0.98, I1 0.97, LogExpit 1.08, nd.K0 0.98, nd.Comb 1.00;
  • still slower, 1.05–1.54x: Softmax 1.05, LogSoftmax 1.09, Xlogy 1.07, GammaQ 1.13, GammaP 1.17, Perm 1.20, Expit 1.23, Factorial of non-integers 1.23, GammaSign 1.24, GammaQInv 1.48, GammaPInv 1.54 (nd.Softmax 1.20, nd.LogSoftmax 1.26);
  • several times slower, where a double-double continued fraction or series produces the rounded result: BetaIncInv and BetaIncCInv 3.5, Binom 3.0, Yn 2.34 (88 vs 38 ns), BetaInc and BetaIncC 2.1, Xlog1py 1.9, Iv 1.77 (open defects, #33).
  • Softmax, LogSoftmax, LogSumExp: float64 kernels instead of a double-double exponential per element (softmax_fast.go; v0.13.0: 4.84, 4.13 and 1.47, now 1.05, 1.09 and 0.35): relative error at most 8u, 5u and 4u (u = 2^-53; at most 3.9u, 2.1u and 1.87u measured, the last by the independent review's 320-bit oracle), no longer correctly rounded, and the bits depend on the architecture. Which to use: Softmax, LogSoftmax, LogSumExp by default (fast, a stated bound); SoftmaxExact, LogSoftmaxExact, LogSumExpExact (and nd.*Exact) where the last bit matters or the bits must be the same on every architecture (the v0.19.0 double-double code, about 4.5 times slower; the *Exact functions have no refbench row). Neither states a relative accuracy where the terms of the others are subnormal (their sum below about n·2^-1000) nor, for LogSumExp, where m + log Σ cancels: open, see the issue linked from the pull request.

Since v0.5.6, I1, I1e, K1 and K1e use the same fitted octave interpolants as I0e and K0e (and are more accurate: worst case against mpmath 3.45 to 0.51 ulp for I1); Comb, Factorial2, Factorial and Xlog1py have faster paths that return the same bits as before.

Method and full table: docs/perf.md.

Functions

Each row gives the Go function and, in brackets, the scipy.special function it is measured against — the pairing comes from the harness (bench/perf_test.go and testdata/gen/fam_*.py), not from transliterating a name, which is why Log1pExp is scipy's softplus and LogGamma is gammaln. 64 of the 67 carry such a pairing; — marks the three that do not, and the notes under the table say what they are. readme_scipy_test.go reads the harness and fails if this column drifts from it.

family functions (scipy equivalent)
gamma Gamma (gamma), LogGamma (gammaln), LogGammaSign (—), GammaSign (gammasgn), Digamma (digamma), Polygamma (polygamma), Beta (beta), LogBeta (betaln)
incomplete gamma / beta GammaP (gammainc), GammaQ (gammaincc), GammaPInv (gammaincinv), GammaQInv (gammainccinv), BetaInc (betainc), BetaIncC (betaincc), BetaIncInv (betaincinv), BetaIncCInv (betainccinv)
error function, normal Erf (erf), Erfc (erfc), Erfcx (erfcx), ErfInv (erfinv), ErfcInv (erfcinv), Ndtr (ndtr), Ndtri (ndtri), LogNdtr (log_ndtr)
combinatorics Comb (comb), Perm (perm), Binom (binom), Factorial (factorial), Factorial2 (factorial2), LogFactorial (gammaln(n+1))
logistic, log helpers Expit (expit), LogExpit (log_expit), Logit (logit), Log1pExp (softplus), Xlogy (xlogy), Xlog1py (xlog1py), LogSumExp (logsumexp), Softmax (softmax), LogSoftmax (log_softmax), LogSumExpExact (—), SoftmaxExact (—), LogSoftmaxExact (—)
zeta Zeta (zeta), HurwitzZeta (zeta)
Bessel J0 (j0), J1 (j1), Jn (jv), Jv (jv), Y0 (y0), Y1 (y1), Yn (yn), Yv (yv), I0 (i0), I1 (i1), In (—), Iv (iv), K0 (k0), K1 (k1), Kn (kn), Kv (kv), I0e (i0e), I1e (i1e), Ive (ive), K0e (k0e), K1e (k1e), Kve (kve), JnZeros (jn_zeros)
elliptic EllipK (ellipk), EllipKm1 (ellipkm1), EllipE (ellipe)

The three without a pairing, and why:

  • Kn — scipy does have kn; this package simply does not measure it against it. The bracketed name is scipy's, not a measurement.
  • In — scipy has no in, and cannot: it is a Python keyword. Iv at integer order is the equivalent, and In(n, x) is Iv(float64(n), x). Kn/Kv and Jn/Jv stand in the same relation, which is why Jn is bracketed jv: scipy's jn is a deprecated alias of jv.
  • LogGammaSign — no scipy counterpart in any spelling; scipy splits the two results across gammaln and gammasgn.

The 25 …To slice kernels — GammaTo, ErfTo, XlogyTo and the rest (since the nd batch API runs on them, GammaSignTo, Factorial2To, PermTo, BinomTo and Xlog1pyTo are among them) — are the batch form of the function they are named after and compute the same thing, so they share its scipy equivalent rather than earning rows here. Two exceptions are worth knowing: TrigammaTo is exported while Trigamma is not, so that kernel has no scalar form in the public API. It is psi'(x), which scipy spells polygamma(1, x) — the same function Polygamma already provides. And Factorial2To and PermTo take float64 arguments, like the nd functions they serve: an argument that is not an integer of magnitude at most 2^31-1 gives NaN, where Factorial2 and Perm take integers.

Zeta and HurwitzZeta share one scipy name, zeta(x, q=None), which takes the second argument optionally. LogFactorial is bracketed as a call rather than a name because the function is gammaln with its argument shifted, which is what the harness measures it against.

Complex functions

github.com/go-numerics/special/cmplx (new in v0.4.0) is a drop-in superset of the standard library's math/cmplx. Changing the import path is enough: the names and signatures are the same, with C11 Annex G special values.

  • New functions: Mul and Div (the Annex G product and quotient), Proj, Log2, Log1p and Expm1.
  • Unchanged from math/cmplx: Abs, Phase, Conj, Polar, Rect, Inf, NaN, IsInf and IsNaN are one call to a correctly specified real function, and are re-exported as they are.
  • Rewritten: every other function. The algorithms are FreeBSD msun's (as NumPy's npymath carries them), carried in double-double where float64 loses accuracy.
  • Underlying real functions: sin, cos and tan use this package's Payne–Hanek reduction, and atan2 is table-driven double-double.
import "github.com/go-numerics/special/cmplx"

cmplx.Tanh(400 + 1i)   // (1+0i); math/cmplx: NaN
cmplx.Log(1 + 1e-10i)  // (5e-21+1e-10i); math/cmplx: real part 0
cmplx.Pow(1i, 2)       // (-1+0i), exactly; math/cmplx: (-1+1.2e-16i)

Each part is measured in ulps against mpmath at 192 and 320 bits. The fixtures cover the tiny, subnormal, huge and near-overflow regions, the branch points, and both sides of every branch cut. Worst part per function:

function this package math/cmplx (Go 1.27.3)
Mul, Div 0.5 ulp 1.2e16 ulp (builtin *, /)
Exp, Cosh, Sinh, Cos, Sin 1.29 ulp 439 ulp, Inf near Re z = 710
Tanh, Tan, Cot 2.55 ulp NaN for |Re z| > 355
Log, Log10, Log2, Sqrt 1.03 ulp (0.5 outside subnormals) 8.9e15 ulp near |z| = 1, wrong sign of π on the cut
Log1p, Expm1 0.53 ulp 2.5e27 ulp (naive formula)
Atanh, Atan 1.44 ulp 1.4e16 ulp
Asinh, Asin, Acos, Acosh 2.82 ulp (#26) 1e9 ulp, wrong quadrant above 1e154
Pow 0.51 ulp; up to 2937 ulp where |y log x| reaches 3e5 (#27) 3.4e20 ulp

Each region has a CI budget of 1.5 times its measured maximum, and at least the design's target. Two budgets sit above the target, and both are open issues:

  • Asinh, Asin, Acos and Acosh: 3 ulp (#26).
  • Pow, region unit: 4500 ulp (#27), inside the design's (2 + |y log x|) ulp bound.

Every region is listed in docs/accuracy-complex.md, and the design is docs/design-complex.md. Timing against NumPy's complex ufuncs has not been measured yet.

Double-double arithmetic

github.com/go-numerics/special/dd (public since v0.3.0) is the double-double type the functions above are built on: dd.DD{Hi, Lo} with Add, Sub, Mul, Div, Sqrt, Exp, Log, the exact accumulators Dot, DotDD, Sum, AddProd (every product exact, accumulation in double-double, so an ill-conditioned dot product keeps about 100 bits), and conversions FromInt64, FromBig, Parse (a decimal string to the nearest double-double), (DD).Big, (DD).Float. Relative errors, fuzzed against math/big: Add 3u², Mul 5u², Div 15u²+56u³ (u = 2⁻⁵³); Sqrt is measured (under 8u²). It is the arithmetic behind double-double QR with iterative refinement (the NIST StRD Filip and Wampler problems to 15 digits). ExpMant and ExpReductionMargin expose the exponential's argument reduction, for callers that must know whether two nearby arguments take the same table entry (go-statistics/stats uses it; it is the single copy of this package).

Testing

go test ./... runs the accuracy fixtures (mpmath rows, per-region ULP budgets). CI adds the 100% statement-coverage gate, and a mutation gate over the *_fast.go files: mutsweep v0.8.0 moves each comparison of a refusal one step (-mutate boundary: < to <=, > to >=) and deletes each refusal in turn, and tools/mutgate fails on any survivor, hang, or mutation caught only by a runtime panic (a guard held by luck, not by an assertion) that mutation-allowlist.json does not list with its reason, and on a listed entry that nothing matches any more. A pull request that changes only *_fast.go files (and documents) sweeps just those; one that changes anything else (tests, other sources, testdata/, dd/, internal/, go.mod, the allowlist, tools/mutgate, .github/workflows) sweeps every file, and so does every push to main; one that changes only documents sweeps nothing. Allowlist entries are keyed by file, function and mutation text. The full sweep took about 40 minutes in one job (the limit is 45), so CI runs it in five shards (--shard k/n: the sorted file list, after --files or --changed-since, split by index mod n, so a new *_fast.go always lands in exactly one shard). An allowlist entry is judged only in the shard that sweeps its file, and a stale one fails in that shard alone. The shards are balanced by mutant count (mutsweep -tally; the sorted index mod 5: binom 14, jv_hankel 12 (7 comparisons and 5 refusals; the deletion of if inDD does not compile) and kv_temme 16 (9 comparisons and 7 refusals) = 42; factorial 0 and jv_mid 13 (10 comparisons and 3 refusals; the deletion of the jvMidTailDone test does not compile) = 13; gamma 7 and kn 9 = 16; i 33 and kv_cf2 6 = 39; jn_yn 43 and kv 24 = 67).

The gate does not cover every guard. mutsweep reaches refusals (an if whose body returns, panics or breaks) and the comparisons of their conditions and of for loops; it does not reach a guard in an if init statement, an if that is not a refusal, or a comparison inside a return (go-fleettools/mutate#21). A clean gate says nothing about those; they are held by tests written for them and checked by running the mutation by hand (for example ylow > 0 in ynRecurErr, TestYnCertificateBoundNeverNegative, and the final return rel <= jyAcceptJ of jnMiller, TestJnMillerFinalComparison). Nine deletions are reported "not a mutant" because the mutated file does not compile, so the gate never tests them: kv_fast.go:166 (if !done), :250 (if at > prev), :271 (if !(ok0 && ok1)) and :129 (if !ok of kvCF2Chain), kv_temme_fast.go:312 (if !ok of kvTemmeChain, asserted by TestKvTemmeCaps at x = 30, which was not run with the deletion), kv_cf2_fast.go:184 (if !converged; asserted by TestKvCF2Caps, likewise not run with the deletion), binom_fast.go:136 (if !ok), gamma_fast.go:158 (the zivRound refusal) and :194 (if ok in gammaNeg). Only the last is verified: TestGammaNegTakesTheFastPath holds it (the fallback is stubbed to return NaN). For the other six nothing is known to hold them; it is unverified. Locally:

go install github.com/go-fleettools/mutate@v0.8.0
go install github.com/go-fleettools/mutate/mutsweep@v0.8.0
go -C tools run ./mutgate -C .. --allowlist ../mutation-allowlist.json --timeout 3m
# a pull request's view: only the files that differ from main
go -C tools run ./mutgate -C .. --allowlist ../mutation-allowlist.json --timeout 3m --changed-since origin/main
# one of CI's five shards
go -C tools run ./mutgate -C .. --allowlist ../mutation-allowlist.json --timeout 3m --shard 4/5

Portability

Pure Go; CI runs the full accuracy suite on amd64, arm64, 386, arm (32-bit), riscv64, loong64, ppc64le and s390x, compiles the tests for every go tool dist list target, and holds a 100% statement-coverage gate.

One finding along the way: on amd64, Go's math.Log (assembly) returns about -709.09 for every subnormal argument (it should go down to -744.44); this package never calls it on a value that can be subnormal.

Roadmap

docs/roadmap.md lists what Julia's SpecialFunctions and LogExpFunctions, R's nmath and GSL's specfunc do better or have in addition, ranked by gain for cost, with an implementation source and an mpmath oracle for each item.

License

BSD 3-Clause. Ported algorithms keep their attribution in place: Cephes (Stephen L. Moshier, distributed under BSD by SciPy), Boost.Math (Boost Software License 1.0), FreeBSD msun (BSD-2-Clause) as carried by NumPy's npymath (BSD-3-Clause), and Go's math trigReduce (BSD-3-Clause).

Documentation

Overview

Package special implements special mathematical functions in pure Go (no cgo): the gamma family, error functions, incomplete gamma and beta functions and their inverses, Bessel functions, zeta functions and combinatorial and logistic helpers.

Accuracy is the product. Every function is measured in ULPs against mpmath evaluated at high precision over its whole domain, tails, poles and special values included; the fixtures and the measured table are in the repository (testdata/fixtures, docs/accuracy.md). Each algorithm cites its source in a comment at its definition.

Conventions: a function returns NaN outside its domain and ±Inf at a pole, as the IEEE 754 operations do; no function panics on a float argument.

Example
package main

import (
	"fmt"

	"github.com/go-numerics/special"
)

func main() {
	fmt.Println(special.Gamma(4.5))
	fmt.Println(special.Erfc(10))              // 2.088487583762545e-45, no underflow to 0
	fmt.Println(special.Ndtr(-30))             // the far tail keeps full relative accuracy
	fmt.Println(special.Ndtri(1e-300))         // and so does its inverse
	fmt.Println(special.LogGamma(1 + 0x1p-30)) // log Γ near its zero at 1
}
Output:
11.631728396567448
2.088487583762545e-45
4.906713927148187e-198
-37.0470962993612
-5.375739784311044e-10

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Beta

func Beta(a, b float64) float64

Beta returns the beta function B(a, b) = Γ(a)Γ(b)/Γ(a+b).

Special values: NaN if either argument is NaN; +Inf when a or b is 0 or a negative integer and a+b is not a non-positive integer; 0 when a+b is a non-positive integer and neither a nor b is; overflow to ±Inf and underflow to 0 as the true value dictates.

Algorithm: for positive arguments below 10 the three gamma values come from Gamma's double-double core and the rounding of a+b is corrected to first order (Γ(s+e) = Γ(s)(1 + ψ(s)e)); otherwise B = exp(log B) with log B from LogBeta's cancellation-free double-double evaluation.

func BetaInc

func BetaInc(a, b, x float64) float64

BetaInc returns the regularised incomplete beta function I_x(a, b) = (1/B(a, b)) ∫₀ˣ t^(a-1) (1-t)^(b-1) dt (DLMF 8.17.2).

Domain: a >= 0, b >= 0, 0 <= x <= 1, not both a and b zero or infinite. Special values: I_0 = 0 and I_1 = 1; for a = 0 (or b = +Inf), I_x = 1 for x > 0; for b = 0 (or a = +Inf), I_x = 0 for x < 1; NaN for an argument outside the domain or NaN.

Algorithm: DiDonato & Morris' TOMS 708 (BRATIO) with double-double prefixes; see the comment at the top of incbeta.go.

func BetaIncC

func BetaIncC(a, b, x float64) float64

BetaIncC returns the complement of the regularised incomplete beta function, 1 - I_x(a, b) = I_(1-x)(b, a), computed directly where it is the smaller of the two so that it keeps its relative accuracy for x near 1.

Domain and special values: as BetaInc, with the complementary values.

Algorithm: as BetaInc.

func BetaIncCInv

func BetaIncCInv(a, b, q float64) float64

BetaIncCInv returns the inverse of the complement of the regularised incomplete beta function: the x in [0, 1] with 1 - I_x(a, b) = q.

Domain and special values: as BetaIncInv, with BetaIncCInv(a, b, 0) = 1 and BetaIncCInv(a, b, 1) = 0.

Algorithm: as BetaIncInv, with q as the exact target of the upper tail (so a small q keeps full relative accuracy in 1 - x).

func BetaIncInv

func BetaIncInv(a, b, p float64) float64

BetaIncInv returns the inverse of the regularised incomplete beta function: the x in [0, 1] with I_x(a, b) = p.

Domain: a > 0, b > 0, 0 <= p <= 1. Special values: BetaIncInv(a, b, 0) = 0 and BetaIncInv(a, b, 1) = 1 for any a, b >= 0 that are not NaN, with a = 0 or b = 0 giving NaN (SciPy's conventions); NaN for p outside [0, 1], a or b negative, infinite (for 0 < p < 1) or NaN. A root closer to 0 than the smallest subnormal returns 0, one closer to 1 than half an ulp of 1 returns 1.

Algorithm: safeguarded Newton iteration in log x or log(1-x) on the smaller tail; see the comment at the top of incbeta_inv.go.

func BetaTo added in v0.2.0

func BetaTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

BetaTo sets dst[i] = Beta(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Binom

func Binom(n, k float64) float64

Binom returns the generalised binomial coefficient Γ(n+1) / (Γ(k+1) Γ(n-k+1)) for real n and k, as SciPy's binom.

For integer k with 0 <= k <= 64 it is the product Π (n-k+i)/i in double-double (any real n, including negative); when n+1 > 0 and n-k+1 > 0 it is 1/((n+1) B(n-k+1, k+1)) with the beta function's cancellation-free evaluation; otherwise the logarithms of the three gamma functions (each in double-double, at double-double arguments) are combined before a single exponential, with their signs. It is 0 where 1/Γ(k+1) or 1/Γ(n-k+1) vanishes (a non-positive integer argument), NaN for NaN arguments or a pole of Γ(n+1) not cancelled by the denominator.

func BinomTo added in v0.21.0

func BinomTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

BinomTo sets dst[i] = Binom(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Comb

func Comb(n, k int64) float64

Comb returns the binomial coefficient C(n, k) = n! / (k! (n-k)!), the number of k-element subsets of an n-set, as a float64: 0 when k < 0, k > n or n < 0 (as SciPy's comb with exact=False).

Algorithm: for min(k, n-k) <= 64 the product Π (n-k+i)/i in double-double, so every result below 2^53 is exact and larger ones are correctly rounded; beyond, 1/((n+1) B(k+1, n-k+1)) with the beta function's cancellation-free double-double logarithm (LogBeta), which avoids the catastrophic cancellation of log n! - log k! - log (n-k)! for large n.

func Digamma

func Digamma(x float64) float64

Digamma returns the digamma function ψ(x) = d/dx log Γ(x).

Special values: ψ(+Inf) = +Inf; ψ(+0) = -Inf, ψ(-0) = +Inf; NaN at the poles x = -1, -2, ..., at -Inf and for NaN.

Algorithm: on [1, 2] ψ(x) = (x - x0) D(x-1) with x0 the positive zero in double-double and D fitted to mpmath (the form of Boost.Math's digamma_imp_1_2); (0, 10) is reduced to [1, 2] by ψ(x+1) = ψ(x) + 1/x with the reciprocals in double-double; x >= 10 uses the asymptotic series (DLMF 5.11.2). For -40 < x < 0, ψ(x) = ψ(t) - Σ 1/(x+k) with t = x+n in [0, 1) and exact x+k, summed in double-double; below, the reflection ψ(x) = ψ(-x) - 1/x - π cot(πx) (DLMF 5.5.4) with exact reduction of πx.

func DigammaTo added in v0.2.0

func DigammaTo(ζdst, ζx []float64)

DigammaTo sets dst[i] = Digamma(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func EllipE

func EllipE(m float64) float64

EllipE returns the complete elliptic integral of the second kind E(m) = ∫₀^{π/2} sqrt(1 - m sin²θ) dθ, parameter m = k², for m <= 1.

Special values: E(1) = 1, E(-Inf) = +Inf, NaN for m > 1 or NaN.

func EllipK

func EllipK(m float64) float64

EllipK returns the complete elliptic integral of the first kind K(m) = ∫₀^{π/2} dθ / sqrt(1 - m sin²θ), parameter m = k² (SciPy's ellipk convention), for m <= 1.

Special values: K(1) = +Inf, K(-Inf) = 0, NaN for m > 1 or NaN. Near m = 1 use EllipKm1 with p = 1-m to keep the digits m itself cannot hold.

func EllipKm1

func EllipKm1(p float64) float64

EllipKm1 returns K(1 - p) for p >= 0 (SciPy's ellipkm1): the form to use near m = 1, where p carries digits that 1-p would lose.

func Erf

func Erf(x float64) float64

Erf returns the error function erf(x) = 2/sqrt(pi) ∫₀ˣ exp(-t²) dt.

Special values: erf(±0) = ±0, erf(±Inf) = ±1, erf(NaN) = NaN.

Algorithm: for |x| <= 1/2, x E(x²) with E fitted to mpmath; beyond, 1 - erfc(|x|) in double-double with erfc from erfcMant.

func ErfInv

func ErfInv(y float64) float64

ErfInv returns the inverse error function: ErfInv(Erf(x)) = x.

Special values: ErfInv(±1) = ±Inf, ErfInv(±0) = ±0; NaN for |y| > 1 or NaN.

Algorithm: for |y| <= 1/2, y R(y²) with R fitted to mpmath; beyond, erfcinv(1 - |y|) (1 - |y| exact) fitted directly in q = 1 - |y| on 8 pieces per octave (erfcInvDirHL), carried as hi + lo and rounded once, with no logarithm, square root or division; about half an ulp.

func ErfInvTo added in v0.2.0

func ErfInvTo(ζdst, ζx []float64)

ErfInvTo sets dst[i] = ErfInv(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func ErfTo added in v0.2.0

func ErfTo(ζdst, ζx []float64)

ErfTo sets dst[i] = Erf(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Erfc

func Erfc(x float64) float64

Erfc returns the complementary error function erfc(x) = 1 - erf(x).

Special values: erfc(+Inf) = 0, erfc(-Inf) = 2, erfc(NaN) = NaN; erfc underflows to 0 for x > 27.2.

Algorithm: for x >= 1/2, exp(-x²) erfcx(x) with x² in double-double and erfcx fitted to mpmath, multiplied before the single rounding; for |x| < 1/2, 1 - erf(x) in double-double; for x <= -1/2, 2 - erfc(-x).

func ErfcInv

func ErfcInv(q float64) float64

ErfcInv returns the inverse complementary error function: ErfcInv(Erfc(x)) = x, for 0 <= q <= 2.

Special values: ErfcInv(0) = +Inf, ErfcInv(2) = -Inf, ErfcInv(1) = 0; NaN outside [0, 2] or for NaN.

Algorithm: as ErfInv, with X evaluated at sqrt(-log q) directly, so that small q (down to the smallest subnormal) keep full relative accuracy.

func ErfcInvTo added in v0.2.0

func ErfcInvTo(ζdst, ζx []float64)

ErfcInvTo sets dst[i] = ErfcInv(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func ErfcTo added in v0.2.0

func ErfcTo(ζdst, ζx []float64)

ErfcTo sets dst[i] = Erfc(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Erfcx

func Erfcx(x float64) float64

Erfcx returns the scaled complementary error function erfcx(x) = exp(x²) erfc(x).

Special values: erfcx(+Inf) = 0, erfcx(-Inf) = +Inf, erfcx(NaN) = NaN; erfcx overflows to +Inf for x < -26.628.

Algorithm: for x >= 1/2 the fitted erfcx directly (no exponential at all); for |x| < 1/2, exp(x²)(1 - erf(x)) in double-double; for x <= -1/2, 2 exp(x²) - erfcx(-x).

func ErfcxTo added in v0.2.0

func ErfcxTo(ζdst, ζx []float64)

ErfcxTo sets dst[i] = Erfcx(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Expit

func Expit(x float64) float64

Expit returns the logistic sigmoid 1/(1 + exp(-x)).

e = exp(-|x|) comes from the float64 kernel as hi + lo (about 2^-68 relative); 1 + e is formed exactly (Fast2Sum) and the quotient 1/(1+e) or e/(1+e) is corrected by its FMA residual, so the result is within about half an ulp everywhere, including the lower tail where it is exp(x) to full relative accuracy. Below -660, where the low part of e would be subnormal, the double-double path is used. Expit(-Inf) = 0, Expit(+Inf) = 1, NaN for NaN.

func ExpitTo added in v0.2.0

func ExpitTo(ζdst, ζx []float64)

ExpitTo sets dst[i] = Expit(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Factorial

func Factorial(n float64) float64

Factorial returns n! = Γ(n+1). Integers 0..170 come from a table of correctly rounded values; other non-negative n use the gamma function (with n+1 never rounded: Γ(1+n) from the core for n < 1, n Γ(n) above).

Special values: NaN for NaN; 0 for n < 0 (the convention of SciPy's factorial); +Inf for n > 170.6243769563027.

func Factorial2

func Factorial2(n int) float64

Factorial2 returns the double factorial n!! = n (n-2) (n-4) ..., ending at 1 or 2. 0!! = (-1)!! = 1 by the usual convention (SciPy's factorial2 returns 0 for -1); n < -1 gives 0, as in SciPy. The product is carried in double-double and rounded once, so the result is correctly rounded (up to the 2^-100 double-double error) and exact whenever n!! < 2^53; it overflows to +Inf for n > 300.

func Factorial2To added in v0.21.0

func Factorial2To(ζdst, ζx []float64)

Factorial2To sets dst[i] = Factorial2(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Gamma

func Gamma(x float64) float64

Gamma returns the gamma function Γ(x) = ∫₀^∞ t^(x-1) e^(-t) dt, continued to negative x by Γ(x) = Γ(x+1)/x.

Special values: Γ(+Inf) = +Inf; Γ(±0) = ±Inf; Γ(x) = NaN for x a negative integer, x = -Inf or NaN; Γ overflows to +Inf for x > 171.6243769563027 and underflows to ±0 for x < -184.

Algorithm: on t in [-0.5, 1], Γ(1+t) = 1 + t(t-1)F(t) with F a piecewise Chebyshev interpolant fitted to mpmath (testdata/gen/fit.py); other x in (0, 10) are reduced to it by the recurrence Γ(x+1) = xΓ(x), whose factors x-k are exact and whose product is carried in double-double; x >= 10 uses Stirling's series (DLMF 5.11.1) with (x-1/2)log x - x formed in double-double and exponentiated by dd.Exp; x < -34 uses the reflection formula Γ(x) = -π / (x sin(πx) Γ(-x)) with sin(πx) reduced exactly. Integers are a table of correctly rounded factorials. Measured error against mpmath: docs/accuracy.md.

func GammaP

func GammaP(a, x float64) float64

GammaP returns the regularised lower incomplete gamma function P(a, x) = γ(a, x)/Γ(a) = (1/Γ(a)) ∫₀ˣ t^(a-1) e^(-t) dt (DLMF 8.2.4).

Domain: a >= 0, x >= 0. Special values: P(a, 0) = 0 for a > 0; P(a, +Inf) = 1; P(0, x) = 1 for x > 0; P(+Inf, x) = 0 for finite x; NaN for a < 0, x < 0, a = x = 0, a = x = +Inf, or a NaN argument.

Algorithm: see the comment at the top of incgamma.go (DiDonato & Morris / Boost.Math method selection, Temme's expansion, double-double prefix).

func GammaPInv

func GammaPInv(a, p float64) float64

GammaPInv returns the inverse of the regularised lower incomplete gamma function: the x >= 0 with P(a, x) = p.

Domain: a >= 0, 0 <= p <= 1. Special values: GammaPInv(a, 0) = 0, GammaPInv(a, 1) = +Inf; for a = 0 or a = +Inf only those two are defined (NaN otherwise); NaN for a < 0, p outside [0, 1] or a NaN argument. A root below the smallest subnormal returns 0.

Algorithm: DiDonato & Morris' initial approximation (via Boost.Math's find_inverse_gamma) refined by a safeguarded Halley iteration in log x; see the comment at the top of incgamma_inv.go.

func GammaQ

func GammaQ(a, x float64) float64

GammaQ returns the regularised upper incomplete gamma function Q(a, x) = Γ(a, x)/Γ(a) = 1 - P(a, x) (DLMF 8.2.4), computed directly (not as 1 - P) where it is the smaller of the two, so it keeps its relative accuracy in the upper tail down to the subnormal range.

Domain and special values: as GammaP, with Q = 1 - P at the edges (Q(a, 0) = 1, Q(a, +Inf) = 0, Q(0, x) = 0, Q(+Inf, x) = 1).

Algorithm: as GammaP.

func GammaQInv

func GammaQInv(a, q float64) float64

GammaQInv returns the inverse of the regularised upper incomplete gamma function: the x >= 0 with Q(a, x) = q.

Domain: a >= 0, 0 <= q <= 1. Special values: GammaQInv(a, 0) = +Inf, GammaQInv(a, 1) = 0; for a = 0 or a = +Inf only those two are defined (NaN otherwise); NaN for a < 0, q outside [0, 1] or a NaN argument.

Algorithm: as GammaPInv, matching Q(x) = q directly when q <= 1/2 (so small q keep full relative accuracy in x).

func GammaSign

func GammaSign(x float64) float64

GammaSign returns the sign of Γ(x): +1 or -1, ±1 at ±0 (following the sign of the infinity Γ returns there), and NaN at the poles x = -1, -2, ..., at -Inf and for NaN.

func GammaSignTo added in v0.21.0

func GammaSignTo(ζdst, ζx []float64)

GammaSignTo sets dst[i] = GammaSign(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func GammaTo added in v0.2.0

func GammaTo(ζdst, ζx []float64)

GammaTo sets dst[i] = Gamma(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func HurwitzZeta

func HurwitzZeta(s, q float64) float64

HurwitzZeta returns the Hurwitz zeta function ζ(s, q) = Σ_{k>=0} (k+q)^-s for s > 1.

Special values: +Inf at s = 1 and at q = 0, -1, -2, ...; NaN for s < 1, for NaN arguments and for q < 0 with non-integer s (where (k+q)^-s is not real); ζ(s, +Inf) = 0.

Algorithm: Euler–Maclaurin summation in double-double (hurwitzPos). For q < 0 and integer s, the negative terms (q+k)^-s, k < m = ceil(-q), are summed explicitly with their signs and the rest by hurwitzPos when m <= 256; beyond, in O(1) time and memory, by the shift formula (DLMF 25.11.3) as three hurwitzPos calls (hurwitzNegFar). For odd s and q = -(m - 1/2) the negative terms cancel the first m positive ones exactly and the result is ζ(s, 1-q).

Accuracy: for q > 0, and for q < 0 with even s, the error is a few ulp. For q < 0 with odd s, ζ(s,q) is the small difference of ζ(s,f) and ζ(s,1-f) (f = q + ceil(-q)) when f is near 1/2, so only an ABSOLUTE bound holds there: |error| <= 2 ulp + 2^-72 (ζ(s,f) + ζ(s,1-f)) (2^-74 measured against a 400-bit oracle); the relative error is a few ulp wherever the result exceeds 2^-20 of that scale, and degrades in proportion below it (about 600 ulp at s = 5, q = -299.5 + ulp). The same bound holds for the explicit sum and for the O(1) formula.

func HurwitzZetaTo added in v0.2.0

func HurwitzZetaTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

HurwitzZetaTo sets dst[i] = HurwitzZeta(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func I0

func I0(x float64) float64

I0 returns the modified Bessel function of the first kind of order zero, I0(x) = sum (x^2/4)^k / (k!)^2, for real x (even). I0(0) = 1, I0(±Inf) = +Inf, I0(NaN) = NaN; overflows to +Inf for |x| > ~713.99.

Algorithm: the power series below 1/8; up to 512, e^x times I0(x) e^-x from degree-11 interpolants on 8 pieces per octave of x (zz_besselI0eOct_coeffs.go, fitted to mpmath), with e^x from dd.ExpMant and the product rounded once; DLMF 10.40.1 beyond.

func I0e

func I0e(x float64) float64

I0e returns I0(x) e^-|x|, defined for all real x; I0e(±Inf) = 0.

func I1

func I1(x float64) float64

I1 returns the modified Bessel function of the first kind of order one, I1(x) = (x/2) sum (x^2/4)^k / (k! (k+1)!), for real x (odd). I1(0) = 0, I1(±Inf) = ±Inf, I1(NaN) = NaN; overflows for |x| > ~713.98.

Algorithm: Boost.Math bessel_i1.hpp double-precision minimax approximations.

func I1e

func I1e(x float64) float64

I1e returns I1(x) e^-|x| (odd in x); I1e(±Inf) = ±0.

func In

func In(n int, x float64) float64

In returns the modified Bessel function of the first kind of integer order n, Iv(float64(n), x).

func Iv

func Iv(v, x float64) float64

Iv returns the modified Bessel function of the first kind of real order v, I_v(x) = sum_k (x/2)^(2k+v) / (k! Gamma(k+v+1)) (DLMF 10.25.2). For non-integer v it is real only for x >= 0 (NaN for x < 0); for integer v, I_v(-x) = (-1)^v I_v(x) and I_{-n} = I_n. Iv(v, 0) is 1 for v = 0, 0 for v > 0 or integer v, and ±Inf (the sign of 1/Gamma(v+1)) for non-integer v < 0; Iv(v, +Inf) = +Inf; Iv(+Inf, x) = 0, Iv(-Inf, x) = NaN; overflow gives +Inf.

Algorithm: Boost.Math bessel_ik.hpp (Temme's series and Steed's CF2 for K, CF1 and the Wronskian for I, power series for small x), carried exponentially scaled, with the large-x expansion DLMF 10.40.1 and the reflection I_{-v} = I_v + (2/pi) sin(v pi) K_v (DLMF 10.27.2). I0 and I1 serve the orders 0 and 1.

func Ive

func Ive(v, x float64) float64

Ive returns Iv(v, x) e^-|x|, with the conventions of Iv; Ive(v, ±Inf) is ±0 (the limit), never NaN, and the result does not overflow.

Algorithm: as Iv, computed scaled throughout.

func J0

func J0(x float64) float64

J0 returns the Bessel function of the first kind of order zero,

J0(x) = (1/pi) * integral_0^pi cos(x sin t) dt,

for real x (even in x). J0(0) = 1, J0(±Inf) = 0 (the limit; SciPy returns NaN), J0(NaN) = NaN.

Algorithm: below 100, one polynomial per cell about the zeros of J0, of J1 and the midpoints between them, with the value and the derivative at the centre in double-double (Harrison 2009); Hankel's expansion in modulus-phase form with the phase in double-double and exact (Payne-Hanek) reduction beyond. The error stays within a few ulps of the value, also next to the zeros.

func J1

func J1(x float64) float64

J1 returns the Bessel function of the first kind of order one, J1(x) = (1/pi) * integral_0^pi cos(t - x sin t) dt, for real x (odd in x). J1(0) = 0, J1(±Inf) = 0, J1(NaN) = NaN.

Algorithm: as J0 (cell polynomials about the zeros of J1 and of its derivative up to 100, modulus-phase Hankel expansion beyond).

func Jn

func Jn(n int, x float64) float64

Jn returns the Bessel function of the first kind of integer order n, for any real x: J_{-n}(x) = (-1)^n J_n(x) and J_n(-x) = (-1)^n J_n(x). Jn(n, 0) = 0 for n != 0, Jn(n, ±Inf) = 0, Jn(n, NaN) = NaN.

Algorithm: J0 and J1 for n = 0, 1; otherwise as Jv: the power series for small x, the double-double modulus-phase Hankel expansion for large x, and, for 0 < x < 100, a recurrence in hi + lo form (jn_yn_fast.go: upward from J0, J1 for n <= x, Miller's downward recurrence from the CF1 ratio for n > x) that answers only where its error bound is below 4 ulp (error at most 4.5 ulp, measured 1.04), and Steed's method in double-double otherwise (at the order n itself for x >= n, after backward recurrence from n for x < n). The plain forward recurrence of Cephes and Boost is not used: it loses the relative accuracy next to the zeros of J_n.

func JnZeros

func JnZeros(n int, nt int) []float64

JnZeros returns the first nt positive zeros of the Bessel function J_n, in increasing order. Since J_{-n} = (-1)^n J_n, a negative n gives the zeros of J_|n|. nt <= 0 gives an empty result. (x = 0, a zero of J_n for n != 0, is not counted, as in SciPy's jn_zeros.)

Algorithm: an initial estimate from McMahon's expansion (DLMF 10.21.19) for n = 0, and from Olver's uniform expansion (A&S 9.5.22, with the Airy zeros of A&S 10.4.94) for n >= 1, as in Boost.Math bessel_jy_zero.hpp; then Halley's iteration on Jn, with dJ_n/dx = J_{n-1} - (n/x) J_n and the second derivative from Bessel's equation, run to convergence.

func Jv

func Jv(v, x float64) float64

Jv returns the Bessel function of the first kind of real order v,

J_v(x) = sum_k (-1)^k (x/2)^(2k+v) / (k! Gamma(k+v+1))   (DLMF 10.2.2).

For integer v it is Jn(int(v), x). For non-integer v the function is real only for x >= 0: Jv(v, x < 0) = NaN. Jv(v, 0) is 0 for v > 0 and ±Inf (the sign of 1/Gamma(v+1)) for non-integer v < 0; Jv(v, +Inf) = 0; Jv(+Inf, x) = 0 and Jv(-Inf, x) = NaN; NaN in, NaN out.

Algorithm: after Boost.Math bessel_jy.hpp (N. M. Temme, J. Comput. Phys. 21, 1976, for Y at x <= 1/4; Steed's method with the continued fractions CF1 and CF2 of A. R. Barnett et al., Comput. Phys. Commun. 8, 1974, above; the power series for small x), carried in double-double; the large-x expansion in modulus-phase form (DLMF 10.18.17-18) with the phase in double-double and Payne-Hanek reduction. Negative orders use Steed's method directly when x >= |v|, else the reflection formula J_{-nu} = cos(nu pi) J_nu - sin(nu pi) Y_nu with exact sinPi/cosPi. Orders beyond 1e7 are supported only where the power series or the large-x expansion applies; elsewhere the result is NaN. The cost of Steed's method grows like x, so above x = 1e6 the large-x expansion is used even next to a zero. For x > 20 and |v| <= 40 Hankel's P, Q form with a certificate answers first (jv_hankel_fast.go, within 0.63 ulp, declining next to the zeros), and for 1 <= x <= 20 and a non-integer |v| <= 40.5 the two power series of J_v and J_-v in double-double (jv_mid_fast.go, within 0.5625 ulp, declining next to the zeros); at a half-integer order |v| <= 40.5 and x >= 1 what those decline is answered by the finite spherical-Bessel sums in double-double (jv_half_fast.go, within 0.55 ulp down to |J| = 1e-6 of the amplitude); otherwise the above.

func K0

func K0(x float64) float64

K0 returns the modified Bessel function of the second kind of order zero, K0(x) = integral_0^Inf cos(x sinh t) dt, for x > 0. K0(0) = +Inf, K0(+Inf) = 0, K0(x < 0) = NaN, K0(NaN) = NaN; underflows to 0 beyond x ~ 745.

Algorithm: Boost.Math bessel_k0.hpp's log-term form on (0, 1]; up to 512, e^-x times K0(x) e^x from degree-11 interpolants on 8 pieces per octave (fitted to mpmath), the product rounded once; DLMF 10.40.2 beyond.

func K0e

func K0e(x float64) float64

K0e returns K0(x) e^x for x > 0 (NaN for x < 0, +Inf at 0, 0 at +Inf).

func K1

func K1(x float64) float64

K1 returns the modified Bessel function of the second kind of order one for x > 0. K1(0) = +Inf, K1(+Inf) = 0, K1(x < 0) = NaN, K1(NaN) = NaN; overflows to +Inf for x below about 5.6e-309 and underflows beyond ~745.

Algorithm: Boost.Math bessel_k1.hpp double-precision minimax approximations.

func K1e

func K1e(x float64) float64

K1e returns K1(x) e^x for x > 0 (NaN for x < 0, +Inf at 0, 0 at +Inf).

func Kn

func Kn(n int, x float64) float64

Kn returns the modified Bessel function of the second kind of integer order n, Kv(float64(n), x).

Algorithm: for |n| >= 2 and 1 <= x < 512, the upward recurrence on K0e and K1e carried as hi + lo pairs, scaled by e^-x at the end (kn_fast.go: error bound 0.55 ulp); Kv elsewhere, and wherever the scaled chain overflows before reaching n (no fixed cap on n: about n = 929 at x near 512).

func Kv

func Kv(v, x float64) float64

Kv returns the modified Bessel function of the second kind of real order v, K_v(x) = (pi/2) (I_{-v}(x) - I_v(x)) / sin(v pi) (DLMF 10.27.4), even in v, defined for x > 0. Kv(v, 0) = +Inf, Kv(v, +Inf) = 0, Kv(v, x < 0) = NaN, Kv(±Inf, x) = +Inf; NaN in, NaN out. Overflow gives +Inf and underflow 0.

Algorithm: K0/K1 for v = 0, 1; integer v >= 2 with 1 <= x < 512 through the hi + lo recurrence of Kn (kn_fast.go); v >= 32 by Debye's uniform expansion and 2 < v < 32 with x >= 24 by the large-argument expansion plus recurrence, 0 < v < 32 with 1.5 <= x < 24 by CF2 plus recurrence, and with 2^-1000 <= x < 1.5 by Temme's series plus recurrence (kv_fast.go, kv_cf2_fast.go, kv_temme_fast.go, error <= 0.6 ulp); otherwise (K_v of 2^1020 or more, x below 2^-1000, v above 1100, x >= 1e6, the single series at x >= 24) Temme's series (x <= 0.5) or Steed's CF2 (in double-double) for the fractional order and forward recurrence in double-double (Boost.Math bessel_ik.hpp), or the large-x expansion DLMF 10.40.2 where it converges.

func Kve

func Kve(v, x float64) float64

Kve returns Kv(v, x) e^x, with the conventions of Kv (Kve(v, +Inf) = 0).

Algorithm: as Kv, computed scaled throughout.

func Log1pExp

func Log1pExp(x float64) float64

Log1pExp returns log(1 + exp(x)), the softplus function, accurate in both tails: x + log1p(exp(-x)) for x > 0 (no overflow) and log1p(exp(x)) for x <= 0 (full relative accuracy as it tends to exp(x)). e = exp(-|x|) comes from the float64 kernel as hi + lo, w = 1 + e is formed exactly and log w = log(w.hi) + w.lo/w.hi from the log kernel (2^-68 absolute); x is added with TwoSum before the single final rounding. Log1pExp(-Inf) = 0, Log1pExp(+Inf) = +Inf, NaN for NaN.

func Log1pExpTo added in v0.2.0

func Log1pExpTo(ζdst, ζx []float64)

Log1pExpTo sets dst[i] = Log1pExp(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func LogBeta

func LogBeta(a, b float64) float64

LogBeta returns log|B(a, b)|, the logarithm of the absolute value of the beta function B(a, b) = Γ(a)Γ(b)/Γ(a+b).

Special values: NaN if either argument is NaN; +Inf when a or b is 0 or a negative integer (and a+b is not); -Inf when B(a, b) = 0 (a+b a non-positive integer, a and b not).

Algorithm: for positive arguments with max(a, b) >= 10, log Γ(b) - log Γ(a+b) is evaluated as a single cancellation-free expression from Stirling's series (logGammaDiff) and added to log Γ(a) in double-double; otherwise the three log-gammas are combined in double-double with the rounding of a+b corrected to first order. Negative arguments go through LogGamma with the same correction.

func LogBetaTo added in v0.2.0

func LogBetaTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

LogBetaTo sets dst[i] = LogBeta(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func LogExpit

func LogExpit(x float64) float64

LogExpit returns log(Expit(x)) = -log(1 + exp(-x)) without underflow for large negative x (where it is x - log1p(exp(x)) ~ x) and with full relative accuracy for large positive x (where it is -exp(-x)).

func LogFactorial

func LogFactorial(n float64) float64

LogFactorial returns log(n!) = log Γ(n+1) for n >= 0 without forming n+1 (which would round for non-integer n). NaN for n < 0 or NaN; +Inf for n = +Inf.

func LogGamma

func LogGamma(x float64) float64

LogGamma returns log|Γ(x)|, the natural logarithm of the absolute value of the gamma function.

Special values: LogGamma(±Inf) = +Inf; LogGamma(x) = +Inf at the poles x = 0, -1, -2, ...; NaN for NaN; +Inf for x > 2.556348e305 (overflow).

Algorithm: near the zeros at x = 1 and x = 2 the result is log1p of Γ(1+t)-1 computed from the same fitted core as Gamma (gamma1pm1), so the relative accuracy holds as the result goes to zero; on (2.5, 10) it is the logarithm of Gamma's double-double value; from 10 Stirling's series (DLMF 5.11.1) is summed in double-double. For x < 0 it is log|Γ(1+t)| - log|t Π(x+k)| with exact factors for x > -34, and the reflection formula log π - log|x sin πx| - log Γ(-x) below.

func LogGammaSign

func LogGammaSign(x float64) (float64, int)

LogGammaSign returns log|Γ(x)| and the sign of Γ(x) (+1 or -1), like math.Lgamma. At a pole the sign is +1, except for -0 where it is -1.

func LogGammaTo added in v0.2.0

func LogGammaTo(ζdst, ζx []float64)

LogGammaTo sets dst[i] = LogGamma(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func LogNdtr

func LogNdtr(x float64) float64

LogNdtr returns log Φ(x), the logarithm of the standard normal cdf, accurate in both tails: for x -> -Inf it is -x²/2 - log(-x) - ... with no underflow, for large x it is -Φ(-x) with full relative accuracy.

Special values: LogNdtr(-Inf) = -Inf, LogNdtr(+Inf) = 0, LogNdtr(NaN) = NaN.

Algorithm: for x <= -1/sqrt(2), log erfcx(z) - z² - log 2 with z = -x/sqrt(2) in double-double; for x >= 1/sqrt(2), log1p(-Φ(-x)) with Φ(-x) in double-double; in between, the logarithm of Φ in double-double.

func LogNdtrTo added in v0.2.0

func LogNdtrTo(ζdst, ζx []float64)

LogNdtrTo sets dst[i] = LogNdtr(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func LogSoftmax

func LogSoftmax(dst, x []float64) []float64

LogSoftmax writes x_i - LogSumExp(x) into dst (allocated when nil or too short) and returns it; dst may be x itself. The difference is formed as (x_i - m) - log(Σ exp(x_j - m)) with x_i - m exact as a sum of two floats and the logarithm in double-double, so it never cancels (both terms are negative): the relative error is at most logSoftmaxErr = 5·2^-53 (softmax_fast.go) unless the sum of the exponentials of the others is below about n·2^-1000, where terms in the subnormal range carry the relative accuracy away (as for LogSumExp). LogSoftmaxExact is the double-double evaluation, about 3.7 times slower and bit-identical on every architecture.

func LogSoftmaxExact added in v0.20.0

func LogSoftmaxExact(dst, x []float64) []float64

LogSoftmaxExact is LogSoftmax with x_i - lse in double-double, lse = m + log1p(Σ_{x_j < m} exp(x_j - m)): half an ulp where the conditions in the comment above hold. It writes into dst (allocated when nil or too short) and returns it; dst may be x itself.

func LogSumExp

func LogSumExp(x []float64) float64

LogSumExp returns log(Σ exp(x_i)) without overflow or underflow: the maximum m is factored out and log1p of Σ_{i≠argmax} exp(x_i - m) is taken by float64 kernels (x_i - m exact as a sum of two floats, floats.Exp, a compensated sum, log1p in double-double: softmax_fast.go). The relative error is at most logSumExpErr = 4·2^-53 whenever |LogSumExp| is at least the log L of the sum (all m >= 0, most m < 0) AND no term that matters is subnormal: when the sum of the exponentials of the others is below about n·2^-1000 the terms in the subnormal range (each with an absolute error up to 2^-1075) can carry the relative accuracy away (x = [0, 2^20 × -720]). Where m + L cancels (m in (-2L, 0)) the double-double evaluation (LogSumExpExact) answers, whose relative error is not bounded there: log1pDD's 2^-90 is magnified by L/|LogSumExp| (x = [-ln 2, -ln 2]); the absolute error stays below 2^-84 L + 2^-53 |LogSumExp|. Both regions are open; LogSumExpExact has the same two limits. It returns -Inf for an empty slice or when every x_i is -Inf, +Inf if any x_i is +Inf, NaN if any is NaN.

func LogSumExpExact added in v0.20.0

func LogSumExpExact(x []float64) float64

LogSumExpExact is LogSumExp with the exponentials, the sum and log1p in double-double: within half an ulp where the conditions in the comment above hold (it has no guard: where m + log Σ cancels it carries log1pDD's 2^-90 relative error magnified, as the fast path's fallback does). It returns -Inf for an empty slice or when every x_i is -Inf, +Inf if any x_i is +Inf, NaN if any is NaN.

func Logit

func Logit(p float64) float64

Logit returns the log-odds log(p/(1-p)), the inverse of Expit, for 0 <= p <= 1: -Inf at 0, +Inf at 1, NaN outside or for NaN.

Algorithm: for q = min(p, 1-p) (1-p is exact for p >= 1/2) in [2^-29, 1/2), Logit(q) = (q - 1/2) G(q) with G fitted directly on 8 pieces per octave of q (logitPieces; the piece is the top 15 bits of q's float64 pattern, as for Ndtri), G and q - 1/2 each carried as hi + lo and multiplied before the single rounding: no logarithm or division, and the result is accurate where it crosses zero. Logit(p) = -Logit(1-p) for p > 1/2. Below 2^-29 (and at the special values), logitTail: 1-p is formed exactly (TwoSum), the quotient p/(1-p) is carried as hi + lo through its FMA residual and log of it comes from the log kernel.

func LogitTo added in v0.2.0

func LogitTo(ζdst, ζx []float64)

LogitTo sets dst[i] = Logit(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Ndtr

func Ndtr(x float64) float64

Ndtr returns the standard normal cumulative distribution function Φ(x) = (1/sqrt(2π)) ∫_{-∞}^x exp(-t²/2) dt = erfc(-x/sqrt 2)/2.

Special values: Φ(-Inf) = 0, Φ(+Inf) = 1, Φ(NaN) = NaN.

Algorithm: the argument -x/sqrt(2) is carried in double-double into erfc (erfcMant), so the lower tail keeps full relative accuracy down to the subnormal range; the rounding of x/sqrt(2) that limits implementations computing erfc(-x*M_SQRT1_2) (an error growing like x² ulp) does not occur.

func NdtrTo added in v0.2.0

func NdtrTo(ζdst, ζx []float64)

NdtrTo sets dst[i] = Ndtr(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Ndtri

func Ndtri(p float64) float64

Ndtri returns the inverse of the standard normal cdf: Ndtri(Ndtr(x)) = x, for 0 <= p <= 1.

Special values: Ndtri(0) = -Inf, Ndtri(1) = +Inf, Ndtri(1/2) = 0; NaN outside [0, 1] or for NaN.

Algorithm: for q = min(p, 1-p) (1-p is exact for p >= 1/2) in [2^-29, 1/2], Ndtri(q) = (q - 1/2) G(q) with G fitted directly on 8 pieces per octave of q (ndtriPieces; the piece is the top 15 bits of q's float64 pattern), G and q - 1/2 each carried as hi + lo and multiplied before the single rounding; no logarithm, square root or division. Ndtri(p) = -Ndtri(1-p) for p > 1/2. Below 2^-28 (and for the special values), ndtriSlow: -sqrt(2) erfcinv(2p) with erfcinv from ErfcInv's fitted approximations as hi + lo and sqrt(2) as a double-double, multiplied before the single rounding (so p in the far lower tail, down to subnormals, keeps full relative accuracy).

func NdtriTo added in v0.2.0

func NdtriTo(ζdst, ζx []float64)

NdtriTo sets dst[i] = Ndtri(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Perm

func Perm(n, k int64) float64

Perm returns the number of k-permutations of n, n!/(n-k)! = n (n-1) ... (n-k+1), as a float64: 0 when k < 0, k > n or n < 0 (as SciPy's perm with exact=False).

Algorithm: the product in double-double for k <= 64 (exact below 2^53, correctly rounded above); beyond, exp(log Γ(n+1) - log Γ(n-k+1)) with the difference formed without cancellation (logGammaDiff) when n-k+1 is at least 10 and k <= n-k+1.

func PermTo added in v0.21.0

func PermTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

PermTo sets dst[i] = Perm(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Polygamma

func Polygamma(n int, x float64) float64

Polygamma returns the polygamma function ψ⁽ⁿ⁾(x), the n-th derivative of the digamma function; Polygamma(0, x) = Digamma(x).

Special values: NaN for n < 0, NaN or x = -Inf; ψ⁽ⁿ⁾(+Inf) = 0 (n >= 1); at the poles x = 0, -1, -2, ... the value is (-1)^(n+1) Inf.

Algorithm: ψ⁽ⁿ⁾(x) = (-1)^(n+1) n! ζ(n+1, x) (DLMF 25.11.12), with ζ from the double-double Euler–Maclaurin summation and n! in double-double, the two combined as scaled values so that the product overflows only when the result does. From n = 600000 (polygammaHugeN), where n! and x^-(n+1) leave the range of the scaled exponential, the product is formed from log n! - (n+1) log x in double-double (polygammaHuge): ±Inf or ±0 outside the narrow band of x near n/e where the result is finite, never NaN.

func Softmax

func Softmax(dst, x []float64) []float64

Softmax writes exp(x_i) / Σ_j exp(x_j) into dst (allocated when nil or too short) and returns it; dst may be x itself (any other overlap is not supported). The maximum is subtracted first, x_i - m exactly as a sum of two floats, the exponentials come from floats.Exp and the sum is compensated (softmax_fast.go): the relative error of every result at or above 2^-1022 is at most softmaxErr = 8·2^-53 (4 ulp at most; below 2^-1022 the absolute error is under 2^-1073 plus the same fraction), and about 1.5 ulp in practice. For the correctly rounded quotient, bit-identical on every architecture, use SoftmaxExact (about 4.5 times slower). An empty x gives an empty result; if x holds NaN or +Inf, or only -Inf, the results are NaN (as SciPy's softmax).

func SoftmaxExact added in v0.20.0

func SoftmaxExact(dst, x []float64) []float64

SoftmaxExact is Softmax with every exponential, the sum and the quotient in double-double: the result is the correctly rounded exp(x_i) / Σ_j exp(x_j) up to the conditions in the comment above (no subnormal terms that matter). Use it where the last bit matters or where the bits must not depend on the architecture; Softmax is several times faster, within 8·2^-53 relative. It writes into dst (allocated when nil or too short) and returns it; dst may be x itself. An empty x gives an empty result; if x holds NaN or +Inf the results are NaN (as SciPy's softmax).

func TrigammaTo added in v0.2.0

func TrigammaTo(ζdst, ζx []float64)

TrigammaTo sets dst[i] = Polygamma(1, x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Xlog1py

func Xlog1py(x, y float64) float64

Xlog1py returns x*log1p(y), defined as 0 when x = 0 and y is not NaN, as SciPy's xlog1py, with log1p(y) in double-double: one rounding.

func Xlog1pyTo added in v0.21.0

func Xlog1pyTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

Xlog1pyTo sets dst[i] = Xlog1py(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Xlogy

func Xlogy(x, y float64) float64

Xlogy returns x*log(y), defined as 0 when x = 0 and y is not NaN (so that 0*log(0) terms of entropies vanish), as SciPy's xlogy. log(y) comes from the log kernel as hi + lo (2^-68 absolute) and the product is rounded once by an FMA. The kernel is written out here (the same operations as logHL, without its subnormal branch): at a few ns per call, the spill of a call under Go's ABI is a tenth of the cost.

func XlogyTo added in v0.2.0

func XlogyTo(ζdst, ζa, ζb []float64, ζsa, ζsb int)

XlogyTo sets dst[i] = Xlogy(a[i*sa], b[i*sb]) for i < len(dst) (a stride of 0 repeats a single value), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

func Y0

func Y0(x float64) float64

Y0 returns the Bessel function of the second kind of order zero, Y0(x) = (2/pi)(log(x/2) + gamma) J0(x) + ..., defined for x > 0. Y0(0) = -Inf, Y0(+Inf) = 0, Y0(x < 0) = NaN, Y0(NaN) = NaN.

Algorithm: the series DLMF 10.8.2 for x <= 0.6; cell polynomials about the zeros of Y0 and Y1 and the midpoints between them up to 100 (cells split further near the logarithmic singularity at 0); the modulus-phase Hankel expansion beyond.

func Y1

func Y1(x float64) float64

Y1 returns the Bessel function of the second kind of order one, defined for x > 0. Y1(0) = -Inf, Y1(+Inf) = 0, Y1(x < 0) = NaN, Y1(NaN) = NaN; Y1(x) overflows to -Inf for x below about 3.5e-309.

Algorithm: as Y0 (series DLMF 10.8.1 for x <= 1.5).

func Yn

func Yn(n int, x float64) float64

Yn returns the Bessel function of the second kind of integer order n for x >= 0, with Y_{-n}(x) = (-1)^n Y_n(x). Yn(n, 0) = -Inf (+Inf for odd negative n), Yn(n, x < 0) = NaN, Yn(n, +Inf) = 0, Yn(n, NaN) = NaN; large negative values overflow to -Inf.

Algorithm: Y0 and Y1 for n = 0, 1; otherwise, for 0 < x < 100, the upward recurrence from Y0 and Y1 as hi + lo pairs (jn_yn_fast.go), which answers only where its error bound is below 6 ulp (error at most 6.5 ulp, measured 1.40); else as Yv (Temme's series and forward recurrence for small x, Steed's method in double-double, the modulus-phase Hankel expansion for large x).

func Yv

func Yv(v, x float64) float64

Yv returns the Bessel function of the second kind of real order v, Y_v(x) = (J_v(x) cos(v pi) - J_{-v}(x)) / sin(v pi) (DLMF 10.2.3), and its limit for integer v (Yn). It is real only for x >= 0: Yv(v, x < 0) = NaN. Yv(v, 0) = -Inf for v > 0 or v = 0; for v < 0 it is the limit -Inf*cos(v pi) (0 at half-integers, where Y_{-n-1/2} = ±J_{n+1/2}); Yv(v, +Inf) = 0; Yv(+Inf, x > 0) = -Inf, Yv(-Inf, x) = NaN; NaN in, NaN out. Large negative values overflow to -Inf.

Algorithm: as Jv; for small x and non-integer v, the two power series of the definition where the second is negligible.

func Zeta

func Zeta(s float64) float64

Zeta returns the Riemann zeta function ζ(s) for real s.

Special values: ζ(1) = +Inf, ζ(+Inf) = 1, ζ(0) = -1/2, ζ(-2k) = 0 for integers k >= 1; NaN for NaN and -Inf. ζ overflows to ±Inf for s < -260 (roughly).

Algorithm: on [-1, 6], ζ(s) = 1/(s-1) + R(s) with R entire and fitted to mpmath (zz_zetaPieces_coeffs.go), so the pole is exact; on (6, 64), 1 + g(s) 2^-s with g fitted to mpmath; for s < -1 the reflection formula ζ(s) = 2^s π^(s-1) sin(πs/2) Γ(1-s) ζ(1-s) (DLMF 25.4.1) with the powers and the gamma function combined in double-double before one exponential and sin(πs/2) reduced exactly, so the trivial zeros are exact and nearby values keep their relative accuracy.

func ZetaTo added in v0.2.0

func ZetaTo(ζdst, ζx []float64)

ZetaTo sets dst[i] = Zeta(x[i]) for i < len(dst) (x at least as long), bit-identical to the scalar function, in one loop with no call per element: the loop of the nd functions.

Types

This section is empty.

Source Files

Directories

Path Synopsis
Package cmplx is a drop-in superset of the standard library's math/cmplx: the same names and signatures, with the special values of C11 Annex G (ISO/IEC 9899:2011 G.5.1, G.6) and an accuracy contract measured per component against mpmath (docs/design-complex.md).
Package cmplx is a drop-in superset of the standard library's math/cmplx: the same names and signatures, with the special values of C11 Annex G (ISO/IEC 9899:2011 G.5.1, G.6) and an accuracy contract measured per component against mpmath (docs/design-complex.md).
Package dd is a minimal double-double ("float128-lite") arithmetic used where a special function must carry more than 53 bits through an intermediate step: the argument of an exponential, a long product, a cancellation.
Package dd is a minimal double-double ("float128-lite") arithmetic used where a special function must carry more than 53 bits through an intermediate step: the argument of an exponential, a long product, a cancellation.
internal
libm
Package libm holds the functions of Go's package math whose results depend on the architecture, in a form whose results do not.
Package libm holds the functions of Go's package math whose results depend on the architecture, in a form whose results do not.
Package nd applies the special functions of package special element-wise to go-ndarray arrays (github.com/go-ndarray/ndarray).
Package nd applies the special functions of package special element-wise to go-ndarray arrays (github.com/go-ndarray/ndarray).
tools module

Jump to

Keyboard shortcuts

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