-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat_test.go
84 lines (76 loc) · 1.6 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
// Copyright 2022 The Go 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_test
import (
"fmt"
"github.com/dr2chase/mat"
"testing"
)
func rmAndCm() (rm, cm mat.MuM[mat.F]) {
rm = mat.RowMajor[mat.F](5, 5)
cm = mat.ColumnMajor[mat.F](5, 5)
f := func(i, j int) mat.F {
diff := j - i
switch diff {
case 0:
return mat.F(1)
case -1, 1:
return mat.F(2 * diff)
}
return mat.F(0)
}
mat.SetByIJ[mat.F](rm, f)
mat.SetByIJ[mat.F](cm, f)
return
}
func TestPrint(t *testing.T) {
rm, cm := rmAndCm()
fmt.Println("Row major matrix")
rm.Print()
fmt.Println("Column major matrix")
cm.Print()
}
func TestEqual(t *testing.T) {
rm, cm := rmAndCm()
if !mat.EqualsM(mat.M[mat.F](rm), mat.M[mat.F](cm)) {
t.Fail()
}
if !cm.Equals(rm) {
t.Fail()
}
}
func TestV(t *testing.T) {
// rm, cm := rmAndCm()
v := mat.Vector[mat.F](5)
v.SetByI(func(i int) mat.F { return mat.F(i + 1) })
v.Print()
}
func TestVM(t *testing.T) {
rm, cm := rmAndCm()
v := mat.Vector[mat.F](5)
v.SetByI(func(i int) mat.F { return mat.F(i + 1) })
// v.Print()
rmv := rm.TimesVector(v)
rmv.Print()
cmv := cm.TimesVector(v)
cmv.Print()
rmvl := rm.LeftTimesVector(v)
rmvl.Print()
cmvl := cm.LeftTimesVector(v)
cmvl.Print()
rmtvl := rm.Transpose().LeftTimesVector(v)
rmtvl.Print()
if !rmv.Equals(cmv) {
t.Fail()
}
if !rmvl.Equals(cmvl) {
t.Fail()
}
if !rmtvl.Equals(cmv) {
t.Fail()
}
fmt.Println("rmv rmv = ", rmv.Inner(rmv))
fmt.Println("rmvl rmvl = ", rmvl.Inner(rmvl))
fmt.Println("rmv rmvl = ", rmv.Inner(rmvl))
}