This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrapezoidal_test.go
82 lines (78 loc) · 1.8 KB
/
trapezoidal_test.go
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
// 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"
"github.com/gonum/floats"
)
func TestTrapezoidal(t *testing.T) {
const N = 1e6
x := floats.Span(make([]float64, N), 0, 1)
for i, test := range []struct {
x []float64
f func(x float64) float64
want float64
}{
{
x: x,
f: func(x float64) float64 { return x },
want: 0.5,
},
{
x: floats.Span(make([]float64, N), -1, 1),
f: func(x float64) float64 { return x },
want: 0,
},
{
x: x,
f: func(x float64) float64 { return x + 10 },
want: 10.5,
},
{
x: x,
f: func(x float64) float64 { return 3*x*x + 10 },
want: 11,
},
{
x: x,
f: func(x float64) float64 { return math.Exp(x) },
want: 1.7182818284591876,
},
{
x: floats.Span(make([]float64, N), 0, math.Pi),
f: func(x float64) float64 { return math.Cos(x) },
want: 0,
},
{
x: floats.Span(make([]float64, N), 0, 2*math.Pi),
f: func(x float64) float64 { return math.Cos(x) },
want: 0,
},
{
x: floats.Span(make([]float64, N*10), 0, math.Pi),
f: func(x float64) float64 { return math.Sin(x) },
want: 2,
},
{
x: floats.Span(make([]float64, N*10), 0, 0.5*math.Pi),
f: func(x float64) float64 { return math.Sin(x) },
want: 1,
},
{
x: floats.Span(make([]float64, N), 0, 2*math.Pi),
f: func(x float64) float64 { return math.Sin(x) },
want: 0,
},
} {
y := make([]float64, len(test.x))
for i, v := range test.x {
y[i] = test.f(v)
}
v := Trapezoidal(test.x, y)
if !floats.EqualWithinAbs(v, test.want, 1e-12) {
t.Errorf("test #%d: got=%v want=%v\n", i, v, test.want)
}
}
}