使用Python执行模矩阵求逆的最简单方法?
我想在Python中采用矩阵的模逆,例如 [[1,2],[3,4]] mod 7 。我看过 numpy (它进行矩阵求逆,但不进行模矩阵求逆),并且在网上看到了一些数论包,但似乎没有任何东西可以执行这个相对常见的过程(至少,对我来说似乎相对常见)。
顺便说一句,上述矩阵的逆矩阵是 [[5,1],[5,3]] (mod 7)。不过我希望 Python 能为我做这件事。
I'd like to take the modular inverse of a matrix like [[1,2],[3,4]] mod 7 in Python. I've looked at numpy (which does matrix inversion but not modular matrix inversion) and I saw a few number theory packages online, but nothing that seems to do this relatively common procedure (at least, it seems relatively common to me).
By the way, the inverse of the above matrix is [[5,1],[5,3]] (mod 7). I'd like Python to do it for me though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧...对于那些关心的人,我解决了我自己的问题。我花了一段时间,但我认为这有效。它可能不是最优雅的,应该包括更多的错误处理,但它可以工作:
Okay...for those who care, I solved my own problem. It took me a while, but I think this works. It's probably not the most elegant, and should include some more error handling, but it works:
当舍入误差不是问题时有效的黑客技巧:
一个不太hacky的方法是实际实现高斯消除。这是我使用高斯消去法的代码,这是我为了自己的目的而编写的(舍入错误对我来说是一个问题)。 q 是模数,不一定是素数。
编辑:正如评论者指出的那样,在某些情况下该算法会失败。修复这个问题有点不简单,而且我现在没有时间。当时,在我的例子中,它适用于随机矩阵(模数是大素数的乘积)。基本上,第一个非零条目可能与模数不互质。主要情况很简单,因为您可以搜索不同的行并交换。在非素数情况下,我认为可能所有领先条目都不是相对素数,因此您必须将它们组合起来
A hackish trick which works when rounding errors aren't an issue:
A less hackish way is to actually implement gaussian elimination. Here's my code using Gaussian elimination, which I wrote for my own purposes (rounding errors were an issue for me). q is the modulus, which is not necessarily prime.
EDIT: as commenters point out, there are some cases this algorithm fails. It's slightly nontrivial to fix, and I don't have time nowadays. Back then it worked for random matrices in my case (the moduli were products of large primes). Basically, the first non-zero entry might not be relatively prime to the modulus. The prime case is easy since you can search for a different row and swap. In the non-prime case, I think it could be that all leading entries aren't relatively prime so you have to combine them
可以使用 Sage (www.sagemath.org) 计算:
虽然Sage安装起来很大,而且你必须使用它附带的python版本,这很痛苦。
It can be calculated using Sage (www.sagemath.org) as
Although Sage is huge to install and you have to use the version of python that comes with it which is a pain.
'sympy' 包 Matrix 类函数 'sqMatrix.inv_mod(mod)' 计算小模数和任意大模数的模矩阵逆。通过将 sympy 与 numpy 结合起来,计算二维 numpy 数组的模逆变得很容易(请参见下面的代码片段):
'sympy' package Matrix class function 'sqMatrix.inv_mod(mod)' computes modulo matrix inverse for small and arbitrarily large modulus. By combining sympy with numpy, it becomes easy to compute modulo inverse of 2-D numpy arrays (see the code snippet below):
不幸的是 numpy 没有模块化算术实现。您始终可以使用行缩减或行列式对建议的算法进行编码,如所示 在这里。模逆似乎对于密码学非常有用。
Unfortunately numpy does not have modular arithmetic implementations. You can always code up the proposed algorithm using row reduction or determinants as demonstrated here. A modular inverse seems to be quite useful for cryptography.
您可以使用此处的答案计算矩阵的共轭https://stackoverflow.com/a/75566371/4454877,然后除以行列式(或者更确切地说,乘以它的倒数!)并在最后取元素模。我不确定这是否受到舍入问题的影响。
主要缺点是对于较大的矩阵来说它似乎相当慢。
You can calculate the adjugate of the matrix using the answer here https://stackoverflow.com/a/75566371/4454877, then divide by the determinant (or rather, multiply by its inverse!) and take the elementwise modulus at the end. I'm unsure if this is affected by rounding issues.
The main downside is that it seems rather slow for larger matrices.