-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.go
129 lines (110 loc) · 2.38 KB
/
math_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
package gosor
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAdd(t *testing.T) {
t.Parallel()
t1, err := New(WithSize(3, 2, 3), WithValues(
1, 2, 3,
1, 2, 3,
1, 2, 3,
1, 2, 3,
1, 2, 3,
1, 2, 3,
))
require.NoError(t, err)
t2, err := New(WithSize(3, 2, 3), WithValues(
1, 2, 3,
1, 2, 3,
5, 2, 3,
5, 2, 3,
1, 2, 3,
1, 2, 3,
))
require.NoError(t, err)
expectedRes, err := New(WithSize(3, 2, 3), WithValues(
2, 4, 6,
2, 4, 6,
6, 4, 6,
6, 4, 6,
2, 4, 6,
2, 4, 6,
))
require.NoError(t, err)
res, err := Add(t1, t2)
require.NoError(t, err)
require.Equal(t, expectedRes.Items(), res.Items())
}
func TestAddWithDifferentDimensions(t *testing.T) {
t.Parallel()
testCases := []struct {
t1 *MaybeTensor
t2 *MaybeTensor
expectedItems []float64
}{
{
Wrap(New(WithSize(2), WithValues(2, 3))),
Wrap(New(WithSize(3, 2), WithValues(2, 2, 3, 3, 6, 6))),
[]float64{4, 5, 5, 6, 8, 9},
},
{
Wrap(New(WithValues(0, 0, 0, 0))),
Wrap(New(WithValues(1))),
[]float64{1, 1, 1, 1},
},
}
for _, tc := range testCases {
res, err := tc.t1.Do(Add, tc.t2).Value()
require.NoError(t, err)
require.Equal(t, tc.expectedItems, res.Items())
}
}
func TestBroadcasting(t *testing.T) {
t.Parallel()
testCases := []struct {
a *Tensor
b *Tensor
aExpectedStrides []int
bExpectedStrides []int
aExpectedSizes []int
bExpectedSizes []int
}{
{
a: &Tensor{
sizes: []int{5, 1, 5},
strides: []int{5, 5, 1},
},
b: &Tensor{
sizes: []int{1, 5, 1},
strides: []int{5, 1, 1},
},
aExpectedStrides: []int{5, 0, 1},
aExpectedSizes: []int{5, 5, 5},
bExpectedStrides: []int{0, 1, 0},
bExpectedSizes: []int{5, 5, 5},
},
{
a: &Tensor{
sizes: []int{5},
strides: []int{1},
},
b: &Tensor{
sizes: []int{3, 4, 5},
strides: []int{20, 5, 1},
},
aExpectedStrides: []int{0, 0, 1},
aExpectedSizes: []int{3, 4, 5},
bExpectedStrides: []int{20, 5, 1},
bExpectedSizes: []int{3, 4, 5},
},
}
for _, tc := range testCases {
resA, resB, err := Broadcast(tc.a, tc.b)
require.NoError(t, err)
require.Equal(t, tc.aExpectedSizes, resA.sizes)
require.Equal(t, tc.aExpectedStrides, resA.strides)
require.Equal(t, tc.bExpectedSizes, resB.sizes)
require.Equal(t, tc.bExpectedStrides, resB.strides)
}
}