/
infinite_domain_test.go
99 lines (79 loc) · 2.25 KB
/
infinite_domain_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goint
import (
"fmt"
"math"
"testing"
)
/* Computes the integral of f over [a, b], and determines if the
/* result is within h of the correct value. Returns (msg, ok), where
/* ok is true if there was an acceptable amount of error. If ok is
/* false, msg contains a string describing the error, otherwise it is
/* empty. */
func test_integral(f Function, a, b, h, correct float64) (string, bool) {
int := Integrate(f, a, b, h)
err := math.Abs(int - correct)
if err > h {
msg := fmt.Sprintf("%.3g differs from %.3g by more than %.3g", int, correct, h)
return msg, false
}
return "", true
}
/* Test integrating e^x over infinite domains */
func TestExponential(t *testing.T) {
const (
h = 1e-8
)
f := math.Exp
// Check (-Inf, 0]; should be 1
if msg, ok := test_integral(f, math.Inf(-1), 0, h, 1); !ok {
t.Error(msg)
}
// Check [0, +Inf); should be +Inf
if msg, ok := test_integral(f, 0, math.Inf(1), h, math.Inf(1)); !ok {
t.Error(msg)
}
// Now check that these results hold for -f
f = func(x float64) float64 { return -math.Exp(x) }
// Check (-Inf, 0]; should be -1
if msg, ok := test_integral(f, math.Inf(-1), 0, h, -1); !ok {
t.Error(msg)
}
// Check [0, +Inf); should be -Inf
if msg, ok := test_integral(f, 0, math.Inf(1), h, math.Inf(-1)); !ok {
t.Error(msg)
}
}
/* Test integrating e^(-x) over infinite domains*/
func TestNegativeExponential(t *testing.T) {
const (
h = 1e-8
correct = 1
)
f := func(x float64) float64 { return math.Exp(-x) }
// Check (-Inf, 0]; should be +Inf
if msg, ok := test_integral(f, math.Inf(-1), 0, h, math.Inf(1)); !ok {
t.Error(msg)
}
// Check [0, +Inf); should be 1
if msg, ok := test_integral(f, 0, math.Inf(1), h, 1); !ok {
t.Error(msg)
}
// Now check that these results hold for -f
f = func(x float64) float64 { return -math.Exp(-x) }
// Check (-Inf, 0]; should be -Inf
if msg, ok := test_integral(f, math.Inf(-1), 0, h, math.Inf(-1)); !ok {
t.Error(msg)
}
// Check [0, +Inf); should be -1
if msg, ok := test_integral(f, 0, math.Inf(1), h, -1); !ok {
t.Error(msg)
}
}
func TestNormal(t *testing.T) {
f := func(x float64) float64 {
return math.Exp(-x*x/2) / math.Sqrt(2*math.Pi)
}
if msg, ok := test_integral(f, math.Inf(-1), 0, 1e-6, .5); !ok {
t.Error(msg)
}
}