Added function to calculate determinants

This commit is contained in:
2022-02-03 22:02:33 +00:00
parent 850af24aa2
commit 785c23ee5f

View File

@@ -1059,4 +1059,58 @@ public class TestIntegerMatrix{
}); });
assertEquals("IntegerMatrix 10x10 failed transpose", correctMatrix, matrix.transpose()); assertEquals("IntegerMatrix 10x10 failed transpose", correctMatrix, matrix.transpose());
} }
@Test
public void testDeterminant(){
//1x1
IntegerMatrix matrix = new IntegerMatrix(grid1);
assertEquals("IntegerMatrix 1x1 failed determinant", 1, matrix.determinant());
//2x2
matrix = new IntegerMatrix(grid2);
assertEquals("IntegerMatrix 2x2 failed determinant1", 0, matrix.determinant());
matrix = new IntegerMatrix(new int[][]{
{1, 4},
{4, 1}
});
assertEquals("IntegerMatrix 2x2 failed determinant2", -15, matrix.determinant());
//3x3
matrix = new IntegerMatrix(grid3);
assertEquals("IntegerMatrix 3x3 failed determinant1", 0, matrix.determinant());
matrix = new IntegerMatrix(new int[][]{
{1, 4, 2},
{2, 4, 1},
{4, 1, 2}
});
assertEquals("IntegerMatrix 3x3 failed determinant2", -21, matrix.determinant());
//4x4
matrix = new IntegerMatrix(grid4);
assertEquals("IntegerMatrix 4x4 failed determiant1", 0, matrix.determinant());
matrix = new IntegerMatrix(new int[][]{
{1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}
});
assertEquals("IntegerMatrix 4x4 failed determinant2", 160, matrix.determinant());
//10x10
matrix = new IntegerMatrix(grid10);
assertEquals("IntegerMatrix 10x10 failed determinant1", 0, matrix.determinant());
matrix = new IntegerMatrix(new int[][]{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 1},
{3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
{4, 5, 6, 7, 8, 9, 10, 1, 2, 3},
{5, 6, 7, 8, 9, 10, 1, 2, 3, 4},
{6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
{7, 8, 9, 10, 1, 2, 3, 4, 5, 6},
{8, 9, 10, 1, 2, 3, 4, 5, 6, 7},
{9, 10, 1, 2, 3, 4, 5, 6, 7, 8},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
});
assertEquals("IntegerMatrix 10x10 failed determinant2", -10000000, matrix.determinant());
}
} }