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

时间:2012-07-14 12:54:38

标签: c# math binary

我有这个方程组 1 =x⊕y⊕z
1 =x⊕y⊕w
0 =x⊕w⊕z
1 =w⊕y⊕z

我试图用here来实现高斯消元来解决这个系统,用XOR代替除法,减法和乘法,但它给出了我错误的答案..正确的答案是(x,y,z,w)=(0,1,0,0)我做错了什么?

public static void ComputeCoefficents(byte[,] X, byte[] Y)
    {
        int I, J, K, K1, N;
        N = Y.Length;
        for (K = 0; K < N; K++)
        {
            K1 = K + 1;
            for (I = K; I < N; I++)
            {
                if (X[I, K] != 0)
                {
                    for (J = K1; J < N; J++)
                    {
                        X[I, J] /= X[I, K];
                    }
                    //Y[I] /= X[I, K];
                    Y[I] ^= X[I, K];

                }
            }
            for (I = K1; I < N; I++)
            {
                if (X[I, K] != 0)
                {
                    for (J = K1; J < N; J++)
                    {
                        X[I, J] ^= X[K, J];
                    }
                    Y[I] ^= Y[K];
                }
            }
        }
        for (I = N - 2; I >= 0; I--)
        {
            for (J = N - 1; J >= I + 1; J--)
            {
                //Y[I] -= AndOperation(X[I, J], Y[J]);
                Y[I] ^= (byte)(X[I, J]* Y[J]);

            }
        }
    } 

1 个答案:

答案 0 :(得分:1)

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

一般来说,如果你的方程是

形式,你可以做高斯消元mod 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

在Z2 *中是and而+是xor,所以你可以使用Gausian消除来求解形式的方程式

x (xor) y (xor) z   = 1
x (xor) y (xor) w   = 1 
x (xor) z (xor) w   = 0
y (xor) z (xor) w   = 1

让我们手动使用Gausian消除这个等式。

相应的增广矩阵是:

 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 

给出(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.

}