可以通过对 执行高斯消元(在伽罗瓦域中执行所有算术)来完成在伽罗瓦域上的矩阵求逆[A | I]
,这会导致[I | A^-1]
.
这是一些执行高斯消除(行减少)的伪代码。
def row_reduce(A):
A_rre = A.copy()
p = 0 # The pivot
for j in range(A.shape[1]):
# Find a pivot in column `j` at or below row `p`
idxs = np.nonzero(A_rre[p:,j])[0]
if idxs.size == 0:
continue
i = p + idxs[0] # Row with a pivot
# Swap row `p` and `i`. The pivot is now located at row `p`.
A_rre[[p,i],:] = A_rre[[i,p],:]
# Force pivot value to be 1
A_rre[p,:] /= A_rre[p,j]
# Force zeros above and below the pivot
idxs = np.nonzero(A_rre[:,j])[0].tolist()
idxs.remove(p)
A_rre[idxs,:] -= np.multiply.outer(A_rre[idxs,j], A_rre[p,:])
p += 1
if p == A_rre.shape[0]:
break
return A_rre
我有这个用例,但找不到完成此任务的 Python 库。所以我创建了一个 Python 包galois,它在 Galois 字段上扩展了 NumPy 数组。np.linalg
它支持使用正规函数的线性代数。
这是使用您的测试矩阵的示例。
In [1]: import numpy as np
In [2]: import galois
In [3]: GF = galois.GF(2)
In [4]: A = GF([[0,0,0,1,0,0,1,0],[1,1,1,0,1,0,1,1],[1,1,1,0,1,1,0,1],[0,1,0,0,0,0,1,0],[0,1,1,1,1,1,1,0
...: ],[1,0,1,1,0,0,1,0],[0,0,1,0,0,0,1,0],[0,0,0,0,0,1,0,0]]); A
Out[4]:
GF([[0, 0, 0, 1, 0, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 1, 1],
[1, 1, 1, 0, 1, 1, 0, 1],
[0, 1, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 0]], order=2)
In [5]: A_inv = np.linalg.inv(A); A_inv
Out[5]:
GF([[1, 1, 1, 0, 0, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 0, 1],
[0, 1, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 1, 1, 1]], order=2)
In [6]: A @ A_inv
Out[6]:
GF([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]], order=2)