From b9fac81ac0cdf317781d419149225a525c4c630e Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 7 Feb 2022 19:30:03 +0000 Subject: [PATCH] Added adjoint and inverse functions --- .../mattrixwv/matrix/TestIntegerMatrix.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java index 7b64c0a..5b78031 100644 --- a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java @@ -1238,4 +1238,98 @@ public class TestIntegerMatrix{ {2475, 4950, 7425, 9900, 12375, 14850, 17325, 19800, 22275, 24750} }); } + + @Test + public void testAdjoint(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix correctMatrix = new IntegerMatrix(grid1); + assertEquals("IntegerMatrix 1x1 failed adjoint", correctMatrix, matrix.adjoint()); + + //2x2 + matrix = new IntegerMatrix(grid2); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, -2}, + {-1, 1} + }); + assertEquals("IntegerMatrix 2x2 failed adjoint", correctMatrix, matrix.adjoint()); + + //3x3 + matrix = new IntegerMatrix(grid3); + correctMatrix = new IntegerMatrix(3, 3, 0); + assertEquals("IntegerMatrix 3x3 failed adjoint", correctMatrix, matrix.adjoint()); + + //4x4 + matrix = new IntegerMatrix(new int[][]{ + {1, 2, 3, 4}, + {2, 3, 4, 1}, + {3, 4, 1, 2}, + {4, 1, 2, 3} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {-36, 4, 4, 44}, + {4, 4, 44, -36}, + {4, 44, -36, 4}, + {44, -36, 4, 4} + }); + assertEquals("IntegerMatrix 4x4 failed adjoint", correctMatrix, matrix.adjoint()); + + //10x10 + //?Skipping 10x10 test because test took > 5s by itself + /* + matrix = new IntegerMatrix(grid10); + correctMatrix = new IntegerMatrix(10, 10, 0); + assertEquals("IntegerMatrix 10x10 failed adjoint", correctMatrix, matrix.adjoint()); + */ + } + + @Test + public void testInverse(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix correctMatrix = new IntegerMatrix(grid1); + assertEquals("IntegerMatrix 1x1 failed inverse", correctMatrix, matrix.inverse()); + + //2x2 + matrix = new IntegerMatrix(new int[][]{ + {1, 4}, + {4, 1} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {-0, 0}, + {0, -0} + }); + assertEquals("IntegerMatrix 2x2 failed inverse", correctMatrix, matrix.inverse()); + + //3x3 + matrix = new IntegerMatrix(new int[][]{ + {1, 4, 2}, + {2, 4, 1}, + {4, 1, 2} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {-0, 0, 0}, + {0, 0, -0}, + {0, -0, 0} + }); + assertEquals("IntegerMatrix 3x3 failed inverse", correctMatrix, matrix.inverse()); + + //4x4 + matrix = new IntegerMatrix(new int[][]{ + {1, 2, 3, 4}, + {2, 3, 4, 1}, + {3, 4, 1, 2}, + {4, 1, 2, 3} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {-0, 0, 0, 0}, + {0, 0, 0, -0}, + {0, 0, -0, 0}, + {0, -0, 0, 0} + }); + assertEquals("IntegerMatrix 4x4 failed inverse", correctMatrix, matrix.inverse()); + + //10x10 + //?Skipped 10x10 because it would take a long time to compute + } }