Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rref(matrix, augment=None):
- def row_swap(i, j):
- matrix[i,:], matrix[j,:] = matrix[j,:], matrix[i,:].copy()
- augment[i,:], augment[j,:] = augment[j,:], augment[i,:].copy()
- def row_xor(i, j):
- matrix[i,:] = matrix[i,:] ^ matrix[j,:]
- augment[i,:] = augment[i,:] ^ augment[j,:]
- matrix = matrix.copy()
- if augment is None:
- augment = np.matrix(np.zeros(matrix.shape), dtype=dt)
- else:
- augment = augment.copy()
- height, width = matrix.shape
- pivots = []
- print("reducing")
- for i in range(width):
- col = matrix[:,i]
- pivot_row = len(pivots)
- if pivot_row >= height:
- break
- if col.take(pivot_row) == 0:
- nonzeros = col.flat[pivot_row:].nonzero()[1]
- if len(nonzeros) == 0:
- continue
- else:
- row_swap(pivot_row, pivot_row + nonzeros[0])
- pivots.append(i)
- nonzeros = col.nonzero()[0]
- for nonzero in nonzeros:
- if nonzero != pivot_row:
- row_xor(nonzero, pivot_row)
- return matrix, augment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement