forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinner_test.go
186 lines (168 loc) · 3.68 KB
/
inner_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
185
186
// 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 mat
import (
"math"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/blas/blas64"
"gonum.org/v1/gonum/blas/testblas"
)
func TestInner(t *testing.T) {
t.Parallel()
for i, test := range []struct {
x []float64
y []float64
m [][]float64
}{
{
x: []float64{5},
y: []float64{10},
m: [][]float64{{2}},
},
{
x: []float64{5, 6, 1},
y: []float64{10},
m: [][]float64{{2}, {-3}, {5}},
},
{
x: []float64{5},
y: []float64{10, 15},
m: [][]float64{{2, -3}},
},
{
x: []float64{1, 5},
y: []float64{10, 15},
m: [][]float64{
{2, -3},
{4, -1},
},
},
{
x: []float64{2, 3, 9},
y: []float64{8, 9},
m: [][]float64{
{2, 3},
{4, 5},
{6, 7},
},
},
{
x: []float64{2, 3},
y: []float64{8, 9, 9},
m: [][]float64{
{2, 3, 6},
{4, 5, 7},
},
},
} {
for _, inc := range []struct{ x, y int }{
{1, 1},
{1, 2},
{2, 1},
{2, 2},
} {
x := NewDense(1, len(test.x), test.x)
m := NewDense(flatten(test.m))
mWant := NewDense(flatten(test.m))
y := NewDense(len(test.y), 1, test.y)
var tmp, cell Dense
tmp.Mul(mWant, y)
cell.Mul(x, &tmp)
rm, cm := cell.Dims()
if rm != 1 {
t.Errorf("Test %d result doesn't have 1 row", i)
}
if cm != 1 {
t.Errorf("Test %d result doesn't have 1 column", i)
}
want := cell.At(0, 0)
got := Inner(makeVecDenseInc(inc.x, test.x), m, makeVecDenseInc(inc.y, test.y))
if got != want {
t.Errorf("Test %v: want %v, got %v", i, want, got)
}
}
}
}
func TestInnerSym(t *testing.T) {
t.Parallel()
for _, inc := range []struct{ x, y int }{
{1, 1},
{1, 2},
{2, 1},
{2, 2},
} {
n := 10
xData := make([]float64, n)
yData := make([]float64, n)
data := make([]float64, n*n)
for i := 0; i < n; i++ {
xData[i] = float64(i)
yData[i] = float64(i)
for j := i; j < n; j++ {
data[i*n+j] = float64(i*n + j)
data[j*n+i] = data[i*n+j]
}
}
x := makeVecDenseInc(inc.x, xData)
y := makeVecDenseInc(inc.y, yData)
m := NewDense(n, n, data)
ans := Inner(x, m, y)
sym := NewSymDense(n, data)
// Poison the lower half of data to ensure it is not used.
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
data[i*n+j] = math.NaN()
}
}
if math.Abs(Inner(x, sym, y)-ans) > 1e-14 {
t.Error("inner different symmetric and dense")
}
}
}
func makeVecDenseInc(inc int, f []float64) *VecDense {
v := &VecDense{
mat: blas64.Vector{
N: len(f),
Inc: inc,
Data: make([]float64, (len(f)-1)*inc+1),
},
}
// Contaminate backing data in all positions...
const base = 100
for i := range v.mat.Data {
v.mat.Data[i] = float64(i + base)
}
// then write real elements.
for i := range f {
v.mat.Data[i*inc] = f[i]
}
return v
}
func benchmarkInner(b *testing.B, m, n int) {
src := rand.NewSource(1)
x := NewVecDense(m, nil)
randomSlice(x.mat.Data, src)
y := NewVecDense(n, nil)
randomSlice(y.mat.Data, src)
data := make([]float64, m*n)
randomSlice(data, src)
mat := &Dense{mat: blas64.General{Rows: m, Cols: n, Stride: n, Data: data}, capRows: m, capCols: n}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Inner(x, mat, y)
}
}
func BenchmarkInnerSmSm(b *testing.B) {
benchmarkInner(b, testblas.SmallMat, testblas.SmallMat)
}
func BenchmarkInnerMedMed(b *testing.B) {
benchmarkInner(b, testblas.MediumMat, testblas.MediumMat)
}
func BenchmarkInnerLgLg(b *testing.B) {
benchmarkInner(b, testblas.LargeMat, testblas.LargeMat)
}
func BenchmarkInnerLgSm(b *testing.B) {
benchmarkInner(b, testblas.LargeMat, testblas.SmallMat)
}