File: quad_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 (128 lines) | stat: -rw-r--r-- 3,685 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright ©2015 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 quad

import (
	"math"
	"testing"

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

func TestFixed(t *testing.T) {
	t.Parallel()
	for i, test := range []struct {
		f        func(float64) float64
		min, max float64
		n        []int
		tol      []float64
		ans      float64
	}{
		// Tolerances determined from intuition and a bit of post-hoc tweaking.
		{
			f:   func(x float64) float64 { return math.Exp(x) },
			min: -3,
			max: 5,
			n:   []int{3, 4, 6, 7, 15, 16, 300, 301},
			tol: []float64{5e-2, 5e-3, 5e-6, 1e-7, 1e-14, 1e-14, 1e-14, 1e-14},
			ans: math.Exp(5) - math.Exp(-3),
		},
		{
			f:   distuv.UnitNormal.Prob,
			min: math.Inf(-1),
			max: math.Inf(1),
			n:   []int{15, 16, 50, 51, 300, 301},
			tol: []float64{5e-3, 1e-3, 1e-7, 1e-7, 1e-14, 1e-14},
			ans: 1,
		},
		{
			f:   func(x float64) float64 { return math.Exp(-x) },
			min: 5,
			max: math.Inf(1),
			n:   []int{15, 16, 50, 51, 300, 301},
			tol: []float64{5e-3, 1e-3, 1e-7, 1e-7, 1e-14, 1e-14},
			ans: math.Exp(-5),
		},
		{
			f:   func(x float64) float64 { return math.Exp(x) },
			min: math.Inf(-1),
			max: -5,
			n:   []int{15, 16, 50, 51, 300, 301},
			tol: []float64{5e-3, 1e-3, 1e-7, 1e-7, 1e-14, 1e-14},
			ans: math.Exp(-5),
		},
		{
			f:   func(x float64) float64 { return math.Exp(x) },
			min: 3,
			max: 3,
			n:   []int{15, 16, 50, 51, 300, 301},
			tol: []float64{0, 0, 0, 0, 0, 0},
			ans: 0,
		},
	} {
		for j, n := range test.n {
			ans := Fixed(test.f, test.min, test.max, n, nil, 0)
			if !scalar.EqualWithinAbsOrRel(ans, test.ans, test.tol[j], test.tol[j]) {
				t.Errorf("Case %d, n = %d: Mismatch. Want %v, got %v", i, n, test.ans, ans)
			}
			ans2 := Fixed(test.f, test.min, test.max, n, nil, 3)
			if !scalar.EqualWithinAbsOrRel(ans2, test.ans, test.tol[j], test.tol[j]) {
				t.Errorf("Case %d, n = %d: Mismatch concurrent. Want %v, got %v", i, n, test.ans, ans)
			}
		}
	}
}

// legendreNonSingle wraps Legendre but does not implement FixedLocationSingle.
type legendreNonSingle struct {
	Legendre Legendre
}

func (l legendreNonSingle) FixedLocations(x, weight []float64, min, max float64) {
	l.Legendre.FixedLocations(x, weight, min, max)
}

func TestFixedNonSingle(t *testing.T) {
	t.Parallel()
	// TODO(btracey): Add tests with infinite bounds when we have native support
	// for indefinite integrals.
	for i, test := range []struct {
		f        func(float64) float64
		min, max float64
		n        []int
		tol      []float64
		ans      float64
	}{
		// Tolerances determined from intuition and a bit of post-hoc tweaking.
		{
			f:   func(x float64) float64 { return math.Exp(x) },
			min: -3,
			max: 5,
			n:   []int{3, 4, 6, 7, 15, 16, 300, 301},
			tol: []float64{5e-2, 5e-3, 5e-6, 1e-7, 1e-14, 1e-14, 1e-14, 1e-14},
			ans: math.Exp(5) - math.Exp(-3),
		},
		{
			f:   func(x float64) float64 { return math.Exp(x) },
			min: 3,
			max: 3,
			n:   []int{3, 4, 6, 7, 15, 16, 300, 301},
			tol: []float64{0, 0, 0, 0, 0, 0, 0, 0},
			ans: 0,
		},
	} {
		for j, n := range test.n {
			ans := Fixed(test.f, test.min, test.max, n, legendreNonSingle{}, 0)
			if !scalar.EqualWithinAbsOrRel(ans, test.ans, test.tol[j], test.tol[j]) {
				t.Errorf("Case = %d, n = %d: Mismatch. Want %v, got %v", i, n, test.ans, ans)
			}
			ans2 := Fixed(test.f, test.min, test.max, n, legendreNonSingle{}, 3)
			if !scalar.EqualWithinAbsOrRel(ans2, test.ans, test.tol[j], test.tol[j]) {
				t.Errorf("Case = %d, n = %d: Mismatch concurrent. Want %v, got %v", i, n, test.ans, ans)
			}
		}
	}
}