Files
gonum/mathext/digamma.go
Dan Kortschak 5f0141ca4c all: run gofmt and generate all packages
Changes made in dsp/fourier/internal/fftpack break the formatting used
there, so these are reverted. There will be complaints in CI.

[git-generate]
gofmt -w .
go generate gonum.org/v1/gonum/blas
go generate gonum.org/v1/gonum/blas/gonum
go generate gonum.org/v1/gonum/unit
go generate gonum.org/v1/gonum/unit/constant
go generate gonum.org/v1/gonum/graph/formats/dot
go generate gonum.org/v1/gonum/graph/formats/rdf
go generate gonum.org/v1/gonum/stat/card

git checkout -- dsp/fourier/internal/fftpack
2022-08-06 07:05:17 +09:30

46 lines
1.1 KiB
Go

// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mathext
import (
"math"
)
// Digamma returns the logorithmic derivative of the gamma function at x.
//
// ψ(x) = d/dx (Ln (Γ(x)).
func Digamma(x float64) float64 {
// This is adapted from
// http://web.science.mq.edu.au/~mjohnson/code/digamma.c
var result float64
switch {
case math.IsNaN(x), math.IsInf(x, 1):
return x
case math.IsInf(x, -1):
return math.NaN()
case x == 0:
return math.Copysign(math.Inf(1), -x)
case x < 0:
if x == math.Floor(x) {
return math.NaN()
}
// Reflection formula, http://dlmf.nist.gov/5.5#E4
_, r := math.Modf(x)
result = -math.Pi / math.Tan(math.Pi*r)
x = 1 - x
}
for ; x < 7; x++ {
// Recurrence relation, http://dlmf.nist.gov/5.5#E2
result -= 1 / x
}
x -= 0.5
xx := 1 / x
xx2 := xx * xx
xx4 := xx2 * xx2
// Asymptotic expansion, http://dlmf.nist.gov/5.11#E2
result += math.Log(x) + (1.0/24.0)*xx2 - (7.0/960.0)*xx4 + (31.0/8064.0)*xx4*xx2 - (127.0/30720.0)*xx4*xx4
return result
}