Skip to content

Commit

Permalink
Add ability to row_reduce() to solve for I on the right side
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Nov 7, 2022
1 parent fcf2751 commit 9297913
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/galois/_fields/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def vector(self, dtype: DTypeLike | None = None) -> FieldArray:

return y

def row_reduce(self, ncols: int | None = None) -> Self:
def row_reduce(self, ncols: int | None = None, eye: Literal["left", "right"] = "left") -> Self:
r"""
Performs Gaussian elimination on the matrix to achieve reduced row echelon form (RREF).
Expand All @@ -987,6 +987,8 @@ def row_reduce(self, ncols: int | None = None) -> Self:
ncols
The number of columns to perform Gaussian elimination over. The default is `None` which represents
the number of columns of the matrix.
eye
The location of the identity matrix :math:`\mathbf{I}`, either on the left or the right.
Returns
-------
Expand All @@ -1013,13 +1015,28 @@ def row_reduce(self, ncols: int | None = None) -> Self:
A.row_reduce()
np.linalg.matrix_rank(A)
Perform Gaussian elimination to get an :math:`\mathbf{I}` on the right side of :math:`\mathbf{A}`.
.. ipython:: python
A.row_reduce(eye="right")
Or only perform Gaussian elimination over 2 columns.
.. ipython:: python
A.row_reduce(ncols=2)
"""
A_rre, _ = _linalg.row_reduce_jit(type(self))(self, ncols=ncols)
verify_literal(eye, ["left", "right"])

if eye == "left":
A = self
A_rre, _ = _linalg.row_reduce_jit(type(A))(A, ncols=ncols)
else:
A = self[::-1, ::-1]
A_rre, _ = _linalg.row_reduce_jit(type(A))(A, ncols=ncols)
A_rre = A_rre[::-1, ::-1]

return A_rre

def lu_decompose(self) -> tuple[Self, Self]:
Expand Down
8 changes: 8 additions & 0 deletions tests/fields/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ def test_row_reduce(field_row_reduce):
assert type(z) is GF


def test_row_reduce_eye_right():
GF = galois.GF(2)
H = GF([[1,0,1,0,1,0,1,0], [0,1,1,0,0,1,1,0], [0,0,0,1,1,1,1,0], [1,1,1,1,1,1,1,1]])
H_rre = H.row_reduce(eye="right")
assert type(H_rre) is GF
assert np.array_equal(H_rre, [[0,1,1,1,1,0,0,0], [1,0,1,1,0,1,0,0], [1,1,0,1,0,0,1,0], [1,1,1,0,0,0,0,1]])


def test_lu_decompose_exceptions():
GF = galois.GF(2**8)
with pytest.raises(ValueError):
Expand Down

0 comments on commit 9297913

Please sign in to comment.