This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
forked from cmars/conflux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.go
153 lines (134 loc) · 3.7 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
/*
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-2015 Casey Marshall <[email protected]>
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"
"gopkg.in/errgo.v1"
)
var ErrMatrixTooNarrow = errors.New("matrix is too narrow to reduce")
// Matrix represents a rectangular array of numbers over a finite field Z(p).
type Matrix struct {
columns, rows int
cells []*Zp
}
// NewMatrix returns a new Matrix of the given dimensions and finite field p.
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
}
// Get returns the value at the given (row, column) location.
func (m *Matrix) Get(i, j int) *Zp {
return m.cells[i+(j*m.columns)]
}
// Set sets the value at the given (row, column) location.
func (m *Matrix) Set(i, j int, x *Zp) {
m.cells[i+(j*m.columns)] = x.Copy()
}
// Reduce performs Gaussian elimination on a matrix of coefficients, in-place.
func (m *Matrix) Reduce() error {
if m.columns < m.rows {
return errgo.Mask(ErrMatrixTooNarrow)
}
for j := 0; j < m.rows; j++ {
m.processRowForward(j)
}
for j := m.rows - 1; j > 0; j-- {
m.backSubstitute(j)
}
return nil
}
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))
}
}
}
func (m *Matrix) processRowForward(j int) {
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
}
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())
}
}
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)
}
}
}
}
// String returns a string representation of the matrix.
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())
}