模2 python代码中的高斯消去



我想知道模2中的高斯消去法(甚至通常在模k中)是否已经在某个地方实现过,这样我就不必重新发明轮子,只需使用可用的资源?

您要查找的算法的伪代码存在,并且是:

    // 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,这个任务应该既不难也不长。

我在谷歌上搜索了"高斯消除模2 python",但没有找到你想要的python代码,但我认为这是好的,因为在翻译过程中,你可能会更好地理解算法和方法。

第1版:如果你也熟悉C#,并且把C#翻译成Python并不费力,那么Michael Anderson对这个问题的回答也可能对你有所帮助。

编辑2:发布答案后,我继续搜索,找到了这个

"在任何域上"意味着"在模2上",甚至对于任何k&ge都是"在模k上";2.

它还包含Java版本和Python版本的源代码。

根据我为Python版本fieldmath.py提供的最后一个链接,它包括类BinaryField,假设它是模2

享受吧!

我只是希望高斯-乔丹消去Gaussian消去

第3版:如果你也熟悉VC++,并且将VC++翻译成Python对你来说是一项n’t的工作,那么你也可以尝试一下。

我希望这口井能回答你的问题。

最新更新