//Matrix/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java //Mattrixwv // Created: 02-07-22 //Modified: 02-09-22 package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; public class TestDoubleMatrix{ //Grid 1x1 private static final double[][] grid1 = { {0.5} }; private static final double[][] transformGrid1_1 = { {0.5} }; private static final double[][] transformGrid1_2 = { {1.5} }; //Grid 2x2 private static final double[][] grid2 = { {0.5, 1.5}, {0.5, 1.5} }; private static final double[][] transformGrid2_1 = { {0.5, 0}, {0.5, 0} }; private static final double[][] transformGrid2_2 = { {1.5, 2.5}, {1.5, 2.5} }; //Grid 3x3 private static final double[][] grid3 = { {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }; private static final double[][] transformGrid3_1 = { {1.5, 0.5, 0}, {1.5, 0.5, 0}, {1.5, 0.5, 0} }; private static final double[][] transformGrid3_2 = { {1.5, 2.5, 3.5}, {1.5, 2.5, 3.5}, {1.5, 2.5, 3.5} }; //Grid 4x4 private static final double[][] grid4 = { {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }; private static final double[][] transformGrid4_1 = { {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0} }; private static final double[][] transformGrid4_2 = { {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0} }; //Grid 10x10 private static final double[][] grid10 = { {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }; private static final double[][] transformGrid10_1 = { {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0} }; private static final double[][] transformGrid10_2 = { {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} }; @Test public void testEquals(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); 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)); @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)); @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)); @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)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals10 = matrix.equals(grid10); assertTrue("DoubleMatrix 10x10 failed equals double[][].", gridEquals10); } @Test public void testGets(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); assertEquals("DoubleMatrix 1x1 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals("DoubleMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals("DoubleMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); //2x2 matrix = new DoubleMatrix(grid2); assertEquals("DoubleMatrix 2x2 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5}}); assertEquals("DoubleMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} }); assertEquals("DoubleMatrix 2x2 failed getCol.", correctMatrix, matrix.getCol(0)); //3x3 matrix = new DoubleMatrix(grid3); assertEquals("DoubleMatrix 3x3 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5}}); assertEquals("DoubleMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, {0.5} }); assertEquals("DoubleMatrix 3x3 failed getCol.", correctMatrix, matrix.getCol(0)); //4x4 matrix = new DoubleMatrix(grid4); assertEquals("DoubleMatrix 4x4 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5}}); assertEquals("DoubleMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, {0.5}, {0.5} }); assertEquals("DoubleMatrix 4x4 failed getCol.", correctMatrix, matrix.getCol(0)); //10x10 matrix = new DoubleMatrix(grid10); assertEquals("DoubleMatrix 10x10 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}}); assertEquals("DoubleMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); //GetCol 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} }); assertEquals("DoubleMatrix 10x10 failed getCol.", correctMatrix, matrix.getCol(0)); } @Test public void testSets(){ //1x1 //Set DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.set(0, 0, 1.5); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.5}}); assertEquals("DoubleMatrix 1x1 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{0.0}); correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); assertEquals("DoubleMatrix 1x1 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0.5}); correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals("DoubleMatrix 1x1 failed setCol.", correctMatrix, matrix); //2x2 //Set matrix = new DoubleMatrix(grid2); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(1, new double[]{1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {1.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed setRow.", correctMatrix, matrix); //SetCol matrix = new DoubleMatrix(grid2); matrix.setCol(0, new double[]{2.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {2.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed setCol.", correctMatrix, matrix); //3x3 //Set matrix = new DoubleMatrix(grid3); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{0, 0.5, 1.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0, 0.5, 1.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0, 0, 0}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 0.5, 1.5}, {0.0, 1.5, 2.5}, {0.0, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed setCol.", correctMatrix, matrix); //4x4 //Set matrix = new DoubleMatrix(grid4); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{3.5, 2.5, 1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {3.5, 2.5, 1.5, 0.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0, 0, 0, 0}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 2.5, 1.5, 0.5}, {0.0, 1.5, 2.5, 3.5}, {0.0, 1.5, 2.5, 3.5}, {0.0, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 //Set matrix = new DoubleMatrix(grid10); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed setCol.", correctMatrix, matrix); } @Test public void testAdds(){ //1x1 //AddRow DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.addRow(new double[]{0.5}); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}, {0.5}}); assertEquals("DoubleMatrix 1x1 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid1); matrix.addCol(new double[]{0.5}); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); assertEquals("DoubleMatrix 1x1 failed addCol.", correctMatrix, matrix); //2x2 //AddRow matrix = new DoubleMatrix(grid2); matrix.addRow(new double[]{0.5, 1.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {0.5, 1.5}, {0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid2); matrix.addCol(new double[]{2.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 2x2 failed addCol.", correctMatrix, matrix); //3x3 //AddRow matrix = new DoubleMatrix(grid3); matrix.addRow(new double[]{0.5, 1.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid3); matrix.addCol(new double[]{3.5, 3.5, 3.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 //AddRow matrix = new DoubleMatrix(grid4); matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid4); matrix.addCol(new double[]{4.5, 4.5, 4.5, 4.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5} }); assertEquals("DoubleMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 //AddRow matrix = new DoubleMatrix(grid10); matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid10); matrix.addCol(new double[]{10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} }); assertEquals("DoubleMatrix 10x10 failed addCol.", correctMatrix, matrix); } @Test public void testAppends(){ //1x1 //appendRight DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix secondMatrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); assertEquals("DoubleMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} }); assertEquals("DoubleMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 //appendRight matrix = new DoubleMatrix(grid2); secondMatrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 0.5, 1.5}, {0.5, 1.5, 0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {0.5, 1.5}, {0.5, 1.5}, {0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 //appendRight matrix = new DoubleMatrix(grid3); secondMatrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 0.5, 1.5, 2.5}, {0.5, 1.5, 2.5, 0.5, 1.5, 2.5}, {0.5, 1.5, 2.5, 0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 //appendRight matrix = new DoubleMatrix(grid4); secondMatrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 //appendRight matrix = new DoubleMatrix(grid10); secondMatrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); 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)); //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_1); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 1.5}, {1.0, 1.5} }); 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)); //3x3 matrix = new DoubleMatrix(grid3); transformMatrix = new DoubleMatrix(transformGrid3_1); correctMatrix = new DoubleMatrix(new double[][]{ {2.0, 2.0, 2.5}, {2.0, 2.0, 2.5}, {2.0, 2.0, 2.5} }); 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)); //4x4 matrix = new DoubleMatrix(grid4); transformMatrix = new DoubleMatrix(transformGrid4_1); correctMatrix = new DoubleMatrix(new double[][]{ {3.0, 3.0, 3.0, 3.5}, {3.0, 3.0, 3.0, 3.5}, {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)); 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)); //10x10 matrix = new DoubleMatrix(grid10); transformMatrix = new DoubleMatrix(transformGrid10_1); correctMatrix = new DoubleMatrix(new double[][]{ {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}, {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}, {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}, {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}, {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)); 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}, {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}, {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}, {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}, {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)); } @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 } }