-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrix.go
554 lines (445 loc) · 12.2 KB
/
matrix.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package poseidon
import (
"errors"
"fmt"
)
type Matrix[E Element[E]] [][]E
type Vector[E Element[E]] []E
// return the column numbers of the matrix.
func column[E Element[E]](m Matrix[E]) int {
if len(m) > 0 {
length := len(m[0])
for i := 1; i < len(m); i++ {
if len(m[i]) != length {
panic("m is not matrix!")
}
}
return length
} else {
return 0
}
}
// return the row numbers of the matrix.
func row[E Element[E]](m Matrix[E]) int {
return len(m)
}
// for 0 <= i < row, 0 <= j < column, compute M_ij*scalar.
func ScalarMul[E Element[E]](scalar E, m Matrix[E]) Matrix[E] {
res := make([][]E, len(m))
for i := 0; i < len(m); i++ {
res[i] = make([]E, len(m[i]))
for j := 0; j < len(m[i]); j++ {
res[i][j] = NewElement[E]().Mul(scalar, m[i][j])
}
}
return res
}
// for 0 <= i < length, compute v_i*scalar.
func ScalarVecMul[E Element[E]](scalar E, v Vector[E]) Vector[E] {
res := make([]E, len(v))
for i := 0; i < len(v); i++ {
res[i] = NewElement[E]().Mul(scalar, v[i])
}
return res
}
func VecAdd[E Element[E]](a, b Vector[E]) (Vector[E], error) {
if len(a) != len(b) {
return nil, errors.New("length err: cannot compute vector add")
}
res := make([]E, len(a))
for i := 0; i < len(a); i++ {
res[i] = NewElement[E]().Add(a[i], b[i])
}
return res, nil
}
func VecSub[E Element[E]](a, b Vector[E]) (Vector[E], error) {
if len(a) != len(b) {
return nil, errors.New("length err: cannot compute vector sub")
}
res := make([]E, len(a))
for i := 0; i < len(a); i++ {
res[i] = NewElement[E]().Sub(a[i], b[i])
}
return res, nil
}
// compute the product between two vectors.
func VecMul[E Element[E]](a, b Vector[E]) (E, error) {
res := NewElement[E]()
if len(a) != len(b) {
return res, errors.New("length err: cannot compute vector mul!")
}
for i := 0; i < len(a); i++ {
tmp := NewElement[E]().Mul(a[i], b[i])
res.Add(res, tmp)
}
return res, nil
}
func IsVecEqual[E Element[E]](a, b Vector[E]) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i].Cmp(b[i]) != 0 {
return false
}
}
// time-constant comparison, against timing attacks.
//res := 0
//for i := 0; i < len(a); i++ {
// res |= a[i].Cmp(b[i])
//}
//return res == 0
return true
}
// if delta(m)≠0, m is invertible.
// so we can transform m to the upper triangular matrix,
// and if all upper diagonal elements are not zero, then m is invertible.
func IsInvertible[E Element[E]](m Matrix[E]) bool {
// need to copy m.
tmp := copyMatrixRows(m, 0, row(m))
if !IsSquareMatrix(tmp) {
return false
}
shadow := MakeIdentity[E](row(tmp))
upper, _, err := upperTriangular(tmp, shadow)
if err != nil {
panic(err)
}
for i := 0; i < row(tmp); i++ {
if upper[i][i].Cmp(zero[E]()) == 0 {
return false
}
}
return true
}
// compute the product between two matrices.
func MatMul[E Element[E]](a, b Matrix[E]) (Matrix[E], error) {
if row(a) != column(b) {
return nil, errors.New("cannot compute the result")
}
transb := transpose(b)
var err error
res := make([][]E, row(a))
for i := 0; i < row(a); i++ {
res[i] = make([]E, column(b))
for j := 0; j < column(b); j++ {
res[i][j], err = VecMul(a[i], transb[j])
if err != nil {
return nil, fmt.Errorf("vec mul err: %w", err)
}
}
}
return res, nil
}
// left Matrix multiplication, denote by M*V, where M is the matrix, and V is the vector.
func LeftMatMul[E Element[E]](m Matrix[E], v Vector[E]) (Vector[E], error) {
if !IsSquareMatrix(m) {
panic("matrix is not square!")
}
if row(m) != len(v) {
return nil, errors.New("length err: cannot compute matrix multiplication with the vector")
}
res := make([]E, len(v))
var err error
for i := 0; i < len(v); i++ {
res[i], err = VecMul[E](m[i], v)
if err != nil {
return nil, fmt.Errorf("vector mul err: %w", err)
}
}
return res, nil
}
// right Matrix multiplication, denote by V*M, where V is the vector, and M is the matrix.
func RightMatMul[E Element[E]](v Vector[E], m Matrix[E]) (Vector[E], error) {
if !IsSquareMatrix(m) {
return nil, errors.New("matrix is not square")
}
if row(m) != len(v) {
return nil, errors.New("length err: cannot compute matrix multiplication with the vector")
}
transm := transpose(m)
res := make([]E, len(v))
var err error
for i := 0; i < len(v); i++ {
res[i], err = VecMul(transm[i], v)
if err != nil {
return nil, fmt.Errorf("vector mul err: %w", err)
}
}
return res, nil
}
// swap rows and columns of the matrix.
func transpose[E Element[E]](m Matrix[E]) Matrix[E] {
res := make([][]E, column(m))
for j := 0; j < column(m); j++ {
res[j] = make([]E, len(m))
for i := 0; i < len(m); i++ {
res[j][i] = m[i][j]
}
}
return res
}
// the square matrix is a t*t matrix.
func IsSquareMatrix[E Element[E]](m Matrix[E]) bool {
return row(m) == column(m)
}
// make t*t identity matrix.
func MakeIdentity[E Element[E]](t int) Matrix[E] {
res := make([][]E, t)
for i := 0; i < t; i++ {
res[i] = make([]E, t)
for j := 0; j < t; j++ {
if i == j {
res[i][j] = one[E]()
} else {
res[i][j] = zero[E]()
}
}
}
return res
}
// determine if a matrix is identity.
func IsIdentity[E Element[E]](m Matrix[E]) bool {
for i := 0; i < row(m); i++ {
for j := 0; j < column(m); j++ {
if ((i == j) && m[i][j].Cmp(one[E]()) != 0) || ((i != j) && (m[i][j].Cmp(zero[E]()) != 0)) {
return false
}
}
}
return true
}
func IsEqual[E Element[E]](a, b Matrix[E]) bool {
if row(a) != row(b) || column(a) != column(b) {
return false
}
for i := 0; i < row(a); i++ {
for j := 0; j < column(a); j++ {
if a[i][j].Cmp(b[i][j]) != 0 {
return false
}
}
}
// time-constant comparison, against timing attacks.
//res := 0
//for i := 0; i < row(a); i++ {
// for j := 0; j < column(a); j++ {
// res |= a[i][j].Cmp(b[i][j])
// }
//}
//return res == 0
return true
}
// remove i-th row and j-th column of the matrix.
func minor[E Element[E]](m Matrix[E], rowIndex, columnIndex int) (Matrix[E], error) {
if !IsSquareMatrix(m) {
return nil, errors.New("matrix is not square!")
}
res := make([][]E, row(m)-1)
for i := 0; i < row(m); i++ {
if i < rowIndex {
for j := 0; j < column(m); j++ {
if j != columnIndex {
res[i] = append(res[i], m[i][j])
}
}
} else if i > rowIndex {
for j := 0; j < column(m); j++ {
if j != columnIndex {
res[i-1] = append(res[i-1], m[i][j])
}
}
}
}
return res, nil
}
// determine if the first k elements are zero.
func isFirstKZero[E Element[E]](v Vector[E], k int) bool {
if k == 0 && v[0].Cmp(zero[E]()) == 0 {
return false
}
for i := 0; i < k; i++ {
if v[i].Cmp(zero[E]()) != 0 {
return false
}
}
return true
}
// find the first non-zero element in the given column.
func findNonZero[E Element[E]](m Matrix[E], index int) (pivot E, pivotIndex int, err error) {
pivotIndex = -1
if index > column(m) {
return NewElement[E](), -1, errors.New("index out of range!")
}
for i := 0; i < row(m); i++ {
if m[i][index].Cmp(zero[E]()) != 0 {
pivot = m[i][index]
pivotIndex = i
break
}
}
return
}
// assume matrix is partially reduced to upper triangular.
func eliminate[E Element[E]](m, shadow Matrix[E], columnIndex int) (Matrix[E], Matrix[E], error) {
pivot, pivotIndex, err := findNonZero(m, columnIndex)
if err != nil || pivotIndex == -1 {
return nil, nil, fmt.Errorf("cannot find non-zero element: %w", err)
}
pivotInv := NewElement[E]().Inverse(pivot)
for i := 0; i < row(m); i++ {
if i == pivotIndex {
continue
}
if m[i][columnIndex].Cmp(zero[E]()) != 0 {
factor := NewElement[E]().Mul(m[i][columnIndex], pivotInv)
scalarPivot := ScalarVecMul(factor, m[pivotIndex])
m[i], err = VecSub(m[i], scalarPivot)
if err != nil {
return nil, nil, fmt.Errorf("matrix m eliminate failed, vec sub err: %w", err)
}
shadowPivot := shadow[pivotIndex]
scalarShadowPivot := ScalarVecMul(factor, shadowPivot)
shadow[i], err = VecSub(shadow[i], scalarShadowPivot)
if err != nil {
return nil, nil, fmt.Errorf("matrix shadow eliminate failed, vec sub err: %w", err)
}
}
}
return m, shadow, nil
}
// copy rows between start index and end index.
func copyMatrixRows[E Element[E]](m Matrix[E], startIndex, endIndex int) Matrix[E] {
if startIndex >= endIndex {
panic("start index should be less than end index!")
}
res := make([][]E, endIndex-startIndex)
for i := 0; i < endIndex-startIndex; i++ {
res[i] = make([]E, column(m))
copy(res[i], m[i+startIndex])
}
return res
}
// reverse rows of the matrix.
func reverseRows[E Element[E]](m Matrix[E]) Matrix[E] {
res := make([][]E, row(m))
for i := 0; i < row(m); i++ {
res[i] = make([]E, column(m))
copy(res[i], m[row(m)-i-1])
}
return res
}
// determine if numbers of zero elements equals to n.
func zeroNums[E Element[E]](v Vector[E], n int) bool {
count := 0
for i := 0; i < len(v); i++ {
if v[i].Cmp(zero[E]()) != 0 {
break
}
count++
}
if count == n {
return true
}
return false
}
// determine if a matrix is upper triangular.
func isUpperTriangular[E Element[E]](m Matrix[E]) bool {
for i := 0; i < row(m); i++ {
if !zeroNums(m[i], i) {
return false
}
}
return true
}
// transform a square matrix to upper triangular matrix.
func upperTriangular[E Element[E]](m, shadow Matrix[E]) (Matrix[E], Matrix[E], error) {
if !IsSquareMatrix(m) {
return nil, nil, errors.New("matrix is not square!")
}
curr := copyMatrixRows(m, 0, row(m))
currShadow := copyMatrixRows(shadow, 0, row(shadow))
result := make([][]E, row(m))
shadowResult := make([][]E, row(shadow))
c := 0
var err error
for row(curr) > 1 {
result[c] = make([]E, column(m))
shadowResult[c] = make([]E, column(shadow))
curr, currShadow, err = eliminate(curr, currShadow, c)
if err != nil {
return nil, nil, fmt.Errorf("matrix eliminate err: %w", err)
}
copy(result[c], curr[0])
copy(shadowResult[c], currShadow[0])
c++
curr = copyMatrixRows(curr, 1, row(curr))
currShadow = copyMatrixRows(currShadow, 1, row(currShadow))
}
result[c] = make([]E, column(m))
shadowResult[c] = make([]E, column(shadow))
copy(result[c], curr[0])
copy(shadowResult[c], currShadow[0])
return result, shadowResult, nil
}
// reduce a upper triangular matrix to identity matrix.
func reduceToIdentity[E Element[E]](m, shadow Matrix[E]) (Matrix[E], Matrix[E], error) {
var err error
result := make([][]E, row(m))
shadowResult := make([][]E, row(shadow))
for i := 0; i < row(m); i++ {
result[i] = make([]E, column(m))
shadowResult[i] = make([]E, column(shadow))
indexi := row(m) - i - 1
factor := m[indexi][indexi]
if factor.Cmp(zero[E]()) == 0 {
return nil, nil, errors.New("cannot compute the result!")
}
factorInv := NewElement[E]().Inverse(factor)
norm := ScalarVecMul(factorInv, m[indexi])
shadowNorm := ScalarVecMul(factorInv, shadow[indexi])
for j := 0; j < i; j++ {
indexj := row(m) - j - 1
val := norm[indexj]
scalarVal := ScalarVecMul(val, result[j])
scalarShadow := ScalarVecMul(val, shadowResult[j])
norm, err = VecSub(norm, scalarVal)
if err != nil {
return nil, nil, fmt.Errorf("reduces to identity matrix failed, err: %w", err)
}
shadowNorm, err = VecSub(shadowNorm, scalarShadow)
if err != nil {
return nil, nil, fmt.Errorf("reduces to identity matrix failed, err: %w", err)
}
}
copy(result[i], norm)
copy(shadowResult[i], shadowNorm)
}
result = reverseRows(result)
shadowResult = reverseRows(shadowResult)
return result, shadowResult, nil
}
// use Gaussian elimination to invert a matrix.
// A|I -> I|A^-1.
func Invert[E Element[E]](m Matrix[E]) (Matrix[E], error) {
if !IsInvertible(m) {
return nil, fmt.Errorf("the matrix is not invertible")
}
shadow := MakeIdentity[E](row(m))
up, upShadow, err := upperTriangular(m, shadow)
if err != nil {
return nil, fmt.Errorf("transform to upper triangular matrix failed, err: %w", err)
}
if !isUpperTriangular(up) {
return nil, fmt.Errorf("the matrix should be upper triangular before reducing")
}
// reduce m to identity, so shadow matrix transforms to the inverse of m.
reduce, reducedShadow, err := reduceToIdentity(up, upShadow)
if err != nil {
return nil, fmt.Errorf("reduce to identity failed, err: %w", err)
}
if !IsIdentity(reduce) {
return nil, errors.New("reduces failed, the result is not the identity matrix")
}
return reducedShadow, nil
}