gpt4 book ai didi

python - Sympy:在有限域中求解矩阵

转载 作者:太空狗 更新时间:2023-10-29 21:46:01 24 4
gpt4 key购买 nike

对于我的项目,我需要在给定矩阵 Y 和 K 的情况下求解矩阵 X。(XY=K) 每个矩阵的元素必须是对随机 256 位素数取模的整数。我第一次尝试使用 SymPy 的 mod_inv(n) 函数来解决这个问题。这个问题是我的内存用完了大约 30 大小的矩阵。我的下一个想法是执行矩阵分解,因为这可能不会占用内存。但是,SymPy 似乎不包含可以找到模数矩阵的求解器。我可以使用任何解决方法或自制代码吗?

最佳答案

sympyMatrix 类支持模逆。这是一个模 5 的示例:

from sympy import Matrix, pprint

A = Matrix([
[5,6],
[7,9]
])

#Find inverse of A modulo 26
A_inv = A.inv_mod(5)
pprint(A_inv)

#Prints the inverse of A modulo 5:
#[3 3]
#[ ]
#[1 0]

rref 查找行简化阶梯形式的方法支持关键字 iszerofunction,它指示矩阵中的哪些条目应被视为零。尽管我不确定,但我相信预期用途是为了数值稳定性(将小数视为零)。我已将其用于模块化缩减。

这是一个模 5 的例子:

from sympy import Matrix, Rational, mod_inverse, pprint

B = Matrix([
[2,2,3,2,2],
[2,3,1,1,4],
[0,0,0,1,0],
[4,1,2,2,3]
])

#Find row-reduced echolon form of B modulo 5:
B_rref = B.rref(iszerofunc=lambda x: x % 5==0)

pprint(B_rref)

# Returns row-reduced echelon form of B modulo 5, along with pivot columns:
# ([1 0 7/2 0 -1], [0, 1, 3])
# [ ]
# [0 1 -2 0 2 ]
# [ ]
# [0 0 0 1 0 ]
# [ ]
# [0 0 -10 0 5 ]

这是正确的,除了 rref[0] 返回的矩阵中仍然有 5 和分数。通过取模并将分数解释为模逆来解决这个问题:

def mod(x,modulus):
numer, denom = x.as_numer_denom()
return numer*mod_inverse(denom,modulus) % modulus

pprint(B_rref[0].applyfunc(lambda x: mod(x,5)))

#returns
#[1 0 1 0 4]
#[ ]
#[0 1 3 0 2]
#[ ]
#[0 0 0 1 0]
#[ ]
#[0 0 0 0 0]

关于python - Sympy:在有限域中求解矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com