mhostetter / galois

A performant NumPy extension for Galois fields and their applications
https://mhostetter.github.io/galois/
MIT License
338 stars 31 forks source link

[bug?] Left Identity matrix of `.row_reduce(eye="left")` in none full rank system #564

Closed MrVeka closed 1 week ago

MrVeka commented 4 months ago

Hi, I found some colone of the identity matrix is not completely left when using .row_reduce(eye="left") on matrices where the rank is less than the number of rows. Here is the left and right behavior :

import galois
import numpy as np

GF = galois.GF(3)
H = GF([[0, 2, 2, 2, 1, 0, 0, 0],
        [2, 0, 2, 2, 0, 1, 0, 0],
        [2, 1, 0, 2, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 0]])

H_rre = H.row_reduce(eye="left")
print(H_rre, "rank:", np.linalg.matrix_rank(H), ", #row:", H.shape[0], end="\n")

>>> [[1 0 1 0 1 1 1 0]
     [0 1 1 0 0 2 1 0]
     [0 0 0 1 2 1 2 0]
     [0 0 0 0 0 0 0 0]] rank: 3 , #row: 4
H_ = GF([[1, 0, 0, 0, 0, 2, 2, 2,],
         [0, 1, 0, 0, 2, 0, 2, 2,],
         [0, 0, 1, 0, 2, 1, 0, 2,],
         [0, 0, 0, 0, 0, 0, 0, 0,]])

H_rre_ = H_.row_reduce(eye="right")
print(H_rre_, "rank:", np.linalg.matrix_rank(H_), ", #row:", H.shape[0])

>>> [[0 0 0 0 0 0 0 0]
     [2 1 0 0 2 1 0 0]
     [1 1 1 0 1 0 1 0]
     [2 1 2 0 0 0 0 1]] rank: 3 , #row: 4

Is it normal behavior?

dimbleby commented 3 months ago

seems normal, reduced row echelon form does not require the property that you suggest. See eg wikipedia.

mhostetter commented 1 week ago

I agree with @dimbleby. This is normal behavior of Gaussian Elimination / row reduction.