/
mesh_test.go
86 lines (79 loc) · 1.83 KB
/
mesh_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
package optim
import (
"math"
"testing"
"github.com/gonum/matrix/mat64"
)
type Problem struct {
Step float64
Point, Exp []float64
Basis *mat64.Dense
Origin []float64
}
var tests = []Problem{
Problem{
Step: 1.3,
Basis: nil,
Origin: []float64{0, 0},
Point: []float64{0.1, 0.1},
Exp: []float64{0.0, 0.0},
},
Problem{
Step: 1.3,
Basis: nil,
Origin: []float64{0, 0},
Point: []float64{1.0, 1.0},
Exp: []float64{1.3, 1.3},
},
Problem{
Step: 1.3,
Basis: nil,
Origin: []float64{0, 0},
Point: []float64{1.9, 1.9},
Exp: []float64{1.3, 1.3},
},
Problem{ // 45 deg clockwise rotation of the identity basis
Step: 1.0,
Basis: mat64.NewDense(2, 2, []float64{1 / math.Sqrt(2), 1 / math.Sqrt(2), -1 / math.Sqrt(2), 1 / math.Sqrt(2)}),
Origin: []float64{0, 0},
Point: []float64{1.0, 1.0},
Exp: []float64{1 / math.Sqrt(2), 1 / math.Sqrt(2)},
},
Problem{ // non-zero origin
Step: 1.0,
Basis: nil,
Origin: []float64{0.2, 0.3},
Point: []float64{1.6, 2.1},
Exp: []float64{1.2, 2.3},
},
}
func TestSimple(t *testing.T) {
maxulps := uint64(1)
for i, prob := range tests {
m := &InfMesh{StepSize: prob.Step, Basis: prob.Basis, Center: prob.Origin}
got := m.Nearest(prob.Point)
t.Logf("prob %v:", i)
for j := range got {
if diff := DiffInUlps(got[j], prob.Exp[j]); diff > maxulps {
t.Errorf(" (FAIL) v[%v]=%v: got %v, expected %v", j, prob.Point[j], got[j], prob.Exp[j])
} else {
t.Logf(" (pass) v[%v]=%v: got %v", j, prob.Point[j], got[j])
}
}
}
}
func DiffInUlps(x, y float64) uint64 {
switch {
case math.IsNaN(x) || math.IsNaN(y) || math.IsInf(x, 0) || math.IsInf(y, 0):
return math.MaxInt64
case x == y:
return 0
default:
xi := math.Float64bits(x)
yi := math.Float64bits(y)
if xi > yi {
return xi - yi
}
return yi - xi
}
}