-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmat_test.go
308 lines (288 loc) · 6.16 KB
/
mat_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package lap
import (
"math"
"reflect"
"testing"
"unsafe"
)
const almostEps = 1e-16
func TestNewDenseMatrix(t *testing.T) {
n := 4
m := 6
// will panic if new dense accesses invalid memory
mat := NewDenseMatrix(n, m, nil)
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
v := mat.At(i, j)
if v != 0 {
t.Error("matrix was expected to be zero")
}
}
}
}
func TestSwapRows_Copy(t *testing.T) {
A := NewDenseMatrix(2, 3, []float64{
0, 1, 2,
3, 4, 5,
})
var B DenseM
B.Copy(A)
B.SwapRows(0, 1)
for i := 0; i < 3; i++ {
at := B.At(0, i)
if at != float64(i+3) {
t.Errorf("row 0 at position %d had value %.0f, expected %d", i, at, i+3)
}
}
}
func TestDims(t *testing.T) {
A := NewDenseMatrix(3, 4, nil)
m, n := A.Dims()
if m != 3 || n != 4 {
t.Errorf("matrix had shape (%d,%d), expected (3,4)", m, n)
}
}
func TestEye(t *testing.T) {
A := Eye(3)
var expectation float64
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i == j {
expectation = 1
} else {
expectation = 0
}
at := A.At(i, j)
if at != expectation {
t.Errorf("at r,c (%d,%d) identity matrix had value %f, expected %f", i, j, at, expectation)
}
}
}
}
func TestMatCopy(t *testing.T) {
// MatCopy produces a copy of A with no overlapping memory, this tests that
// the pointers truly do not overlap. TestMatCopyTo verifies that the
// elements are the same. This is fragile, in that we assume they share
// an implementation. _shrug_
A := NewDenseMatrix(4, 4, nil)
var B DenseM
B.Copy(A)
dataptrA := (*reflect.SliceHeader)(unsafe.Pointer(&A.data)).Data
dataptrB := (*reflect.SliceHeader)(unsafe.Pointer(&B.data)).Data
if dataptrA == dataptrB {
t.Error("had same data pointer, expected to be different")
}
}
func TestMatCopyTo(t *testing.T) {
A := NewDenseMatrix(4, 4, []float64{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16})
B := NewDenseMatrix(4, 4, nil)
B.Copy(A)
m, n := A.Dims()
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
a := A.At(i, j)
b := B.At(i, j)
if a != b {
t.Errorf("at (%d,%d), expected A==B, got %f and %f", i, j, a, b)
}
}
}
}
func TestDenseSlice(t *testing.T) {
var d DenseM
d.Copy(magic3)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
for k := i + 1; k <= 3; k++ {
for l := j + 1; l <= 3; l++ {
sli := d.Slice(i, k, j, l)
r, c := sli.Dims()
for ii := 0; ii < r; ii++ {
for jj := 0; jj < c; jj++ {
expect := d.At(i+ii, j+jj)
got := sli.At(ii, jj)
if expect != got {
t.Error("got not expect", got, expect)
}
}
}
}
}
}
}
}
func TestNorm(t *testing.T) {
data := []float64{
1, 2, 3,
4, 5, 6}
A := NewDenseMatrix(2, 3, data)
var expectation float64
for _, v := range data {
expectation += v * v
}
expectation = math.Sqrt(expectation)
norm := Norm(A, 2)
if !almostEqual(expectation, norm, almostEps) {
t.Errorf("expected %f, got %f", expectation, norm)
}
}
func TestMatTranspose(t *testing.T) {
inp := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6})
exp := NewDenseMatrix(3, 2, []float64{
1, 4,
2, 5,
3, 6})
inpT := T(inp)
if !matrixEqual(exp, inpT) {
t.Error("matrix transpose did not match expectation")
}
}
func TestMatAdd(t *testing.T) {
inp := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
sub := NewDenseMatrix(2, 3, []float64{
2, 3, 4,
5, 6, 7,
})
exp := NewDenseMatrix(2, 3, []float64{
3, 5, 7,
9, 11, 13,
})
out := NewDenseMatrix(2, 3, nil)
out.Add(inp, sub)
if !matrixEqual(exp, out) {
t.Error("matrix sub did not match expectation")
}
}
func TestMatSub(t *testing.T) {
inp := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
sub := NewDenseMatrix(2, 3, []float64{
2, 3, 4,
5, 6, 7,
})
exp := NewDenseMatrix(2, 3, []float64{
-1, -1, -1,
-1, -1, -1,
})
out := NewDenseMatrix(2, 3, nil)
out.Sub(inp, sub)
if !matrixEqual(exp, out) {
t.Error("matrix sub did not match expectation")
t.Log(out)
}
}
func TestMatSwapRows(t *testing.T) {
inp1 := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
inp2 := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
exp := NewDenseMatrix(2, 3, []float64{
4, 5, 6,
1, 2, 3,
})
inp1.SwapRows(0, 1)
inp2.SwapRows(0, 1)
if !matrixEqual(exp, inp1) {
t.Error("matrix swap rows non copying did not match expectation")
}
if !matrixEqual(exp, inp2) {
t.Error("matrix swap rows copying did not match expectation")
}
}
func TestMatSwapCols(t *testing.T) {
inp := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
exp := NewDenseMatrix(2, 3, []float64{
2, 1, 3,
5, 4, 6,
})
inp.SwapCols(0, 1)
if !matrixEqual(exp, inp) {
t.Error("matrix swap cols did not match expectation")
}
}
func TestMatColView(t *testing.T) {
inp := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
cols := [][]float64{
{1, 4}, {2, 5}, {3, 6},
}
for j := range cols {
got := inp.ColView(j)
expect := NewDenseVector(got.Len(), cols[j])
if !vectorEqual(got, expect) {
t.Error("got column not equal")
}
}
}
func TestMatRowView(t *testing.T) {
inp := NewDenseMatrix(2, 3, []float64{
1, 2, 3,
4, 5, 6,
})
rows := [][]float64{
{1, 2, 3}, {4, 5, 6},
}
for i := range rows {
got := inp.RowView(i)
expect := NewDenseVector(got.Len(), rows[i])
if !vectorEqual(got, expect) {
t.Error("got row not equal", i)
}
}
}
func TestSliceVec(t *testing.T) {
var stairs = NewDenseVector(10, nil)
for i := 0; i < 10; i++ {
stairs.SetVec(i, float64(i))
}
var v DenseV
idx := []int{1, 3, 5, 9}
slice := SliceVec(stairs, idx)
n := v.CopyVec(slice)
if n != len(idx) {
t.Error("slice copy length wrong")
}
for i := range idx {
if v.AtVec(i) != stairs.AtVec(idx[i]) || v.AtVec(i) != slice.AtVec(i) {
t.Error("slice copy wrong", Formatted(&v), Formatted(slice), stairs)
}
}
}
// func TestSquareMatrixInvert(t *testing.T) {
// inp := NewDenseMatrix(2, 2, []float64{
// 1, 2,
// 3, 4,
// })
// exp := NewDenseMatrix(2, 2, []float64{
// -2, 1,
// 1.5, -0.5,
// })
// var inv DenseM
// err := inv.invertSquare(inp, nil)
// if err != nil {
// t.Fatal(err)
// }
// if !matrixEqual(exp, inv) {
// t.Error("matrix inversion did not match expectation")
// }
// }