如何实现二元方程的高斯消元法

Sal*_*ady 2 c# math binary


我有这个方程组
1=x\xe2\x8a\x95y\xe2\x8a\x95z
\n1=x\xe2\x8a\x95y\xe2\x8a\x95w
\n0=x\xe2\x8a\x95w\xe2\ x8a\x95z
\n1=w\xe2\x8a\x95y\xe2\x8a\x95z

我正在尝试实现高斯消除来解决这个系统,如此处所述用异或代替除法、减法和乘法,但它给出了我的错误答案..正确答案是 (x,y,z,w)=(0,1,0,0)
我做错了什么?

\n\n
public static void ComputeCoefficents(byte[,] X, byte[] Y)\n    {\n        int I, J, K, K1, N;\n        N = Y.Length;\n        for (K = 0; K < N; K++)\n        {\n            K1 = K + 1;\n            for (I = K; I < N; I++)\n            {\n                if (X[I, K] != 0)\n                {\n                    for (J = K1; J < N; J++)\n                    {\n                        X[I, J] /= X[I, K];\n                    }\n                    //Y[I] /= X[I, K];\n                    Y[I] ^= X[I, K];\n\n                }\n            }\n            for (I = K1; I < N; I++)\n            {\n                if (X[I, K] != 0)\n                {\n                    for (J = K1; J < N; J++)\n                    {\n                        X[I, J] ^= X[K, J];\n                    }\n                    Y[I] ^= Y[K];\n                }\n            }\n        }\n        for (I = N - 2; I >= 0; I--)\n        {\n            for (J = N - 1; J >= I + 1; J--)\n            {\n                //Y[I] -= AndOperation(X[I, J], Y[J]);\n                Y[I] ^= (byte)(X[I, J]* Y[J]);\n\n            }\n        }\n    } \n
Run Code Online (Sandbox Code Playgroud)\n

Mic*_*son 5

我认为你正在尝试为此应用高斯消除模 2。

一般来说,如果您的方程具有以下形式,您可以进行高斯消去模 k

a_1 * x + b_1 * y + c_1 * z = d_1
a_2 * x + b_2 * y + c_2 * z = d_2
a_3 * x + b_3 * y + c_3 * z = d_3
a_4 * x + b_4 * y + c_4 * z = d_4
Run Code Online (Sandbox Code Playgroud)

在 Z2 * isand和 + is中xor,因此您可以使用高斯消去法来求解以下形式的方程

x (xor) y (xor) z   = 1
x (xor) y (xor) w   = 1 
x (xor) z (xor) w   = 0
y (xor) z (xor) w   = 1
Run Code Online (Sandbox Code Playgroud)

让我们手动使用高斯消去法来计算这个方程。

相应的增广矩阵为:

 1 1 1 0 | 1
 1 1 0 1 | 1
 1 0 1 1 | 0
 0 1 1 1 | 1

 1 1 1 0 | 1
 0 0 1 1 | 0   (R2 = R2 + R1)
 0 1 0 1 | 1   (R3 = R3 + R1)
 0 1 1 1 | 1

 1 1 1 0 | 1
 0 1 1 1 | 1   (R2 = R4)
 0 1 0 1 | 1   
 0 0 1 1 | 0   (R4 = R2)

 1 0 0 1 | 0   (R1 = R1 + R2)
 0 1 1 1 | 1   
 0 0 1 0 | 0   (R3 = R3 + R2)   
 0 0 1 1 | 0   

 1 0 0 1 | 0
 0 1 0 1 | 1   (R2 = R2 + R3)  
 0 0 1 0 | 0      
 0 0 0 1 | 0   (R4 = R4 + R3)

 1 0 0 0 | 0   (R1 = R1 + R4)
 0 1 0 0 | 1   (R2 = R2 + R4)  
 0 0 1 0 | 0      
 0 0 0 1 | 0 
Run Code Online (Sandbox Code Playgroud)

给出 (x,y,z,w) = (0,1,0,0) 的解。

但这需要行旋转 - 我在您的代码中看不到。

代码中还存在一些可能不需要的乘法和除法。我希望代码如下所示:(您需要修复 TODO)。

public static void ComputeCoefficents(byte[,] X, byte[] Y) {
  int I, J, K, K1, N;
  N = Y.Length;

  for (K = 0; K < N; K++) {
    //First ensure that we have a non-zero entry in X[K,K]
    if( X[K,K] == 0 ) {
      for(int i = 0; i<N ; ++i ) { 
        if(X[i,K] != 0 ) {
             for( ... ) //TODO: A loop to swap the entries
             //TODO swap entries in Y too
           }
      }
    if( X[K,K] == 0 ) {
       // TODO: Handle the case where we have a zero column 
       //      - for now we just move on to the next column
       //      - This means we have no solutions or multiple 
       //        solutions
       continue
    }

    // Do full row elimination.
    for( int I = 0; I<N; ++I)
    {
       if( I!=K ){ //Don't self eliminate
         if( X[I,K] ) { 
           for( int J=K; J<N; ++J ) { X[I,J] = X[I,J] ^ X[K,J]; }
           Y[J] = Y[J] ^ Y[K];
         }
       }
    }
  }

  //Now assuming we didnt hit any zero columns Y should be our solution.
Run Code Online (Sandbox Code Playgroud)

}