-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolation_test.go
184 lines (164 loc) · 5.56 KB
/
interpolation_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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package edau
import "math"
import "math/rand"
import "time"
import "testing"
var testPoints []float64
var testLocations []float64
func init() {
rand.Seed(time.Now().UnixNano())
testPoints = make([]float64, 16)
for i := 0; i < 16; i++ {
testPoints[i] = math.Sin(math.Pi*float64(i)/2)
}
testLocations = make([]float64, 16)
for i := 0; i < 16; i++ {
testLocations[i] = 7 + rand.Float64()
}
}
func TestLagrangeN4(t *testing.T) {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget4(testPoints, loc)
result := InterpLagrangeN(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.1 {
t.Fatalf("TestLagrangeN (4) for %f expected %f but got %f (diff = %f)", loc, expect, result, diff)
}
}
}
func TestLagrangeN6(t *testing.T) {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget6(testPoints, loc)
result := InterpLagrangeN(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.05 {
t.Fatalf("TestLagrangeN (6) for %f expected %f but got %f (diff = %f)", loc, expect, result, diff)
}
}
}
func TestInterpLagrange4Pt3Ord(t *testing.T) {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget4(testPoints, loc)
result := InterpLagrange4Pt3Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.1 {
t.Fatalf("TestInterpLagrange4Pt3Ord for %f expected %f but got %f (diff = %f)", loc, expect, result, diff)
}
}
}
func TestInterpLagrange6Pt5Ord(t *testing.T) {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget6(testPoints, loc)
result := InterpLagrange6Pt5Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.05 {
t.Fatalf("TestInterpLagrange6Pt5Ord for %f expected %f but got %f (diff = %f)", loc, expect, result, diff)
}
}
}
func TestInterpHermite4pt3Ord(t *testing.T) {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget4(testPoints, loc)
result := InterpHermite4Pt3Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.1 {
t.Fatalf("TestInterpHermite4pt3Ord for %f expected %f but got %f (diff = %f)", loc, expect, result, diff)
}
}
}
func TestInterpFastHermite6Pt3Ord(t *testing.T) {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget6(testPoints, loc)
result := InterpHermite6Pt3Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.05 {
t.Fatalf("TestInterpFastHermite6Pt5Ord for %f expected %f but got %f (diff = %f)", loc, expect, result, diff)
}
}
}
// benchmarks
func BenchmarkLagrangeN4(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget4(testPoints, loc)
result := InterpLagrangeN(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.1 { panic("precision failure") }
}
}
}
func BenchmarkLagrangeN6(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget6(testPoints, loc)
result := InterpLagrangeN(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.05 { panic("precision failure") }
}
}
}
func BenchmarkLagrange4Pt3Ord(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget4(testPoints, loc)
result := InterpLagrange4Pt3Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.1 { panic("precision failure") }
}
}
}
func BenchmarkLagrange6Pt5Ord(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget6(testPoints, loc)
result := InterpLagrange6Pt5Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.05 { panic("precision failure") }
}
}
}
func BenchmarkHermite4Pt3Ord(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget4(testPoints, loc)
result := InterpHermite4Pt3Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.1 { panic("precision failure") }
}
}
}
func BenchmarkHermite6Pt3Ord(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, loc := range testLocations {
samples, target := alignSamplesAndTarget6(testPoints, loc)
result := InterpHermite6Pt3Ord(samples, target)
expect := math.Sin(math.Pi*loc/2)
diff := math.Abs(result - expect)
if diff > 0.05 { panic("precision failure") }
}
}
}
// --- helper functions ---
func alignSamplesAndTarget4(samples []float64, targetPosition float64) ([]float64, float64) {
targetFloorPosition := int(targetPosition)
targetPosition -= float64(targetFloorPosition - 1) // shift target to align to samples zero-indexing
targetSamples := samples[targetFloorPosition - 1 : targetFloorPosition + 3]
return targetSamples, targetPosition
}
func alignSamplesAndTarget6(samples []float64, targetPosition float64) ([]float64, float64) {
targetFloorPosition := int(targetPosition)
targetPosition -= float64(targetFloorPosition - 2) // shift target to align to samples zero-indexing
targetSamples := samples[targetFloorPosition - 2 : targetFloorPosition + 4]
return targetSamples, targetPosition
}