-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix_test.go
166 lines (150 loc) · 2.79 KB
/
matrix_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
package magcal
import (
"fmt"
"math/rand"
"testing"
)
func TestMulSimple(t *testing.T) {
m := matrix(make([]float32, 9))
v := make([]float32, 3)
for i := 0; i < 3; i++ {
v[i] = 3*rand.Float32() - 1.5
m[i*3+i] = 1
}
w := m.mul(v)
if !w.close(v, 3) {
t.Errorf(`want %v got %v`, v, w)
}
}
func TestTrans(t *testing.T) {
m := matrix(make([]float32, 9))
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
m[i*3+j] = 3*rand.Float32() - 1.5
}
}
mm := m.trans()
mmm := mm.trans()
if !mmm.close(m, 3) {
print(m.string())
print(mmm.string())
t.Errorf(`trans not equal`)
}
}
func TestInvSimple(t *testing.T) {
m := matrix(make([]float32, 9))
a := vector(make([]float32, 3))
for i := 0; i < 3; i++ {
a[i] = 3*rand.Float32() - 1.5
m[i*3+i] = 1 + float32(i)
}
b := m.mul(a)
w := m.inv()
actual := w.mul(b)
expected := a
if !actual.close(expected, 3) {
print(m.string())
println(a.string())
println()
println(b.string())
println()
println(w.string())
println()
t.Errorf(`want %v got %v`, expected, actual)
}
}
func TestInv(t *testing.T) {
m := matrix(make([]float32, 9))
v := vector(make([]float32, 3))
for i := 0; i < 3; i++ {
v[i] = 3*rand.Float32() - 1.5
for j := 0; j < 3; j++ {
m[i*3+j] = 3*rand.Float32() - 1.5
}
}
vv := m.mul(v)
mm := m.inv()
actual := mm.mul(vv)
expected := v
if !actual.close(expected, 3) {
t.Errorf(`want %v got %v`, expected, actual)
}
}
// Helper methods
func (m matrix) string() string {
result := ""
for i := 0; i < 3; i++ {
result += fmt.Sprintf("[%0.3f,%0.3f,%0.3f]\r\n", m[i*3], m[i*3+1], m[i*3+2])
}
return result
}
// Calculate inverse matrix
// https://www.wikihow.com/Find-the-Inverse-of-a-3x3-Matrix
func (m matrix) det() float32 {
d1 := m[0] * m.detSub(0, 0)
d2 := m[1] * m.detSub(0, 1)
d3 := m[2] * m.detSub(0, 2)
return d1 - d2 + d3
}
func (m matrix) detSub(x, y int) float32 {
i1, i2, j1, j2 := 0, 0, 0, 0
if x == 0 {
i1 = 1
i2 = 2
}
if x == 1 {
i1 = 0
i2 = 2
}
if x == 2 {
i1 = 0
i2 = 1
}
if y == 0 {
j1 = 1
j2 = 2
}
if y == 1 {
j1 = 0
j2 = 2
}
if y == 2 {
j1 = 0
j2 = 1
}
return m[i1*3+j1]*m[i2*3+j2] - m[i1*3+j2]*m[i2*3+j1]
}
func (m matrix) trans() (w matrix) {
w = make([]float32, 9)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
w[j*3+i] = m[i*3+j]
}
}
return w
}
func (m matrix) inv() (w matrix) {
d := m.det()
t := m.trans()
w = make([]float32, 9)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
w[i*3+j] = t.detSub(i, j) / d
if (i == 1 || j == 1) && (i*j != 1) {
w[i*3+j] *= -1
}
}
}
return w
}
func (m matrix) close(w matrix, precision int) bool {
mul := float32(10 * precision)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if int(m[i*3+j]*mul) != int(w[i*3+j]*mul) {
return false
}
}
}
return true
}