簡體   English   中英

創建和反轉大型伽羅瓦域矩陣

[英]Create and invert large Galois field matrix

我有一個大小為 128x128 的矩陣。 每個條目都是一個二進制字段元素(在我的用例中,只有 0 和 1)。 我嘗試在 matlab 中反轉這個矩陣。 我在 matlab 中找到了一些函數,它們在這里進行有限域矩陣求逆http://www.mathworks.com/help/comm/galois-field-computations.html

但是,這些內置函數僅支持最大 16x16 的矩陣大小。 還有其他方法可以克服這個限制嗎? 我對其他工具持開放態度,例如 python 或 C/C++。

如果您想嘗試您的方法,這里是測試矩陣及其逆矩陣。

矩陣 A [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^-1 [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]

看看 SAGE www.sagemath.org

我有一個尺寸為128x128的矩陣。 每個條目都是一個二進制字段元素(在我的用例中,只有0和1)。 我嘗試在Matlab中反轉此矩陣。 我在http://www.mathworks.com/help/comm/galois-field-computations.html上找到了一些可進行有限域矩陣求逆的函數。

但是,這些內置函數僅支持最大16x16的矩陣大小。 還有其他方法可以克服此限制嗎? 我對其他工具(例如python或C / C ++)持開放態度。

如果您想嘗試您的方法,這里是測試矩陣及其逆。

矩陣A [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 ^ -1 [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]

反相通過伽羅瓦域的矩陣可以通過執行來實現高斯消去上(具有在伽羅瓦域執行的所有算術) [A | I] [A | I] ,結果為[I | A^-1] [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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM