mirror of
https://github.com/gonum/gonum.git
synced 2025-10-16 20:20:41 +08:00

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).
45 lines
1.1 KiB
Go
45 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"
|
|
"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 {
|
|
t.Errorf("test %d Digamma(%g) failed: got %g want %g", i, test.x, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkDigamma(b *testing.B) {
|
|
var r float64
|
|
for i := 0; i < b.N; i++ {
|
|
r = Digamma(-1.111111111e9)
|
|
}
|
|
result = r
|
|
}
|