簡體   English   中英

用矩陣逆模 N 擴展歐幾里德算法

[英]Extend Euclid Algorithm with matrix inverse mod N

我正在使用矩陣 mod N實現擴展的 Eucilid 算法 這是我的代碼實現:

def eea(a, b):
    if not isinstance(a, int) or not isinstance(b, int) or not a or not b:
        result = 'Error(eea): Invalid input num'
    else:
        original_a = a
        original_b = b
        x, y, u, v = (0, 1, 1, 0)
        while a != 0:
            q, r = (b // a, b % a)
            m, n = (x - u * q, y - v * q)
            b, a, x, y, u, v = (a, r, u, v, m, n)
        cnsm = b
        result = [cnsm, x, y]
        if original_a < 0 and original_b < 0:
            result[0] = abs(result[0])
            result[1] = -1 * result[1]
            result[2] = -1 * result[2]
        if result[0] < 0:
            result = [abs(result[0]), x, y]
            if original_a < 0 < original_b or original_b < 0 < original_a:
                result[2] = -1 * result[2]
        if result[0] > 0:
            if original_b < 0 < original_a:
                result[2] = -1 * result[2]
    return result

現在,我需要使用以下矩陣計算矩陣逆模36

[3, 2]
[4, 7]

(這是視頻鏈接:)

矩陣逆模 N

但是,我的代碼只能得到x = -11, y = -4 ,正是方程13x = 36y + 1 ,但在視頻中,解是x = 25, y = 9 ,那么我該如何更改我的代碼能滿足這種情況嗎?

−11 與 25 mod 36 一致,所以在 Python 中你可以只取x % N

暫無
暫無

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

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