File: trapezoidal_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 (74 lines) | stat: -rw-r--r-- 2,031 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
// 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 integrate

import (
	"math"
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/integrate/testquad"
)

func TestTrapezoidal(t *testing.T) {
	t.Parallel()
	rnd := rand.New(rand.NewSource(1))
	for i, test := range []struct {
		integral testquad.Integral
		n        int
		tol      float64
	}{
		{integral: testquad.Constant(0), n: 2, tol: 0},
		{integral: testquad.Constant(0), n: 10, tol: 0},
		{integral: testquad.Poly(0), n: 2, tol: 1e-14},
		{integral: testquad.Poly(0), n: 10, tol: 1e-14},
		{integral: testquad.Poly(1), n: 2, tol: 1e-14},
		{integral: testquad.Poly(1), n: 10, tol: 1e-14},
		{integral: testquad.Poly(2), n: 1e5, tol: 1e-8},
		{integral: testquad.Poly(3), n: 1e5, tol: 1e-8},
		{integral: testquad.Poly(4), n: 1e5, tol: 1e-7},
		{integral: testquad.Poly(5), n: 1e5, tol: 1e-7},
		{integral: testquad.Sin(), n: 1e5, tol: 1e-11},
		{integral: testquad.XExpMinusX(), n: 1e5, tol: 1e-10},
		{integral: testquad.Sqrt(), n: 1e5, tol: 1e-8},
		{integral: testquad.ExpOverX2Plus1(), n: 1e5, tol: 1e-10},
	} {
		n := test.n
		a := test.integral.A
		b := test.integral.B

		x := jitterSpan(n, a, b, rnd)
		y := make([]float64, n)
		for i, xi := range x {
			y[i] = test.integral.F(xi)
		}

		got := Trapezoidal(x, y)

		want := test.integral.Value
		diff := math.Abs(got - want)
		if diff > test.tol {
			t.Errorf("Test #%d: %v, n=%v: unexpected result; got=%v want=%v diff=%v",
				i, test.integral.Name, n, got, want, diff)
		}
	}
}

func jitterSpan(n int, a, b float64, rnd *rand.Rand) []float64 {
	dx := (b - a) / float64(n-1)
	x := make([]float64, n)
	x[0] = a
	for i := 1; i < n-1; i++ {
		// Set x[i] to its regular location.
		x[i] = a + float64(i)*dx
		// Generate a random number in [-1,1).
		jitter := 2*rnd.Float64() - 1
		// Jitter x[i] without crossing over its neighbors.
		x[i] += 0.4 * jitter * dx
	}
	x[n-1] = b
	return x
}