File: digamma_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (51 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 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"

	"gonum.org/v1/gonum/floats/scalar"
)

var result float64

func TestDigamma(t *testing.T) {
	t.Parallel()
	const tol = 1e-10

	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},
		{0.5, -1.96351002602142347944097633299875556719315960466043},
		{10, 2.251752589066721107647456163885851537211808918028330369448},
		{math.Pow10(20), 46.05170185988091368035482909368728415202202143924212618733},
		{-1.111111111e9, math.NaN()},
		{1.46, -0.001580561987083417676105544023567034348339520110000},
	} {

		got := Digamma(test.x)
		if !(math.IsNaN(got) && math.IsNaN(test.want)) && !scalar.EqualWithinAbsOrRel(got, test.want, tol, tol) {
			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
}