Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array sizing in PLU decomposition #477

Merged
merged 7 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<a href="https://pypi.org/project/galois"><img src="https://img.shields.io/pypi/wheel/galois"></a>
<a href="https://pypistats.org/packages/galois"><img src="https://img.shields.io/pypi/dm/galois"></a>
<a href="https://pypi.org/project/galois"><img src="https://img.shields.io/pypi/l/galois"></a>
<a href="https://twitter.com/galois_py"><img src="https://img.shields.io/twitter/follow/galois_py?label=galois_py&style=flat&logo=twitter"></a>
<a href="https://twitter.com/galois_py"><img src="https://img.shields.io/static/v1?label=follow&message=@galois_py&color=blue&logo=twitter"></a>
</div>

<div align=center>
Expand Down
10 changes: 5 additions & 5 deletions docs/_templates/base.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% extends "!base.html" %}

{% block outdated %}
You're not viewing the latest version.
<a href="{{ '../' ~ base_url}}">
<strong>Click here to go to latest.</strong>
</a>
{% endblock %}
You're not viewing the latest version.
<a href="{{ '../' ~ base_url}}">
<strong>Click here to go to latest.</strong>
</a>
{% endblock %}
16 changes: 8 additions & 8 deletions src/galois/_domains/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,18 @@ def __call__(self, A: Array) -> tuple[Array, Array]:
if not A.ndim == 2:
raise ValueError(f"Argument 'A' must be a 2-D matrix, not have shape {A.shape}.")

n = A.shape[0]
m = A.shape[0]
Ai = A.copy()
L = self.field.Identity(n)
L = self.field.Identity(m)

for i in range(0, n - 1):
for i in range(0, m - 1):
if Ai[i, i] == 0:
idxs = np.nonzero(Ai[i:, i])[0] # The first non-zero entry in column `i` below row `i`
if idxs.size == 0: # pylint: disable=no-else-continue
L[i, i] = 1
continue
else:
raise ValueError("The LU decomposition of 'A' does not exist. Use the LUP decomposition instead.")
raise ValueError("The LU decomposition of 'A' does not exist. Use the PLU decomposition instead.")

l = Ai[i + 1 :, i] / Ai[i, i]
Ai[i + 1 :, :] -= np.multiply.outer(l, Ai[i, :])
Expand All @@ -343,13 +343,13 @@ def __call__(self, A: Array) -> tuple[Array, Array, Array, int]:
if not A.ndim == 2:
raise ValueError(f"Argument 'A' must be a 2-D matrix, not have shape {A.shape}.")

n = A.shape[0]
m, n = A.shape
Ai = A.copy()
L = self.field.Zeros((n, n))
P = self.field.Identity(n) # Row permutation matrix
L = self.field.Zeros((m, m))
P = self.field.Identity(m) # Row permutation matrix
N_permutations = 0 # Number of permutations

for i in range(0, n - 1):
for i in range(0, min(m, n)):
if Ai[i, i] == 0:
idxs = np.nonzero(Ai[i:, i])[0] # The first non-zero entry in column `i` below row `i`
if idxs.size == 0:
Expand Down
22 changes: 22 additions & 0 deletions tests/fields/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,28 @@ def test_plu_decompose(field_plu_decompose):
assert type(u) is GF


def test_bug_476():
"""
See https://github.com/mhostetter/galois/issues/476.
"""
GF = galois.GF(2)
A = GF(
[
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1],
]
)
P, L, U = A.plu_decompose()
PLU = P @ L @ U
assert np.array_equal(A, PLU)


def test_matrix_inverse_exceptions():
GF = galois.GF(2**8)
with pytest.raises(np.linalg.LinAlgError):
Expand Down