github.com/gopherd/gonum@v0.0.4/mathext/digamma_test.go (about)

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mathext
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  
    11  	"github.com/gopherd/gonum/floats/scalar"
    12  )
    13  
    14  var result float64
    15  
    16  func TestDigamma(t *testing.T) {
    17  	t.Parallel()
    18  	const tol = 1e-10
    19  
    20  	for i, test := range []struct {
    21  		x, want float64
    22  	}{
    23  		// Results computed using WolframAlpha.
    24  		{0.0, math.Inf(-1)},
    25  		{math.Copysign(0.0, -1.0), math.Inf(1)},
    26  		{math.Inf(1), math.Inf(1)},
    27  		{math.Inf(-1), math.NaN()},
    28  		{math.NaN(), math.NaN()},
    29  		{-1.0, math.NaN()},
    30  		{-100.5, 4.615124601338064117341315601525112558522917517910505881343},
    31  		{0.5, -1.96351002602142347944097633299875556719315960466043},
    32  		{10, 2.251752589066721107647456163885851537211808918028330369448},
    33  		{math.Pow10(20), 46.05170185988091368035482909368728415202202143924212618733},
    34  		{-1.111111111e9, math.NaN()},
    35  		{1.46, -0.001580561987083417676105544023567034348339520110000},
    36  	} {
    37  
    38  		got := Digamma(test.x)
    39  		if !(math.IsNaN(got) && math.IsNaN(test.want)) && !scalar.EqualWithinAbsOrRel(got, test.want, tol, tol) {
    40  			t.Errorf("test %d Digamma(%g) failed: got %g want %g", i, test.x, got, test.want)
    41  		}
    42  	}
    43  }
    44  
    45  func BenchmarkDigamma(b *testing.B) {
    46  	var r float64
    47  	for i := 0; i < b.N; i++ {
    48  		r = Digamma(-1.111111111e9)
    49  	}
    50  	result = r
    51  }