2


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

我正在尝试实施高斯消元法来解决这个系统,如此所述,用 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]);

            }
        }
    } 
4

1 回答 1

4

我认为您正在尝试为此应用高斯消除模型 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 * 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

让我们手动使用高斯消元来做这个方程。

对应的增广矩阵为:

 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.

}

于 2012-07-18T07:06:24.617 回答