Sympy:在有限域中求解矩阵

时间:2015-07-02 16:40:16

标签: python matrix sympy modular-arithmetic matrix-factorization

对于我的项目,我需要求矩阵X给定矩阵Y和K.(XY = K)每个矩阵的元素必须是以随机256位素数为模的整数。我首次尝试解决此问题时使用了SymPy的mod_inv(n)函数。这个问题是我用大约30的矩阵耗尽了内存。我的下一个想法是执行矩阵分解,因为它可能对内存不太重。但是,SymPy似乎不包含可以找到模数的矩阵的求解器。我可以使用任何变通办法或自制代码吗?

1 个答案:

答案 0 :(得分:6)

sympy的{​​{1}}类支持模块化反转。这是模5的示例:

Matrix

用于查找行缩减梯形表单的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,该关键字指示矩阵中的哪些条目应被视为零。我相信预期的用途是数值稳定性(将小数字视为零),但我也用它来模块化减少。

以下是模5的示例:

iszerofunction

这是正确的,除了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 ] 返回的矩阵仍然有5个和分数。通过将mod和解释分数作为模块化反转来处理这个问题:

rref[0]
相关问题