Debian Codegraph @

matrix.go

raw
1 /**
2 * Matrix Algebra over an 8-bit Galois Field
3 *
4 * Copyright 2015, Klaus Post
5 * Copyright 2015, Backblaze, Inc.
6 */
7
8 package reedsolomon
9
10 import (
11 "errors"
12 "fmt"
13 "strconv"
14 "strings"
15 )
16
17 // byte[row][col]
18 type matrix [][]byte
19
20 // newMatrix returns a matrix of zeros.
21 func newMatrix(rows, cols int) (matrix, error) {
22 if rows <= 0 {
23 return nil, errInvalidRowSize
24 }
25 if cols <= 0 {
26 return nil, errInvalidColSize
27 }
28
29 m := matrix(make([][]byte, rows))
30 for i := range m {
31 m[i] = make([]byte, cols)
32 }
33 return m, nil
34 }
35
36 // NewMatrixData initializes a matrix with the given row-major data.
37 // Note that data is not copied from input.
38 func newMatrixData(data [][]byte) (matrix, error) {
39 m := matrix(data)
40 err := m.Check()
41 if err != nil {
42 return nil, err
43 }
44 return m, nil
45 }
46
47 // IdentityMatrix returns an identity matrix of the given size.
48 func identityMatrix(size int) (matrix, error) {
49 m, err := newMatrix(size, size)
50 if err != nil {
51 return nil, err
52 }
53 for i := range m {
54 m[i][i] = 1
55 }
56 return m, nil
57 }
58
59 // errInvalidRowSize will be returned if attempting to create a matrix with negative or zero row number.
60 var errInvalidRowSize = errors.New("invalid row size")
61
62 // errInvalidColSize will be returned if attempting to create a matrix with negative or zero column number.
63 var errInvalidColSize = errors.New("invalid column size")
64
65 // errColSizeMismatch is returned if the size of matrix columns mismatch.
66 var errColSizeMismatch = errors.New("column size is not the same for all rows")
67
68 func (m matrix) Check() error {
69 rows := len(m)
70 if rows == 0 {
71 return errInvalidRowSize
72 }
73 cols := len(m[0])
74 if cols == 0 {
75 return errInvalidColSize
76 }
77
78 for _, col := range m {
79 if len(col) != cols {
80 return errColSizeMismatch
81 }
82 }
83 return nil
84 }
85
86 // String returns a human-readable string of the matrix contents.
87 //
88 // Example: [[1, 2], [3, 4]]
89 func (m matrix) String() string {
90 rowOut := make([]string, 0, len(m))
91 for _, row := range m {
92 colOut := make([]string, 0, len(row))
93 for _, col := range row {
94 colOut = append(colOut, strconv.Itoa(int(col)))
95 }
96 rowOut = append(rowOut, "["+strings.Join(colOut, ", ")+"]")
97 }
98 return "[" + strings.Join(rowOut, ", ") + "]"
99 }
100
101 // Multiply multiplies this matrix (the one on the left) by another
102 // matrix (the one on the right) and returns a new matrix with the result.
103 func (m matrix) Multiply(right matrix) (matrix, error) {
104 if len(m[0]) != len(right) {
105 return nil, fmt.Errorf("columns on left (%d) is different than rows on right (%d)", len(m[0]), len(right))
106 }
107 result, _ := newMatrix(len(m), len(right[0]))
108 for r, row := range result {
109 for c := range row {
110 var value byte
111 for i := range m[0] {
112 value ^= galMultiply(m[r][i], right[i][c])
113 }
114 result[r][c] = value
115 }
116 }
117 return result, nil
118 }
119
120 // Augment returns the concatenation of this matrix and the matrix on the right.
121 func (m matrix) Augment(right matrix) (matrix, error) {
122 if len(m) != len(right) {
123 return nil, errMatrixSize
124 }
125
126 result, _ := newMatrix(len(m), len(m[0])+len(right[0]))
127 for r, row := range m {
128 for c := range row {
129 result[r][c] = m[r][c]
130 }
131 cols := len(m[0])
132 for c := range right[0] {
133 result[r][cols+c] = right[r][c]
134 }
135 }
136 return result, nil
137 }
138
139 // errMatrixSize is returned if matrix dimensions are doesn't match.
140 var errMatrixSize = errors.New("matrix sizes do not match")
141
142 func (m matrix) SameSize(n matrix) error {
143 if len(m) != len(n) {
144 return errMatrixSize
145 }
146 for i := range m {
147 if len(m[i]) != len(n[i]) {
148 return errMatrixSize
149 }
150 }
151 return nil
152 }
153
154 // SubMatrix returns a part of this matrix. Data is copied.
155 func (m matrix) SubMatrix(rmin, cmin, rmax, cmax int) (matrix, error) {
156 result, err := newMatrix(rmax-rmin, cmax-cmin)
157 if err != nil {
158 return nil, err
159 }
160 // OPTME: If used heavily, use copy function to copy slice
161 for r := rmin; r < rmax; r++ {
162 for c := cmin; c < cmax; c++ {
163 result[r-rmin][c-cmin] = m[r][c]
164 }
165 }
166 return result, nil
167 }
168
169 // SwapRows Exchanges two rows in the matrix.
170 func (m matrix) SwapRows(r1, r2 int) error {
171 if r1 < 0 || len(m) <= r1 || r2 < 0 || len(m) <= r2 {
172 return errInvalidRowSize
173 }
174 m[r2], m[r1] = m[r1], m[r2]
175 return nil
176 }
177
178 // IsSquare will return true if the matrix is square, otherwise false.
179 func (m matrix) IsSquare() bool {
180 return len(m) == len(m[0])
181 }
182
183 // errSingular is returned if the matrix is singular and cannot be inversed
184 var errSingular = errors.New("matrix is singular")
185
186 // errNotSquare is returned if attempting to inverse a non-square matrix.
187 var errNotSquare = errors.New("only square matrices can be inverted")
188
189 // Invert returns the inverse of this matrix.
190 // Returns ErrSingular when the matrix is singular and doesn't have an inverse.
191 // The matrix must be square, otherwise ErrNotSquare is returned.
192 func (m matrix) Invert() (matrix, error) {
193 if !m.IsSquare() {
194 return nil, errNotSquare
195 }
196
197 size := len(m)
198 work, _ := identityMatrix(size)
199 work, _ = m.Augment(work)
200
201 err := work.gaussianElimination()
202 if err != nil {
203 return nil, err
204 }
205
206 return work.SubMatrix(0, size, size, size*2)
207 }
208
209 func (m matrix) gaussianElimination() error {
210 rows := len(m)
211 columns := len(m[0])
212 // Clear out the part below the main diagonal and scale the main
213 // diagonal to be 1.
214 for r := range rows {
215 // If the element on the diagonal is 0, find a row below
216 // that has a non-zero and swap them.
217 if m[r][r] == 0 {
218 for rowBelow := r + 1; rowBelow < rows; rowBelow++ {
219 if m[rowBelow][r] != 0 {
220 err := m.SwapRows(r, rowBelow)
221 if err != nil {
222 return err
223 }
224 break
225 }
226 }
227 }
228 // If we couldn't find one, the matrix is singular.
229 if m[r][r] == 0 {
230 return errSingular
231 }
232 // Scale to 1.
233 if m[r][r] != 1 {
234 scale := galOneOver(m[r][r])
235 for c := range columns {
236 m[r][c] = galMultiply(m[r][c], scale)
237 }
238 }
239 // Make everything below the 1 be a 0 by subtracting
240 // a multiple of it. (Subtraction and addition are
241 // both exclusive or in the Galois field.)
242 for rowBelow := r + 1; rowBelow < rows; rowBelow++ {
243 if m[rowBelow][r] != 0 {
244 scale := m[rowBelow][r]
245 for c := range columns {
246 m[rowBelow][c] ^= galMultiply(scale, m[r][c])
247 }
248 }
249 }
250 }
251
252 // Now clear the part above the main diagonal.
253 for d := range rows {
254 for rowAbove := range d {
255 if m[rowAbove][d] != 0 {
256 scale := m[rowAbove][d]
257 for c := range columns {
258 m[rowAbove][c] ^= galMultiply(scale, m[d][c])
259 }
260
261 }
262 }
263 }
264 return nil
265 }
266
267 // Create a Vandermonde matrix, which is guaranteed to have the
268 // property that any subset of rows that forms a square matrix
269 // is invertible.
270 func vandermonde(rows, cols int) (matrix, error) {
271 result, err := newMatrix(rows, cols)
272 if err != nil {
273 return nil, err
274 }
275 for r, row := range result {
276 for c := range row {
277 result[r][c] = galExp(byte(r), c)
278 }
279 }
280 return result, nil
281 }