Repository navigation
Expand file tree
/
Copy pathufunc_test.go
More file actions
133 lines (118 loc) · 4.77 KB
/
Copy pathufunc_test.go
File metadata and controls
133 lines (118 loc) · 4.77 KB
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
package ndarray
import (
"math"
"testing"
)
func eqClose(t *testing.T, got, want []float64) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("len = %d (%v), want %d (%v)", len(got), got, len(want), want)
}
for i := range got {
if math.Abs(got[i]-want[i]) > 1e-12 {
t.Fatalf("[%d] = %v, want %v (%v vs %v)", i, got[i], want[i], got, want)
}
}
}
func TestMathUfuncs(t *testing.T) {
// All expectations from numpy 2.2.4.
a := mustArr(t, ok(FromData([]float64{0, 1, 4, 9}, 4)))
eqFloats(t, a.Sqrt().materialize(), []float64{0, 1, 2, 3})
eqClose(t, mustArr(t, ok(FromData([]float64{0, 1}, 2))).Exp().materialize(),
[]float64{1, math.E})
eqClose(t, mustArr(t, ok(FromData([]float64{1, math.E}, 2))).Log().materialize(),
[]float64{0, 1})
eqClose(t, mustArr(t, ok(FromData([]float64{1, 2, 8}, 3))).Log2().materialize(),
[]float64{0, 1, 3})
eqClose(t, mustArr(t, ok(FromData([]float64{1, 10, 100}, 3))).Log10().materialize(),
[]float64{0, 1, 2})
eqClose(t, mustArr(t, ok(FromData([]float64{0, math.Pi / 2}, 2))).Sin().materialize(),
[]float64{0, 1})
eqClose(t, mustArr(t, ok(FromData([]float64{0, math.Pi}, 2))).Cos().materialize(),
[]float64{1, -1})
eqClose(t, mustArr(t, ok(FromData([]float64{0}, 1))).Tan().materialize(),
[]float64{0})
fc := mustArr(t, ok(FromData([]float64{1.7, -1.2}, 2)))
eqFloats(t, fc.Floor().materialize(), []float64{1, -2})
cc := mustArr(t, ok(FromData([]float64{1.2, -1.7}, 2)))
eqFloats(t, cc.Ceil().materialize(), []float64{2, -1})
// Round is half to even, as np.round: 0.5 -> 0 and 2.5 -> 2. RoundHalfAway
// is the old math.Round: 0.5 -> 1 and 2.5 -> 3.
rc := mustArr(t, ok(FromData([]float64{0.5, 1.5, 2.5, -0.5}, 4)))
eqFloats(t, rc.Round().materialize(), []float64{0, 2, 2, 0})
eqFloats(t, rc.RoundHalfAway().materialize(), []float64{1, 2, 3, -1})
sq := mustArr(t, ok(FromData([]float64{2, -3}, 2)))
eqFloats(t, sq.Square().materialize(), []float64{4, 9})
pw := mustArr(t, ok(FromData([]float64{2, 3}, 2)))
eqFloats(t, pw.Power(3).materialize(), []float64{8, 27})
}
func TestComparisonUfuncs(t *testing.T) {
x := mustArr(t, ok(FromData([]float64{1, 2, 3}, 3)))
y := mustArr(t, ok(FromData([]float64{3, 2, 1}, 3)))
cases := []struct {
name string
fn func(*Array) (*Array, error)
want []float64
}{
{"gt", x.Greater, []float64{0, 0, 1}},
{"ge", x.GreaterEqual, []float64{0, 1, 1}},
{"lt", x.Less, []float64{1, 0, 0}},
{"le", x.LessEqual, []float64{1, 1, 0}},
{"eq", x.Equal, []float64{0, 1, 0}},
{"ne", x.NotEqual, []float64{1, 0, 1}},
{"max", x.Maximum, []float64{3, 2, 3}},
{"min", x.Minimum, []float64{1, 2, 1}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := mustArr(t, ok(c.fn(y)))
eqFloats(t, r.materialize(), c.want)
})
}
}
func TestComparisonBroadcastAndError(t *testing.T) {
// Comparison against a scalar broadcasts; m > 2 over arange(6).reshape(2,3).
m := mustArr(t, ok(FromData([]float64{0, 1, 2, 3, 4, 5}, 2, 3)))
two := scalarArray(2)
gt := mustArr(t, ok(m.Greater(two)))
eqInts(t, gt.Shape(), []int{2, 3})
eqFloats(t, gt.materialize(), []float64{0, 0, 0, 1, 1, 1})
// Mask composes with arithmetic: keep only elements > 2.
kept := mustArr(t, ok(gt.Mul(m)))
eqFloats(t, kept.materialize(), []float64{0, 0, 0, 3, 4, 5})
// Incompatible shapes surface ErrBroadcast through binOp.
bad := mustArr(t, ok(FromData([]float64{1, 2}, 2)))
for _, fn := range []func(*Array) (*Array, error){
m.Greater, m.GreaterEqual, m.Less, m.LessEqual,
m.Equal, m.NotEqual, m.Maximum, m.Minimum,
} {
if _, err := fn(bad); err == nil {
t.Fatalf("expected broadcast error")
}
}
}
// TestExpTopOfRange: exp is finite up to ln(MaxFloat64) on every arch.
// amd64's math.Exp returns +Inf from x ~ 709.436 (golang/go#81995), which Exp used to
// inherit; numpy gives 1.3549863193146328e+308 for 709.5. The input is a
// strided view, so the contiguous copy path is exercised too.
func TestExpTopOfRange(t *testing.T) {
x := mustArr(t, ok(FromData([]float64{709.5, 0, 709.78, 0, -1000, 0}, 6)))
v, err := x.Slice(Step(2))
if err != nil {
t.Fatal(err)
}
wantData(t, v.Exp(), []int{3}, []float64{1.3549863193146328e+308, 1.7928227943945155e+308, 0})
}
// TestLogSubnormal: Log and Log10 are right for subnormal inputs on every
// arch. amd64's math.Log returns -709.09 for both 5e-324 and 1e-310 (golang/go#56600),
// which Log and Log10 used to inherit; numpy gives -744.44 and -713.80.
func TestLogSubnormal(t *testing.T) {
x := mustArr(t, ok(FromData([]float64{5e-324, 1e-310, 1, math.E}, 4)))
wantData(t, x.Log(), []int{4}, []float64{-744.4400719213812, -713.8013788281542, 0, 1})
got := x.Log10()
for i, want := range []float64{-323.3062153431158, -310, 0, 0.4342944819032518} {
if math.Abs(got.data[i]-want) > 2e-16*math.Abs(want) {
t.Errorf("Log10[%d] = %v, want %v", i, got.data[i], want)
}
}
}