mathext: speed up Digamma for large negative arguments
Before the Digamma function could be quite slow for large negative
arguments because it had to do use the recurrence relation thousands
of times. This avoids that by instead using the reflection formula. It
also adds some explicit checks for special cases (Inf, NaN, poles).
MODIFY
mathext/digamma_test.go
mathext/digamma_test.go
@@ -9,15 +9,24 @@ import (
"testing"
)
+var result float64
+
func TestDigamma(t *testing.T) {
for i, test := range []struct {
x, want float64
}{
// Results computed using WolframAlpha.
+ {0.0, math.Inf(-1)},
+ {math.Copysign(0.0, -1.0), math.Inf(1)},
+ {math.Inf(1), math.Inf(1)},
+ {math.Inf(-1), math.NaN()},
+ {math.NaN(), math.NaN()},
+ {-1.0, math.NaN()},
{-100.5, 4.615124601338064117341315601525112558522917517910505881343},
{.5, -1.96351002602142347944097633299875556719315960466043},
{10, 2.251752589066721107647456163885851537211808918028330369448},
{math.Pow10(20), 46.05170185988091368035482909368728415202202143924212618733},
+ {-1.111111111e9, 30.497454343508262861},
} {
if got := Digamma(test.x); math.Abs(got-test.want) > 1e-10 {
@@ -25,3 +34,11 @@ func TestDigamma(t *testing.T) {
}
}
}
+
+func BenchmarkDigamma(b *testing.B) {
+ var r float64
+ for i := 0; i < b.N; i++ {
+ r = Digamma(-1.111111111e9)
+ }
+ result = r
+}

mathext: speed up Digamma for large negative arguments

Before the Digamma function could be quite slow for large negative
arguments because it had to do use the recurrence relation thousands
of times. This avoids that by instead using the reflection formula. It
also adds some explicit checks for special cases (Inf, NaN, poles).