模2 python代码中的高斯消除

时间:2015-11-04 20:35:58

标签: python

我想知道模2中的高斯消除(或者甚至通常以模k为目的)是否已经在某处实现过,因此我不必重新发明轮子并只使用可用的资源?

1 个答案:

答案 0 :(得分:2)

您正在寻找的算法的伪代码存在并且是:

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

我用谷歌搜索“高斯消除modulo 2 python”,但没有找到你正在寻找的python代码,但我认为这是好的,因为在翻译过程中你可以更好地理解算法和方法。 / p>

编辑1:如果您对C#也很熟悉并且没有将C#转换为Python的努力,那么Michael Anderson对此question的回答也可能对您有所帮助。

编辑2:发布答案后,我继续搜索并找到this

对于任何k≥2,

“任何字段”意味着“超过模2”,甚至“超过模k”。

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

根据我给你的Python版本的最后一个链接 fieldmath.py 包括类 BinaryField ,它被认为是 modulo 2 如你所愿。

享受!

我只是希望高斯 - 乔丹消除高斯消除不是两回事。

编辑3:如果你也熟悉VC ++并且将VC ++翻译成Python,那么你可以尝试{strong> 那么努力,那么你也可以尝试this

我希望这能很好地回答你的问题。