-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmat_test.go
460 lines (361 loc) · 9.77 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// This file is generated from mgl32/mat_test.go; DO NOT EDIT
// Copyright 2014 The go-gl 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 mgl64
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestMulIdent(t *testing.T) {
t.Parallel()
i1 := [...]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
i2 := Ident4()
i3 := Ident4()
mul := i2.Mul4(i3)
for i := range mul {
if mul[i] != i1[i] {
t.Errorf("Multiplication of identities does not yield identity")
}
}
}
// M>N
func TestMatRowsTall(t *testing.T) {
t.Parallel()
row0 := Vec3{1, 2, 3}
row1 := Vec3{4, 5, 6}
rows := [2]Vec3{row0, row1}
m1 := Mat2x3FromRows(rows[0], rows[1])
t.Logf("2x3 matrix as built from rows: %v", m1)
for r := 0; r < 2; r++ {
for c := 0; c < 3; c++ {
if !FloatEqualThreshold(m1.At(r, c), rows[r][c], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when built from rows. Got: %f, Expected: %f", r, c, m1.At(r, c), rows[r][c])
}
}
}
row0, row1 = m1.Rows()
rows2 := [2]Vec3{row0, row1}
t.Logf("2x3 matrix returned rows: %v", rows2)
for r := 0; r < 2; r++ {
for c := 0; c < 3; c++ {
if !FloatEqualThreshold(rows2[r][c], rows[r][c], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when rows are gotten. Got: %f, Expected: %f", r, c, rows2[r][c], rows[r][c])
}
}
}
}
// M<N
func TestMatRowsWide(t *testing.T) {
t.Parallel()
v0 := Vec3{1, 2, 3}
v1 := Vec3{4, 5, 6}
v2 := Vec3{7, 8, 9}
v3 := Vec3{10, 11, 12}
rows := [4]Vec3{v0, v1, v2, v3}
m1 := Mat4x3FromRows(v0, v1, v2, v3)
t.Logf("4x3 matrix as built from rows: %v", m1)
for r := 0; r < 4; r++ {
for c := 0; c < 3; c++ {
if !FloatEqualThreshold(m1.At(r, c), rows[r][c], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when built from rows. Got: %f, Expected: %f", r, c, m1.At(r, c), rows[r][c])
}
}
}
v0, v1, v2, v3 = m1.Rows()
r2 := [4]Vec3{v0, v1, v2, v3}
t.Logf("4x3 matrix returned rows: %v", r2)
for r := 0; r < 4; r++ {
for c := 0; c < 3; c++ {
if !FloatEqualThreshold(r2[r][c], rows[r][c], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when rows are gotten. Got: %f, Expected: %f", r, c, r2[r][c], rows[r][c])
}
}
}
}
// Square matrix
func TestMatRowsSquare(t *testing.T) {
t.Parallel()
v0 := Vec4{1, 2, 3, 4}
v1 := Vec4{5, 6, 7, 8}
v2 := Vec4{9, 10, 11, 12}
v3 := Vec4{13, 14, 15, 16}
rows := [4]Vec4{v0, v1, v2, v3}
m1 := Mat4FromRows(v0, v1, v2, v3)
t.Logf("4x4 matrix as built from rows: %v", m1)
for r := 0; r < 4; r++ {
for c := 0; c < 4; c++ {
if !FloatEqualThreshold(m1.At(r, c), rows[r][c], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when built from rows. Got: %f, Expected: %f", r, c, m1.At(r, c), rows[r][c])
}
}
}
v0, v1, v2, v3 = m1.Rows()
r2 := [4]Vec4{v0, v1, v2, v3}
t.Logf("4x4 matrix returned rows: %v", r2)
for r := 0; r < 4; r++ {
for c := 0; c < 4; c++ {
if !FloatEqualThreshold(r2[r][c], rows[r][c], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when rows are gotten. Got: %f, Expected: %f", r, c, r2[r][c], rows[r][c])
}
}
}
}
// M<N
func TestMatColsTall(t *testing.T) {
v0 := Vec3{1, 2, 3}
v1 := Vec3{4, 5, 6}
cols := [2]Vec3{v0, v1}
m1 := Mat3x2FromCols(v0, v1)
t.Logf("3x2 matrix as built from cols: %v", m1)
for r := 0; r < 3; r++ {
for c := 0; c < 2; c++ {
if !FloatEqualThreshold(m1.At(r, c), cols[c][r], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when built from rows. Got: %f, Expected: %f", r, c, m1.At(r, c), cols[c][r])
}
}
}
v0, v1 = m1.Cols()
r2 := [2]Vec3{v0, v1}
t.Logf("3x2 matrix returned cols: %v", r2)
for r := 0; r < 3; r++ {
for c := 0; c < 2; c++ {
if !FloatEqualThreshold(r2[c][r], cols[c][r], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when rows are gotten. Got: %f, Expected: %f", r, c, r2[c][r], cols[c][r])
}
}
}
}
// M>N
func TestMatColsWide(t *testing.T) {
t.Parallel()
v0 := Vec3{1, 2, 3}
v1 := Vec3{4, 5, 6}
v2 := Vec3{7, 8, 9}
v3 := Vec3{10, 11, 12}
cols := [4]Vec3{v0, v1, v2, v3}
m1 := Mat3x4FromCols(v0, v1, v2, v3)
t.Logf("3x4 matrix as built from cols: %v", m1)
for r := 0; r < 3; r++ {
for c := 0; c < 4; c++ {
if !FloatEqualThreshold(m1.At(r, c), cols[c][r], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when built from cols. Got: %f, Expected: %f", r, c, m1.At(r, c), cols[c][r])
}
}
}
v0, v1, v2, v3 = m1.Cols()
r2 := [4]Vec3{v0, v1, v2, v3}
t.Logf("3x4 matrix returned cols: %v", r2)
for r := 0; r < 3; r++ {
for c := 0; c < 4; c++ {
if !FloatEqualThreshold(r2[c][r], cols[c][r], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when rows are gotten. Got: %f, Expected: %f", r, c, r2[c][r], cols[c][r])
}
}
}
}
// Square matrix
func TestMatColsSquare(t *testing.T) {
t.Parallel()
v0 := Vec4{1, 2, 3, 4}
v1 := Vec4{5, 6, 7, 8}
v2 := Vec4{9, 10, 11, 12}
v3 := Vec4{13, 14, 15, 16}
cols := [4]Vec4{v0, v1, v2, v3}
m1 := Mat4FromCols(v0, v1, v2, v3)
t.Logf("4x4 matrix as built from cols: %v", m1)
for r := 0; r < 4; r++ {
for c := 0; c < 4; c++ {
if !FloatEqualThreshold(m1.At(r, c), cols[c][r], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when built from rows. Got: %f, Expected: %f", r, c, m1.At(r, c), cols[c][r])
}
}
}
v0, v1, v2, v3 = m1.Cols()
r2 := [4]Vec4{v0, v1, v2, v3}
t.Logf("4x4 matrix returned cols: %v", r2)
for r := 0; r < 4; r++ {
for c := 0; c < 4; c++ {
if !FloatEqualThreshold(r2[c][r], cols[c][r], 1e-5) {
t.Errorf("Matrix element at (%d,%d) wrong when rows are gotten. Got: %f, Expected: %f", r, c, r2[c][r], cols[c][r])
}
}
}
}
func TestTransposeTall(t *testing.T) {
t.Parallel()
m := Mat3x2FromCols(
Vec3{1, 2, 3},
Vec3{4, 5, 6},
)
transpose := m.Transpose()
correct := Mat2x3FromRows(
Vec3{1, 2, 3},
Vec3{4, 5, 6},
)
if !correct.ApproxEqualThreshold(transpose, 1e-4) {
t.Errorf("Transpose not correct. Got: %v, expected: %v", transpose, correct)
}
}
func TestTransposeWide(t *testing.T) {
t.Parallel()
m := Mat2x3FromCols(
Vec2{1, 2},
Vec2{3, 4},
Vec2{5, 6},
)
transpose := m.Transpose()
correct := Mat3x2FromRows(
Vec2{1, 2},
Vec2{3, 4},
Vec2{5, 6},
)
if !correct.ApproxEqualThreshold(transpose, 1e-4) {
t.Errorf("Transpose not correct. Got: %v, expected: %v", transpose, correct)
}
}
func TestTransposeSquare(t *testing.T) {
t.Parallel()
m := Mat3FromCols(
Vec3{1, 2, 3},
Vec3{4, 5, 6},
Vec3{7, 8, 9},
)
transpose := m.Transpose()
correct := Mat3FromRows(
Vec3{1, 2, 3},
Vec3{4, 5, 6},
Vec3{7, 8, 9},
)
if !correct.ApproxEqualThreshold(transpose, 1e-4) {
t.Errorf("Transpose not correct. Got: %v, expected: %v", transpose, correct)
}
}
func TestAtSet(t *testing.T) {
t.Parallel()
m := Mat3{1, 2, 3, 4, 5, 6, 7, 8, 9}
v := m.At(0, 2)
if !FloatEqualThreshold(v, 7, 1e-4) {
t.Errorf("Incorrect value gotten by At: %v, expected %v", v, 3)
}
m.Set(0, 2, 9001)
v = m.At(0, 2)
if !FloatEqualThreshold(v, 9001, 1e-4) {
t.Errorf("Value set by Set not gotten by At: %v, expected %v", v, 9001)
}
correctMat := Mat3{1, 2, 3, 4, 5, 6, 9001, 8, 9}
if !correctMat.ApproxEqualThreshold(m, 1e-4) {
t.Errorf("After set, not equal to matrix that should be identical. Got: %v, expected: %v", m, correctMat)
}
}
func TestDiagTrace(t *testing.T) {
t.Parallel()
m := Diag4(Vec4{1, 2, 3, 4})
tr := m.Trace()
if !FloatEqualThreshold(tr, 10, 1e-4) {
t.Errorf("Trace of matrix seeded with diagonal vector {1,2,3,4} not equal to 10. Got %v", tr)
}
}
func TestMatAbs(t *testing.T) {
t.Parallel()
m := Mat3{1, -3, 4, 5, -6, 8, -9, 10, 0}
result := Mat3{1, 3, 4, 5, 6, 8, 9, 10, 0}
m = m.Abs()
if !result.ApproxEqualThreshold(m, 1e-6) {
t.Errorf("Matrix absolute value does not work properly. Got: %v, Expected: %v", m, result)
}
}
func TestString(t *testing.T) {
m := Ident4()
str := fmt.Sprintf(` %[2]f %[1]f %[1]f %[1]f
%[1]f %[2]f %[1]f %[1]f
%[1]f %[1]f %[2]f %[1]f
%[1]f %[1]f %[1]f %[2]f
`, 0.0, 1.0)
if str != m.String() {
t.Errorf("Mat string conversion not working got %q expected %q", m.String(), str)
}
}
func BenchmarkMatAdd(b *testing.B) {
b.StopTimer()
rand := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
m1 := Mat4{}
m2 := Mat4{}
for j := 0; j < len(m1); j++ {
m1[j], m2[j] = rand.Float64(), rand.Float64()
}
b.StartTimer()
m1 = m1.Add(m2)
}
}
func BenchmarkMatScale(b *testing.B) {
b.StopTimer()
rand := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
m1 := Mat4{}
for j := 0; j < len(m1); j++ {
m1[j] = rand.Float64()
}
c := rand.Float64()
b.StartTimer()
m1 = m1.Mul(c)
}
}
func BenchmarkMatMul(b *testing.B) {
b.StopTimer()
rand := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
m1 := Mat4{}
m2 := Mat4{}
for j := 0; j < len(m1); j++ {
m1[j], m2[j] = rand.Float64(), rand.Float64()
}
b.StartTimer()
m1 = m1.Mul4(m2)
}
}
func BenchmarkMatTranspose(b *testing.B) {
b.StopTimer()
rand := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
m1 := Mat4{}
for j := 0; j < len(m1); j++ {
m1[j] = rand.Float64()
}
b.StartTimer()
_ = m1.Transpose()
}
}
func BenchmarkMatDet(b *testing.B) {
b.StopTimer()
rand := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
m1 := Mat4{}
for j := 0; j < len(m1); j++ {
m1[j] = rand.Float64()
}
b.StartTimer()
_ = m1.Det()
}
}
func BenchmarkMatInv(b *testing.B) {
b.StopTimer()
rand := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
m1 := Mat4{}
for j := 0; j < len(m1); j++ {
m1[j] = rand.Float64()
}
b.StartTimer()
m1 = m1.Inv()
}
}