forked from cmars/conflux
/
matrix.go
151 lines (132 loc) · 3.38 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
/*
conflux - Distributed database synchronization library
Based on the algorithm described in
"Set Reconciliation with Nearly Optimal Communication Complexity",
Yaron Minsky, Ari Trachtenberg, and Richard Zippel, 2004.
Copyright (C) 2012 Casey Marshall <casey.marshall@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package conflux
import (
"bytes"
"errors"
"fmt"
)
type Matrix struct {
columns, rows int
cells []*Zp
}
func NewMatrix(columns, rows int, x *Zp) *Matrix {
matrix := &Matrix{
rows: rows,
columns: columns,
cells: make([]*Zp, columns*rows)}
for i := 0; i < len(matrix.cells); i++ {
matrix.cells[i] = x.Copy()
}
return matrix
}
func (m *Matrix) Get(i, j int) *Zp {
return m.cells[i+(j*m.columns)]
}
func (m *Matrix) Set(i, j int, x *Zp) {
m.cells[i+(j*m.columns)] = x.Copy()
}
var MatrixTooNarrow = errors.New("Matrix is too narrow to reduce")
func (m *Matrix) Reduce() (err error) {
if m.columns < m.rows {
return MatrixTooNarrow
}
for j := 0; j < m.rows; j++ {
err = m.processRowForward(j)
if err != nil {
return
}
}
for j := m.rows - 1; j > 0; j-- {
m.backSubstitute(j)
}
return
}
func (m *Matrix) backSubstitute(j int) {
if m.Get(j, j).Int64() == int64(1) {
last := m.rows - 1
for j2 := j - 1; j2 >= 0; j2-- {
scmult := m.Get(j, j2).Copy()
m.rowsub(last, j, j2, scmult)
m.Set(j, j2, Zi(scmult.P, 0))
}
}
}
var SwapRowNotFound = errors.New("Swap row not found")
func (m *Matrix) processRowForward(j int) error {
v := m.Get(j, j)
if v.IsZero() {
jswap := -1
for jf := j + 1; jf < m.rows; jf++ {
if !m.Get(j, jf).IsZero() {
jswap = jf
break
}
}
if jswap == -1 {
return nil
}
m.swapRows(j, jswap)
v = m.Get(j, j)
}
if v.Int64() != int64(1) {
m.scmultRow(j, j, v.Copy().Inv())
}
for j2 := j + 1; j2 < m.rows; j2++ {
m.rowsub(j, j, j2, m.Get(j, j2).Copy())
}
return nil
}
func (m *Matrix) swapRows(j1, j2 int) {
start1 := j1 * m.columns
start2 := j2 * m.columns
for i := 0; i < m.columns; i++ {
m.cells[start1+i], m.cells[start2+i] = m.cells[start2+i], m.cells[start1+i]
}
}
func (m *Matrix) scmultRow(scol, j int, sc *Zp) {
start := j * m.columns
for i := scol; i < m.columns; i++ {
v := m.cells[start+i]
v.Mul(v, sc)
}
}
func (m *Matrix) rowsub(scol, src, dst int, scmult *Zp) {
for i := scol; i < m.columns; i++ {
sval := m.Get(i, src)
if !sval.IsZero() {
v := m.Get(i, dst)
if scmult.Int64() != int64(1) {
v.Sub(v, Z(scmult.P).Mul(sval, scmult))
} else {
v.Sub(v, sval)
}
}
}
}
func (m *Matrix) String() string {
buf := bytes.NewBuffer(nil)
for row := 0; row < m.rows; row++ {
fmt.Fprintf(buf, "| ")
for col := 0; col < m.columns; col++ {
fmt.Fprintf(buf, "%v ", m.Get(col, row))
}
fmt.Fprintf(buf, "|\n")
}
return string(buf.Bytes())
}