2015-08-26 127 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