2015-08-26 188 views
0

我有了下面的方法尋找小矩陣

private Matrix matrixMinors() 
    { 
    double[][] matrixM = new double[matrix.length][matrix.length]; 
    for(int i = 0; i < matrixM.length; i++) 
     for(int j = 0; j < matrixM.length; j++) 
     { 
      double[][] newone = new double[matrixM.length - 1][matrixM.length - 1]; 
      for(int k = 0; k < newone.length; k++) 
       for(int h = 0; h < newone[0].length; h++) 
        if(k == i) 
         ; 
        else if(h == j) 
         ; 
        else 
         newone[k][h] = matrix[k][h]; 
      test(newone, "little matrix"); //this just prints the matrix for debugging purposes 
      matrixM[i][j] = determinant(newone, newone.length); 
     } 
    test(matrixM, "minor matrix"); //this just prints the matrix for debugging purposes 
    return new Matrix(matrixM); 
} 

當打印較小的矩陣矩陣類具有全部爲零,任何建議如何解決此問題。

更新:

我的判定方法只保留打印零,但我不知道,如果這只是因爲我給它的數據使得零決定還是我的代碼是錯誤的。

private double determinant(double[][] mat, int size) 
    { 
    double det = 0; 
    if(size == 1) 
     det = mat[0][0]; 
    else if (size == 2) 
     det = mat[0][0] * mat[1][1] - mat[1][0] * mat[0][1]; 
    else 
    { 
     for(int j1 = 0; j1 < size; j1++) 
     { 
      double[][] m = new double[size-1][]; 
      for(int k = 0; k < (size-1); k++) 
       m[k] = new double[size-1]; 
      for(int i = 1; i < size; i++) 
      { 
       int j2 = 0; 
       for(int j = 0; j < size; j++) 
       { 
        if(j == j1) 
         continue; 
        m[i-1][j2] = mat[i][j]; 
        j2++; 
       } 
      } 
      det += Math.pow(-1.0, 1.0 + j1 + 1.0) * mat[0][j1] * determinant(m, size - 1); 
     } 
    } 
    return det; 
} 

回答

0

我的第一個建議是,以取代醜陋的if-else:

這一個:

if(k == i) 
; 
else if(h == j) 
; 
else 
newone[k][h] = matrix[k][h]; 

有了:

if(k!=i && h!=j) { 
    System.out.println("test: "+matrix[k][h]);//to see if it enters here 
    newone[k][h] = matrix[k][h]; 
} 

如果沒有打印測試,那麼邏輯是錯誤的。

我的第二個建議:
爲您的行列式方法編寫一個UnitTest,或者至少檢查它返回的結果(System.out可以提供幫助)。如果它總是返回0,那麼次矩陣當然是全零。

編輯:
您不需要將大小傳遞給行列式。你可以簡單地使用:

private double determinant(double[][] mat) 
{ 
    int size = mat.length; 

我測試的方法,像這樣幾個matixes:

final double[][] mat = new double[][] { { 1, 0, 1 }, { 0, 1, 0 }, { 2, 0, 1 } }; 
final double det = determinant(mat, 3); 
System.out.println("det: " + det); 

結果如預期。

由於newone未如預期,在這裏測試類,你可以使用:

public class TestMatrix 
{ 

    private static final double delta = 0.0001; 

    @Test 
    public void testDeterminant() 
    { 
     final double[][] mat = new double[][] { { 1.5, 2.7, 3.8 }, { -4.1, 5.4, -6.6 }, { 7.1, 8000, 9000 } }; 
     final double det = Matrix.determinant(mat, 3); 
     assertEquals(126817.786, det, delta); 
    } 

    // TODO add other tests! 

    @Test 
    public void testNewOne() throws Exception 
    { 
     final double[][] matrix = { { 3, 0, 2 }, { 2, 0, -2 }, { 0, 1, 1 } }; 
     final double[][] newOne = Matrix.newOne(matrix, 0, 0); 
     assertMatrix(new double[][] { { 0, -2 }, { 1, 1 } }, newOne); 
    } 

    private void assertMatrix(final double[][] ds, final double[][] newOne) 
    { 
     assertEquals(ds.length, newOne.length); 
     for (int i = 0; i < ds.length; i++) 
     { 
      assertEquals(ds[i].length, newOne[i].length); 
      for (int j = 0; j < ds[i].length; j++) 
      { 
       assertEquals(ds[i][j], newOne[i][j], delta); 
      } 
     } 
    } 
} 

我寫了一個newOne方法是這樣的:

public static double[][] newOne(final double[][] matrix, final int i, final int j) 
    { 
     final double[][] newone = new double[matrix.length - 1][matrix.length - 1]; 
     for(int k = 0; k < newone.length; k++) 
     { 
      for(int h = 0; h < newone[0].length; h++) 
       if (k != i && h != j) 
       { 
        newone[k][h] = matrix[k][h]; 
       } 
     } 
     return newone; 
    } 

你matrixMinor然後將:

//... 
for(int i = 0; i < matrixM.length; i++) 
    for(int j = 0; j < matrixM.length; j++) 
    { 
     double[][] newone = newOne(matrixM, i, j); 
     test(newone, "little matrix"); 
     matrixM[i][j]=determinant(newone, newone.length); 
    } 
//... 

然後只是改變方法newOne,直到UnitTest通過(更多的測試不會傷害,我只寫了一個向你展示如何完成)。

+0

我替換了我的if else,但仍然打印出相同的結果。 – Jacob

+0

測試正在打印,但不是4次喜歡它,通常只有一次或兩次。對於我的行列式,請參閱我的更新問題 – Jacob

+0

newone不像預期的那樣簡單地按照矩陣double [] []矩陣= {{3,0,2},{2,0,2},{0,1, 1}};新的第一次迭代應該是{{0,-2},{1,1}}。但它只是顯示爲零 – Jacob