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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright ©2014 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 fd
import (
"math"
"testing"
)
var xSquared = func(x float64) float64 { return x * x }
type testPoint struct {
f func(float64) float64
loc float64
fofx float64
ans float64
}
var testsFirst = []testPoint{
{
f: xSquared,
loc: 0,
fofx: 0,
ans: 0,
},
{
f: xSquared,
loc: 5,
fofx: 25,
ans: 10,
},
{
f: xSquared,
loc: 2,
fofx: 4,
ans: 4,
},
{
f: xSquared,
loc: -5,
fofx: 25,
ans: -10,
},
}
var testsSecond = []testPoint{
{
f: xSquared,
loc: 0,
fofx: 0,
ans: 2,
},
{
f: xSquared,
loc: 5,
fofx: 25,
ans: 2,
},
{
f: xSquared,
loc: 2,
fofx: 4,
ans: 2,
},
{
f: xSquared,
loc: -5,
fofx: 25,
ans: 2,
},
}
func testDerivative(t *testing.T, formula Formula, tol float64, tests []testPoint) {
for i, test := range tests {
ans := Derivative(test.f, test.loc, &Settings{
Formula: formula,
})
if math.Abs(test.ans-ans) > tol {
t.Errorf("Case %v: ans mismatch serial: expected %v, found %v", i, test.ans, ans)
}
ans = Derivative(test.f, test.loc, &Settings{
Formula: formula,
OriginKnown: true,
OriginValue: test.fofx,
})
if math.Abs(test.ans-ans) > tol {
t.Errorf("Case %v: ans mismatch serial origin known: expected %v, found %v", i, test.ans, ans)
}
ans = Derivative(test.f, test.loc, &Settings{
Formula: formula,
Concurrent: true,
})
if math.Abs(test.ans-ans) > tol {
t.Errorf("Case %v: ans mismatch concurrent: expected %v, found %v", i, test.ans, ans)
}
ans = Derivative(test.f, test.loc, &Settings{
Formula: formula,
OriginKnown: true,
OriginValue: test.fofx,
Concurrent: true,
})
if math.Abs(test.ans-ans) > tol {
t.Errorf("Case %v: ans mismatch concurrent: expected %v, found %v", i, test.ans, ans)
}
}
}
func TestForward(t *testing.T) {
t.Parallel()
testDerivative(t, Forward, 2e-4, testsFirst)
}
func TestBackward(t *testing.T) {
t.Parallel()
testDerivative(t, Backward, 2e-4, testsFirst)
}
func TestCentral(t *testing.T) {
t.Parallel()
testDerivative(t, Central, 1e-6, testsFirst)
}
func TestCentralSecond(t *testing.T) {
t.Parallel()
testDerivative(t, Central2nd, 1e-3, testsSecond)
}
// TestDerivativeDefault checks that the derivative works when settings is nil
// or zero value.
func TestDerivativeDefault(t *testing.T) {
t.Parallel()
tol := 1e-6
for i, test := range testsFirst {
ans := Derivative(test.f, test.loc, nil)
if math.Abs(test.ans-ans) > tol {
t.Errorf("Case %v: ans mismatch default: expected %v, found %v", i, test.ans, ans)
}
ans = Derivative(test.f, test.loc, &Settings{})
if math.Abs(test.ans-ans) > tol {
t.Errorf("Case %v: ans mismatch zero value: expected %v, found %v", i, test.ans, ans)
}
}
}
|