【发布时间】:2015-11-04 20:35:57
【问题描述】:
我想知道模 2 中的高斯消元(或者甚至为此目的通常在模 k 中)是否曾经在某个地方实现过,这样我就不必重新发明轮子而只需使用可用资源?
【问题讨论】:
-
这不是重复的...我问的是 mod 2 中的高斯消除!
标签: python
我想知道模 2 中的高斯消元(或者甚至为此目的通常在模 k 中)是否曾经在某个地方实现过,这样我就不必重新发明轮子而只需使用可用资源?
【问题讨论】:
标签: python
您正在寻找的算法的伪代码存在并且是:
// A is n by m binary matrix
i := 1 // row and column index
for i := 1 to m do // for every column
// find non-zero element in column i, starting in row i:
maxi := i
for k := i to n do
if A[k,i] = 1 then maxi := k
end for
if A[maxi,i] = 1 then
swap rows i and maxi in A and b, but do not change the value of i
Now A[i,i] will contain the old value of A[maxi,i], that is 1
for u := i+1 to m do
Add A[u,i] * row i to row u, do this for BOTH, matrix A and RHS vector b
Now A[u,i] will be 0
end for
else
declare error – more than one solution exist
end if
end for
if n>m and if you can find zero row in A with nonzero RHS element, then
declare error – no solution.
end if
// now, matrix A is in upper triangular form and solution can be found
use back substitution to find vector x
取自pdf
二进制算术意味着模 2 的算术,如果我没记错的话,这就是您在问题中寻找的内容。
很遗憾我不会用 Python 写代码,但是如果你熟悉 Python,你可以简单地将上面的伪代码以你自己的方式逐行翻译成 Python,而这个任务应该是 很难也不很长。
我google了“高斯消除模2 python”,但没有找到你要找的python代码,但我认为这是好的,因为在翻译过程中你可能会更好地理解算法和方法。
编辑 1:如果您也熟悉 C#,并且将 C# 转换为 Python 并不费力,那么 Michael Anderson 对此question 的回答也可能对您有所帮助。
编辑2:发布答案后,我继续搜索,发现this
“在任何域上”意味着“超过模 2”,甚至对于任何 k≥2 的“超过模 k”。
它包含Java版本和Python版本的源代码。
根据我为 Python 版本提供的最后一个链接 fieldmath.py 包括 BinaryField 类,它应该是 模 2如你所愿。
享受吧!
我只是希望 Gauss-Jordan 消除 和 Gaussian Elimination 不是两个不同的东西。
编辑 3:如果您也熟悉 VC++,并且将 VC++ 翻译成 Python 对您来说不那么您也可以尝试this。
我希望这能很好地回答你的问题。
【讨论】: