Skip to content

Commit 7decff7

Browse files
committed
Matrix tests and fix
1 parent b573454 commit 7decff7

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

pkg/core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (m *Matrix[A]) Cofactor(other Matrix[A]) {
294294
for c := 0; c < n; c++ {
295295
col := m.columns[c]
296296
for r := 0; r < n; r++ {
297-
negative := (r*n+c)&1 == 1
297+
negative := (r+c)&1 == 1
298298
if negative {
299299
col.SetComponent(r, -col.GetComponent(r), &m.columns[c])
300300
}

pkg/core_test.go

+137
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,143 @@ func TestMatrix(t *testing.T) {
5454
{6, 3},
5555
},
5656
},
57+
{
58+
name: "minor 3x3",
59+
getActual: func() any {
60+
m := InitMatrix(Matrix3f{})
61+
m.SetValues([][]float32{
62+
{2, -1, 3},
63+
{0, 5, 2},
64+
{1, -1, -2},
65+
})
66+
67+
actual := InitMatrix(Matrix3f{})
68+
actual.Minor(m)
69+
return actual.GetValues()
70+
},
71+
expected: [][]float32{
72+
{-8, -2, -5},
73+
{5, -7, -1},
74+
{-17, 4, 10},
75+
},
76+
},
77+
{
78+
name: "cofactor 3x3",
79+
getActual: func() any {
80+
m := InitMatrix(Matrix3f{})
81+
m.SetValues([][]float32{
82+
{2, -1, 3},
83+
{0, 5, 2},
84+
{1, -1, -2},
85+
})
86+
87+
actual := InitMatrix(Matrix3f{})
88+
actual.Cofactor(m)
89+
return actual.GetValues()
90+
},
91+
expected: [][]float32{
92+
{-8, 2, -5},
93+
{-5, -7, 1},
94+
{-17, -4, 10},
95+
},
96+
},
97+
{
98+
name: "cofactor 3x3 #2",
99+
getActual: func() any {
100+
m := InitMatrix(Matrix3f{})
101+
m.SetValues([][]float32{
102+
{1, 2, -1},
103+
{2, 1, 2},
104+
{-1, 2, 1},
105+
})
106+
107+
actual := InitMatrix(Matrix3f{})
108+
actual.Cofactor(m)
109+
return actual.GetValues()
110+
},
111+
expected: [][]float32{
112+
{-3, -4, 5},
113+
{-4, 0, -4},
114+
{5, -4, -3},
115+
},
116+
},
117+
{
118+
name: "transpose 3x3",
119+
getActual: func() any {
120+
m := InitMatrix(Matrix3f{})
121+
m.SetValues([][]float32{
122+
{2, -1, 3},
123+
{0, 5, 2},
124+
{1, -1, -2},
125+
})
126+
127+
actual := InitMatrix(Matrix3f{})
128+
actual.Transpose(m)
129+
return actual.GetValues()
130+
},
131+
expected: [][]float32{
132+
{2, 0, 1},
133+
{-1, 5, -1},
134+
{3, 2, -2},
135+
},
136+
},
137+
{
138+
name: "adjoint 2x2",
139+
getActual: func() any {
140+
m := InitMatrix(Matrix2f{})
141+
m.SetValues([][]float32{
142+
{3, 6},
143+
{-4, 8},
144+
})
145+
// minor = 8, -4, 6, 3
146+
//
147+
actual := InitMatrix(Matrix2f{})
148+
actual.Adjoint(m)
149+
return actual.GetValues()
150+
},
151+
expected: [][]float32{
152+
{8, -6},
153+
{4, 3},
154+
},
155+
},
156+
{
157+
name: "adjoint 3x3",
158+
getActual: func() any {
159+
m := InitMatrix(Matrix3f{})
160+
m.SetValues([][]float32{
161+
{2, -1, 3},
162+
{0, 5, 2},
163+
{1, -1, -2},
164+
})
165+
166+
actual := InitMatrix(Matrix3f{})
167+
actual.Adjoint(m)
168+
return actual.GetValues()
169+
},
170+
expected: [][]float32{
171+
{-8, -5, -17},
172+
{2, -7, -4},
173+
{-5, 1, 10},
174+
},
175+
},
176+
{
177+
name: "inverse 2x2",
178+
getActual: func() any {
179+
m := InitMatrix(Matrix2f{})
180+
m.SetValues([][]float32{
181+
{3, 6},
182+
{-4, 8},
183+
})
184+
185+
actual := InitMatrix(Matrix2f{})
186+
actual.Invert(m)
187+
return actual.GetValues()
188+
},
189+
expected: [][]float32{
190+
{1.0 / 6, -1.0 / 8},
191+
{1.0 / 12, 1.0 / 16},
192+
},
193+
},
57194
}
58195

59196
for _, test := range tests {

0 commit comments

Comments
 (0)