From 14d0e026a6fe1aee0a960aa0caf67af0c94d0b3e Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 8 Feb 2022 22:27:17 +0000 Subject: [PATCH] Finished adding tests --- .../mattrixwv/matrix/TestDoubleMatrix.java | 633 +++++++++++++++++- 1 file changed, 617 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java index 95672ed..7838348 100644 --- a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java //Mattrixwv // Created: 02-07-22 -//Modified: 02-07-22 +//Modified: 02-08-22 package com.mattrixwv.matrix; @@ -117,39 +117,40 @@ public class TestDoubleMatrix{ public void testEquals(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); - assertTrue("DoubleMatrix 1x1 failed equals DoubleMatrix", matrix.equals(matrix)); + assertTrue("DoubleMatrix 1x1 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals = matrix.equals(grid1); assertTrue("DoubleMatrix 1x1 failed equals double[][]", gridEquals); //2x2 matrix = new DoubleMatrix(grid2); - assertTrue("DoubleMatrix 2x2 failed equals DoubleMatrix", matrix.equals(matrix)); + assertTrue("DoubleMatrix 2x2 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals2 = matrix.equals(grid2); assertTrue("DoubleMatrix 2x2 failed equals double[][]", gridEquals2); //3x3 matrix = new DoubleMatrix(grid3); - assertTrue("DoubleMatrix 3x3 failed equals DoubleMatrix", matrix.equals(matrix)); + assertTrue("DoubleMatrix 3x3 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals3 = matrix.equals(grid3); assertTrue("DoubleMatrix 3x3 failed equals double[][]", gridEquals3); //4x4 matrix = new DoubleMatrix(grid4); - assertTrue("DoubleMatrix 4x4 failed equals DoubleMatrix", matrix.equals(matrix)); + assertTrue("DoubleMatrix 4x4 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals4 = matrix.equals(grid4); assertTrue("DoubleMatrix 4x4 failed equals double[][]", gridEquals4); //10x10 matrix = new DoubleMatrix(grid10); - assertTrue("DoubleMatrix 10x10 failed equals DoubleMatrix", matrix.equals(matrix)); + assertTrue("DoubleMatrix 10x10 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals10 = matrix.equals(grid10); assertTrue("DoubleMatrix 10x10 failed equals double[][]", gridEquals10); } + @Test public void testGets(){ //1x1 @@ -222,6 +223,7 @@ public class TestDoubleMatrix{ }); assertEquals("DoubleMatrix 10x10 failed getCol", correctMatrix, matrix.getCol(0)); } + @Test public void testSets(){ //1x1 @@ -368,6 +370,7 @@ public class TestDoubleMatrix{ }); assertEquals("DoubleMatrix 10x10 failed setCol", correctMatrix, matrix); } + @Test public void testAdds(){ //1x1 @@ -480,6 +483,7 @@ public class TestDoubleMatrix{ }); assertEquals("DoubleMatrix 10x10 failed addCol", correctMatrix, matrix); } + @Test public void testAppends(){ //1x1 @@ -590,14 +594,15 @@ public class TestDoubleMatrix{ }); assertEquals("DoubleMatrix 10x10 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); } + @Test public void testAddition(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); - assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix", correctMatrix, matrix.add(transformMatrix)); - assertEquals("DoubleMatrix 1x1 failed add scalar", correctMatrix, matrix.add(0.5)); + assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); + assertEquals("DoubleMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(0.5)); //2x2 matrix = new DoubleMatrix(grid2); @@ -606,12 +611,12 @@ public class TestDoubleMatrix{ {1.0, 1.5}, {1.0, 1.5} }); - assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix", correctMatrix, matrix.add(transformMatrix)); + assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0}, {1.0, 2.0} }); - assertEquals("DoubleMatrix 2x2 failed add scalar", correctMatrix, matrix.add(0.5)); + assertEquals("DoubleMatrix 2x2 failed add scalar.", correctMatrix, matrix.add(0.5)); //3x3 matrix = new DoubleMatrix(grid3); @@ -621,13 +626,13 @@ public class TestDoubleMatrix{ {2.0, 2.0, 2.5}, {2.0, 2.0, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed add DoubleMatrix", correctMatrix, matrix.add(transformMatrix)); + assertEquals("DoubleMatrix 3x3 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0} }); - assertEquals("DoubleMatrix 3x3 failed add scalar", correctMatrix, matrix.add(0.5)); + assertEquals("DoubleMatrix 3x3 failed add scalar.", correctMatrix, matrix.add(0.5)); //4x4 matrix = new DoubleMatrix(grid4); @@ -638,14 +643,14 @@ public class TestDoubleMatrix{ {3.0, 3.0, 3.0, 3.5}, {3.0, 3.0, 3.0, 3.5} }); - assertEquals("DoubleMatrix 4x4 failed add DoubleMatrix", correctMatrix, matrix.add(transformMatrix)); + assertEquals("DoubleMatrix 4x4 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0} }); - assertEquals("DoubleMatrix 4x4 failed add scalar", correctMatrix, matrix.add(0.5)); + assertEquals("DoubleMatrix 4x4 failed add scalar.", correctMatrix, matrix.add(0.5)); //10x10 matrix = new DoubleMatrix(grid10); @@ -662,7 +667,7 @@ public class TestDoubleMatrix{ {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5} }); - assertEquals("DoubleMatrix 10x10 failed add DoubleMatrix", correctMatrix, matrix.add(transformMatrix)); + assertEquals("DoubleMatrix 10x10 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, @@ -675,14 +680,610 @@ public class TestDoubleMatrix{ {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} }); - assertEquals("DoubleMatrix 10x10 failed add scalar", correctMatrix, matrix.add(0.5)); + assertEquals("DoubleMatrix 10x10 failed add scalar.", correctMatrix, matrix.add(0.5)); } + @Test public void testSubtraction(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix transformMatrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(1, 1, 0); + assertEquals("DoubleMatrix 1x1 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + assertEquals("DoubleMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); + //2x2 + matrix = new DoubleMatrix(grid2); + transformMatrix = new DoubleMatrix(grid2); + correctMatrix = new DoubleMatrix(2, 2, 0); + assertEquals("DoubleMatrix 2x2 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 1.0}, + {0.0, 1.0} + }); + assertEquals("DoubleMatrix 2x2 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); + + //3x3 + matrix = new DoubleMatrix(grid3); + transformMatrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(3, 3, 0); + assertEquals("DoubleMatrix 3x3 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 1.0, 2.0}, + {0.0, 1.0, 2.0}, + {0.0, 1.0, 2.0} + }); + assertEquals("DoubleMatrix 3x3 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); + + //4x4 + matrix = new DoubleMatrix(grid4); + transformMatrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(4, 4, 0); + assertEquals("DoubleMatrix 4x4 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 1.0, 2.0, 3.0}, + {0.0, 1.0, 2.0, 3.0}, + {0.0, 1.0, 2.0, 3.0}, + {0.0, 1.0, 2.0, 3.0} + }); + assertEquals("DoubleMatrix 4x4 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); + + //10x10 + matrix = new DoubleMatrix(grid10); + transformMatrix = new DoubleMatrix(grid10); + correctMatrix = new DoubleMatrix(10, 10, 0); + assertEquals("DoubleMatrix 10x10 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, + {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0} + }); + assertEquals("DoubleMatrix 10x10 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); } + @Test public void testMultiplication(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); + assertEquals("DoubleMatrix 1x1 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); + assertEquals("DoubleMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); + //2x2 + matrix = new DoubleMatrix(grid2); + transformMatrix = new DoubleMatrix(transformGrid2_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {3.0, 5.0}, + {3.0, 5.0} + }); + assertEquals("DoubleMatrix 2x2 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + DoubleMatrix vector = new DoubleMatrix(new double[][]{ + {1.5}, + {2.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {4.5}, + {4.5} + }); + assertEquals("DoubleMatrix 2x2 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 3.0}, + {1.0, 3.0} + }); + assertEquals("DoubleMatrix 2x2 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); + + //3x3 + matrix = new DoubleMatrix(grid3); + transformMatrix = new DoubleMatrix(transformGrid3_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {6.75, 11.25, 15.75}, + {6.75, 11.25, 15.75}, + {6.75, 11.25, 15.75} + }); + assertEquals("DoubleMatrix 3x3 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + vector = new DoubleMatrix(new double[][]{ + {1.5}, + {2.5}, + {3.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {13.25}, + {13.25}, + {13.25} + }); + assertEquals("DoubleMatrix 3x3 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 3.0, 5.0}, + {1.0, 3.0, 5.0}, + {1.0, 3.0, 5.0} + }); + assertEquals("DoubleMatrix 3x3 failed multiplication vector.", correctMatrix, matrix.multiply(2.0)); + + //4x4 + matrix = new DoubleMatrix(grid4); + transformMatrix = new DoubleMatrix(transformGrid4_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {20, 12, 4, 0}, + {20, 12, 4, 0}, + {20, 12, 4, 0}, + {20, 12, 4, 0} + }); + assertEquals("DoubleMatrix 4x4 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + vector = new DoubleMatrix(new double[][]{ + {2.5}, + {1.5}, + {0.5}, + {0.0} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {4.75}, + {4.75}, + {4.75}, + {4.75} + }); + assertEquals("DoubleMatrix 4x4 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 3.0, 5.0, 7.0}, + {1.0, 3.0, 5.0, 7.0}, + {1.0, 3.0, 5.0, 7.0}, + {1.0, 3.0, 5.0, 7.0} + }); + assertEquals("DoubleMatrix 4x4 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); + + //10x10 + matrix = new DoubleMatrix(grid10); + transformMatrix = new DoubleMatrix(transformGrid10_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, + {75, 125, 175, 225, 275, 325, 375, 425, 475, 525} + }); + assertEquals("DoubleMatrix 10x10 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + vector = new DoubleMatrix(new double[][]{ + {1.5}, + {2.5}, + {3.5}, + {4.5}, + {5.5}, + {6.5}, + {7.5}, + {8.5}, + {9.5}, + {10.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {382.5}, + {382.5}, + {382.5}, + {382.5}, + {382.5}, + {382.5}, + {382.5}, + {382.5}, + {382.5}, + {382.5} + }); + assertEquals("DoubleMatrix 10x10 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, + {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0} + }); + assertEquals("DoubleMatrix 10x10 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); + } + + @Test + public void testDotProduct(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); + assertEquals("DoubleMatrix 1x1 failed dot product DoubleMatrix.", 0.75, matrix.dotProduct(transformMatrix), 0.0000001); + + //2x2 + matrix = new DoubleMatrix(grid2); + transformMatrix = new DoubleMatrix(transformGrid2_2); + assertEquals("DoubleMatrix 2x2 failed dot product DoubleMatrix.", 16, matrix.dotProduct(transformMatrix), 0.0000001); + + //3x3 + matrix = new DoubleMatrix(grid3); + transformMatrix = new DoubleMatrix(transformGrid3_2); + assertEquals("DoubleMatrix 3x3 failed dot product DoubleMatrix.", 101.25, matrix.dotProduct(transformMatrix), 0.0000001); + + //4x4 + matrix = new DoubleMatrix(grid4); + transformMatrix = new DoubleMatrix(transformGrid4_2); + assertEquals("DoubleMatrix 4x4 failed dot product DoubleMatrix.", 144, matrix.dotProduct(transformMatrix), 0.0000001); + + //10x10 + matrix = new DoubleMatrix(grid10); + transformMatrix = new DoubleMatrix(transformGrid10_2); + assertEquals("DoubleMatrix 10x10 failed dot product DoubleMatrix.", 30000, matrix.dotProduct(transformMatrix), 0.0000001); + } + + @Test + public void testHadamardProduct(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); + assertEquals("DoubleMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //2x2 + matrix = new DoubleMatrix(grid2); + transformMatrix = new DoubleMatrix(transformGrid2_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.75, 3.75}, + {0.75, 3.75} + }); + assertEquals("DoubleMatrix 2x2 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //3x3 + matrix = new DoubleMatrix(grid3); + transformMatrix = new DoubleMatrix(transformGrid3_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.75, 3.75, 8.75}, + {0.75, 3.75, 8.75}, + {0.75, 3.75, 8.75} + }); + assertEquals("DoubleMatrix 3x3 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //4x4 + matrix = new DoubleMatrix(grid4); + transformMatrix = new DoubleMatrix(transformGrid4_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.25, 2.25, 1.25, 0}, + {1.25, 2.25, 1.25, 0}, + {1.25, 2.25, 1.25, 0}, + {1.25, 2.25, 1.25, 0} + }); + assertEquals("DoubleMatrix 4x4 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //10x10 + matrix = new DoubleMatrix(grid10); + transformMatrix = new DoubleMatrix(transformGrid10_2); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, + {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75} + }); + assertEquals("DoubleMatrix 10x10 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + } + + @Test + public void testTranspose(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); + assertEquals("DoubleMatrix 1x1 failed transpose.", correctMatrix, matrix.transpose()); + + //2x2 + matrix = new DoubleMatrix(grid2); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 0.5}, + {1.5, 1.5} + }); + assertEquals("DoubleMatrix 2x2 failed transpose.", correctMatrix, matrix.transpose()); + + //3x3 + matrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 0.5, 0.5}, + {1.5, 1.5, 1.5}, + {2.5, 2.5, 2.5} + }); + assertEquals("DoubleMatrix 3x3 failed transpose.", correctMatrix, matrix.transpose()); + + //4x4 + matrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 0.5, 0.5, 0.5}, + {1.5, 1.5, 1.5, 1.5}, + {2.5, 2.5, 2.5, 2.5}, + {3.5, 3.5, 3.5, 3.5} + }); + assertEquals("DoubleMatrix 4x4 failed transpose.", correctMatrix, matrix.transpose()); + + //10x10 + matrix = new DoubleMatrix(grid10); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5}, + {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}, + {2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5}, + {3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5}, + {4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5}, + {5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5}, + {6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5}, + {7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5}, + {8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5}, + {9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5} + }); + assertEquals("DoubleMatrix 10x10 failed transpose.", correctMatrix, matrix.transpose()); + } + + @Test + public void testDeterminant(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + assertEquals("DoubleMatrix 1x1 failed determinant.", 0.5, matrix.determinant(), 0.0000001); + + //2x2 + matrix = new DoubleMatrix(grid2); + assertEquals("IntegerMatrix 2x2 failed determinant1.", 0, matrix.determinant(), 0.0000001); + matrix = new DoubleMatrix(new double[][]{ + {0.5, 3.5}, + {3.5, 0.5} + }); + assertEquals("DoubleMatrix 2x2 failed determinant2.", -12.0, matrix.determinant(), 0.0000001); + + //3x3 + matrix = new DoubleMatrix(grid3); + assertEquals("IntegerMatrix 3x3 failed determinant1.", 0, matrix.determinant(), 0.0000001); + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5}, + {1.5, 2.5, 0.5}, + {2.5, 0.5, 1.5} + }); + assertEquals("DoubleMatrix 3x3 failed determinant2.", -13.5, matrix.determinant(), 0.0000001); + + //4x4 + matrix = new DoubleMatrix(grid4); + assertEquals("DoubleMatrix 4x4 failed determinant1.", 0, matrix.determinant(), 0.0000001); + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {1.5, 2.5, 3.5, 0.5}, + {2.5, 3.5, 0.5, 1.5}, + {3.5, 0.5, 1.5, 2.5} + }); + assertEquals("DoubleMatrix 4x4 failed determinant2.", 128.0, matrix.determinant(), 0.0000001); + + //10x10 + matrix = new DoubleMatrix(grid10); + assertEquals("DoubleMatrix 10x10 failed determinant1.", 0, matrix.determinant(), 0.0000001); + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, + {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5}, + {2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5}, + {3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5}, + {4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5}, + {5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5}, + {6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5}, + {7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5}, + {8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5}, + {9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5}, + }); + assertEquals("DoubleMatrix 10x10 failed determinatn2.", -5000000000.0, matrix.determinant(), 0.0000001); + } + + @Test + public void testCofactor(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1}}); + assertEquals("DoubleMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); + + //2x2 + matrix = new DoubleMatrix(grid2); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.5, -0.5}, + {-1.5, 0.5} + }); + assertEquals("DoubleMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + + //3x3 + matrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(3, 3, 0); + assertEquals("DoubleMatrix 3x3 failed cofactor1.", correctMatrix, matrix.cofactor()); + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5}, + {1.5, 2.5, 0.5}, + {2.5, 0.5, 1.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {3.5, -1, -5.5}, + {-1, -5.5, 3.5}, + {-5.5, 3.5, -1} + }); + assertEquals("DoubleMatrix 3x3 failed cofactor2.", correctMatrix, matrix.cofactor()); + + //4x4 + matrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(4, 4, 0); + assertEquals("DoubleMatrix 4x4 failed cofactor1.", correctMatrix, matrix.cofactor()); + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {1.5, 2.5, 3.5, 0.5}, + {2.5, 3.5, 0.5, 1.5}, + {3.5, 0.5, 1.5, 2.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {-28, 4, 4, 36}, + {4, 4, 36, -28}, + {4, 36, -28, 4}, + {36, -28, 4, 4} + }); + assertEquals("DoubleMatrix 4x4 failed cofactor2.", correctMatrix, matrix.cofactor()); + + //10x10 + //?Skipping 10x10 test because test took > 5s by itself + /* + matrix = new DoubleMatrix(grid10); + correctMatrix = new DoubleMatrix(10, 10, 0); + assertEquals("DoubleMatrix 10x10 failed cofactor1.", correctMatrix, matrix.cofactor()); + */ + } + + @Test + public void testPower(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.125}}); + assertEquals("DoubleMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); + + //2x2 + matrix = new DoubleMatrix(grid2); + correctMatrix = new DoubleMatrix(new double[][]{ + {2, 6}, + {2, 6} + }); + assertEquals("DoubleMatrix 2x2 failed power.", correctMatrix, matrix.pow(3)); + + //3x3 + matrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(new double[][]{ + {10.125, 30.375, 50.625}, + {10.125, 30.375, 50.625}, + {10.125, 30.375, 50.625} + }); + assertEquals("DoubleMatrix 3x3 failed power.", correctMatrix, matrix.pow(3)); + + //4x4 + matrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(new double[][]{ + {32.0, 96.0, 160.0, 224.0}, + {32.0, 96.0, 160.0, 224.0}, + {32.0, 96.0, 160.0, 224.0}, + {32.0, 96.0, 160.0, 224.0} + }); + assertEquals("DoubleMatrix 4x4 failed power.", correctMatrix, matrix.pow(3)); + + //10x10 + matrix = new DoubleMatrix(grid10); + correctMatrix = new DoubleMatrix(new double[][]{ + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, + {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750} + }); + assertEquals("DoubleMatrix 10x10 failed power.", correctMatrix, matrix.pow(3)); + } + + @Test + public void testAdjoint(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); + assertEquals("DoubleMatrix 1x1 failed adjoint.", correctMatrix, matrix.adjoint()); + + //2x2 + matrix = new DoubleMatrix(grid2); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.5, -1.5}, + {-0.5, 0.5} + }); + assertEquals("DoubleMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + + //3x3 + matrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(3, 3, 0); + assertEquals("DoubleMatrix 3x3 failed adjoint.", correctMatrix, matrix.adjoint()); + + //4x4 + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {1.5, 2.5, 3.5, 0.5}, + {2.5, 3.5, 0.5, 1.5}, + {3.5, 0.5, 1.5, 2.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {-28, 4, 4, 36}, + {4, 4, 36, -28}, + {4, 36, -28, 4}, + {36, -28, 4, 4} + }); + assertEquals("DoubleMatrix 4x4 failed adjoint.", correctMatrix, matrix.adjoint()); + + //10x10 + //?Skippng 10x10 test because test took > 5s by itself + /* + matrix = new DoubleMatrix(grid10); + correctMatrix = new DoubleMatrix(10, 10, 0); + assertEquals("DoubleMatrix 10x10 failed adjoint.", correctMatrix, matrix.adjoint()); + */ + } + + @Test + public void testInverse(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{2.0}}); + assertEquals("DoubleMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); + + //2x2 + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5}, + {1.5, 0.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {-0.25, 0.75}, + {0.75, -0.25} + }); + assertEquals("DoubleMatrix 2x2 failed inverse.", correctMatrix, matrix.inverse()); + + //3x3 + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5}, + {1.5, 2.5, 0.5}, + {2.5, 0.5, 1.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {-7.0/27.0, 2.0/27.0, 11.0/27.0}, + {2.0/27.0, 11.0/27.0, -7.0/27.0}, + {11.0/27.0, -7.0/27.0, 2.0/27.0} + }); + assertEquals("DoubleMatrix 3x3 failed inverse.", correctMatrix, matrix.inverse()); + + //4x4 + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {1.5, 2.5, 3.5, 0.5}, + {2.5, 3.5, 0.5, 1.5}, + {3.5, 0.5, 1.5, 2.5} + }); + correctMatrix = new DoubleMatrix(new double[][]{ + {-0.21875, 0.03125, 0.03125, 0.28125}, + {0.03125, 0.03125, 0.28125, -0.21875}, + {0.03125, 0.28125, -0.21875, 0.03125}, + {0.28125, -0.21875, 0.03125, 0.03125} + }); + assertEquals("DoubleMatrix 4x4 failed inverse.", correctMatrix, matrix.inverse()); + + //10x10 + //?Skipped 10x10 because it would take a long time to compute } }