SHOW:
|
|
- or go back to the newest paste.
1 | def rref(matrix, augment=None): | |
2 | def row_swap(i, j): | |
3 | matrix[i,:], matrix[j,:] = matrix[j,:], matrix[i,:].copy() | |
4 | augment[i,:], augment[j,:] = augment[j,:], augment[i,:].copy() | |
5 | def row_xor(i, j): | |
6 | matrix[i,:] = matrix[i,:] ^ matrix[j,:] | |
7 | augment[i,:] = augment[i,:] ^ augment[j,:] | |
8 | matrix = matrix.copy() | |
9 | if augment is None: | |
10 | augment = np.matrix(np.zeros(matrix.shape), dtype=dt) | |
11 | else: | |
12 | augment = augment.copy() | |
13 | height, width = matrix.shape | |
14 | pivots = [] | |
15 | print("reducing") | |
16 | for i in range(width): | |
17 | col = matrix[:,i] | |
18 | pivot_row = len(pivots) | |
19 | if pivot_row >= height: | |
20 | break | |
21 | if col.take(pivot_row) == 0: | |
22 | nonzeros = col.flat[pivot_row:].nonzero()[1] | |
23 | if len(nonzeros) == 0: | |
24 | continue | |
25 | else: | |
26 | row_swap(pivot_row, pivot_row + nonzeros[0]) | |
27 | - | assert col.take(pivot_row) != 0 |
27 | + | |
28 | nonzeros = col.nonzero()[0] | |
29 | for nonzero in nonzeros: | |
30 | - | for nonzero in [nz for nz in nonzeros if nz > pivot_row]: |
30 | + | if nonzero != pivot_row: |
31 | - | row_xor(nonzero, pivot_row) |
31 | + | row_xor(nonzero, pivot_row) |
32 | - | print("unpivoting") |
32 | + | return matrix, augment |