-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrices_test.go
227 lines (190 loc) · 4.85 KB
/
matrices_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
package jbtracer
import (
"fmt"
"strconv"
"github.com/cucumber/godog"
)
func tableToMatrix(table *godog.Table) (*Matrix, error) {
rows := len(table.Rows)
cols := len(table.Rows[0].Cells)
if rows != cols {
return nil, fmt.Errorf("Matrix currently only supports square dimensions")
}
m := NewMatrix(rows)
for i, row := range table.Rows {
for j, col := range row.Cells {
if f, err := strconv.ParseFloat(col.Value, 64); err != nil {
return nil, err
} else {
m.Set(i, j, f)
}
}
}
return m, nil
}
func matrix(m1name string, table *godog.Table) error {
if m, err := tableToMatrix(table); err != nil {
return err
} else {
matrices[m1name] = m
}
return nil
}
func matrixCellEqual(m1name string, i, j int, expected float64) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
got := m1.Get(i, j)
if !EqualFloat64(got, expected) {
return fmt.Errorf("Expected %s[%d,%d] = %v; got %v", m1name, i, j, expected, got)
}
return nil
}
func matrixEqual(m1name, op, m2name string) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
if m2, ok = matrices[m2name]; !ok {
return fmt.Errorf("Unknown symbol %s", m2name)
}
var expected bool
if op == "=" {
expected = true
} else {
expected = false
}
got := m1.Equal(m2)
if got != expected {
return fmt.Errorf("Expected %s %s %v; got %v", m1name, op, m1, m2)
}
return nil
}
func matrixMultiply(m1name, m2name, m3name string) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
if m2, ok = matrices[m2name]; !ok {
return fmt.Errorf("Unknown symbol %s", m2name)
}
if m3, ok = matrices[m3name]; !ok {
return fmt.Errorf("Unknown symbol %s", m3name)
}
expected := m3
got := m1.Multiply(m2)
if !got.Equal(expected) {
return fmt.Errorf("Expected %s * %s = %v; got %v", m1name, m2name, expected, got)
}
return nil
}
func matrixMultiplyAssign(m3name, m1name, m2name string) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
if m2, ok = matrices[m2name]; !ok {
return fmt.Errorf("Unknown symbol %s", m2name)
}
matrices[m3name] = m1.Multiply(m2)
return nil
}
func matrixMultiplyTuple(m1name, t1name, t2name string) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
if t1, ok = tuples[t1name]; !ok {
return fmt.Errorf("Unknown symbol %s", t1name)
}
if t2, ok = tuples[t2name]; !ok {
return fmt.Errorf("Unknown symbol %s", t2name)
}
expected := t2
got := m1.MultiplyTuple(t1)
if !got.Equal(expected) {
return fmt.Errorf("Expected %s * %s = %v; got %v", m1name, t1name, expected, got)
}
return nil
}
func matrixTranspose(m1name, m2name string) error {
if m2, ok = matrices[m2name]; !ok {
return fmt.Errorf("Unknown symbol %s", m2name)
}
matrices[m1name] = m2.Transpose()
return nil
}
func matrixDeterminant(s1name, m1name string) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
scalars[s1name] = m1.Determinant()
return nil
}
func scalar(s1name string, value float64) error {
scalars[s1name] = value
return nil
}
func scalarEqual(s1name, s2name string) error {
if s1, ok = scalars[s1name]; !ok {
return fmt.Errorf("Unknown symbol %s", s1name)
}
if s2, ok = scalars[s2name]; !ok {
if f, err := strconv.ParseFloat(s2name, 64); err != nil {
return fmt.Errorf("Unknown symbol %s", s2name)
} else {
s2 = f
}
}
expected := s2
got := s1
if got != expected {
return fmt.Errorf("Expected %s = %f; got %f", s1name, expected, got)
}
return nil
}
func matrixSubmatrix(m1name, m2name string, i, j int) error {
if m2, ok = matrices[m2name]; !ok {
return fmt.Errorf("Unknown symbol %s", m2name)
}
matrices[m1name] = m2.Submatrix(i, j)
return nil
}
func matrixMinor(s1name, m1name string, i, j int) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
scalars[s1name] = m1.Minor(i, j)
return nil
}
func matrixCofactor(s1name, m1name string, i, j int) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
scalars[s1name] = m1.Cofactor(i, j)
return nil
}
func matrixInvertible(m1name, op string) error {
if m1, ok = matrices[m1name]; !ok {
return fmt.Errorf("Unknown symbol %s", m1name)
}
var expected bool
if op == "is" {
expected = true
} else {
expected = false
}
_, err := m1.Inverse()
got := err == nil
if got != expected {
return fmt.Errorf("Expected %s=%v %s invertible; got the opposite", m1name, m1, op)
}
return nil
}
func matrixInverse(m1name, m2name string) error {
if m2, ok = matrices[m2name]; !ok {
return fmt.Errorf("Unknown symbol %s", m2name)
}
var err error
matrices[m1name], err = m2.Inverse()
if err != nil {
return fmt.Errorf("Matrix %v is not invertible", m2)
}
return nil
}