MatrixAlgebra { public Matrix<?> subtract(final Matrix<?> m1, final Matrix<?> m2) { Validate.notNull(m1, "m1"); Validate.notNull(m2, "m2"); if (m1 instanceof DoubleMatrix1D) { if (m2 instanceof DoubleMatrix1D) { final double[] x1 = ((DoubleMatrix1D) m1).getData(); final double[] x2 = ((DoubleMatrix1D) m2).getData(); final int n = x1.length; Validate.isTrue(n == x2.length, "Can only subtract matrices of the same shape"); final double[] sum = new double[n]; for (int i = 0; i < n; i++) { sum[i] = x1[i] - x2[i]; } return new DoubleMatrix1D(sum); } throw new IllegalArgumentException("Tried to subtract a " + m1.getClass() + " and " + m2.getClass()); } else if (m1 instanceof DoubleMatrix2D) { if (m2 instanceof DoubleMatrix2D) { final double[][] x1 = ((DoubleMatrix2D) m1).getData(); final double[][] x2 = ((DoubleMatrix2D) m2).getData(); final int n = x1.length; final int m = x1[0].length; Validate.isTrue(n == x2.length, "Can only subtract matrices of the same shape"); final double[][] sum = new double[n][x1[0].length]; for (int i = 0; i < n; i++) { Validate.isTrue(m == x2[i].length, "Can only subtract matrices of the same shape"); for (int j = 0; j < m; j++) { sum[i][j] = x1[i][j] - x2[i][j]; } } return new DoubleMatrix2D(sum); } throw new IllegalArgumentException("Tried to subtract a " + m1.getClass() + " and " + m2.getClass()); } throw new NotImplementedException(); }  Matrix<?> add(final Matrix<?> m1, final Matrix<?> m2); Matrix<?> divide(final Matrix<?> m1, final Matrix<?> m2); Matrix<?> kroneckerProduct(final Matrix<?> m1, final Matrix<?> m2); abstract Matrix<?> multiply(final Matrix<?> m1, final Matrix<?> m2); Matrix<?> scale(final Matrix<?> m, final double scale); Matrix<?> subtract(final Matrix<?> m1, final Matrix<?> m2); abstract double getCondition(final Matrix<?> m); abstract double getDeterminant(final Matrix<?> m); abstract DoubleMatrix2D getInverse(final Matrix<?> m); abstract double getInnerProduct(final Matrix<?> m1, final Matrix<?> m2); abstract DoubleMatrix2D getOuterProduct(final Matrix<?> m1, final Matrix<?> m2); abstract double getNorm1(final Matrix<?> m); abstract double getNorm2(final Matrix<?> m); abstract double getNormInfinity(final Matrix<?> m); abstract DoubleMatrix2D getPower(final Matrix<?> m, final int p); abstract DoubleMatrix2D getPower(Matrix<?> m, double p); abstract double getTrace(final Matrix<?> m); abstract DoubleMatrix2D getTranspose(final Matrix<?> m); DoubleMatrix2D matrixTransposeMultiplyMatrix(DoubleMatrix2D a);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSubtractWrongSize() { ALGEBRA.subtract(M1, M3); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSubtractDifferentRowNumber1D() { ALGEBRA.subtract(M1, new DoubleMatrix1D(new double[] {1, 2, 3 })); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSubtractDifferentRowNumber2D() { ALGEBRA.subtract(M3, new DoubleMatrix2D(new double[][] {new double[] {1, 2 }, new double[] {3, 4 }, new double[] {5, 6 } })); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSubtractDifferentColumnNumber2D() { ALGEBRA.subtract(M3, new DoubleMatrix2D(new double[][] {new double[] {1, 2, 3 }, new double[] {4, 5, 6 } })); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSubtractWrongType1() { ALGEBRA.subtract(M1, M5); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSubtractWrongType2() { ALGEBRA.subtract(M3, M5); }
@Test(expectedExceptions = NotImplementedException.class) public void testSubtractWrongType3() { ALGEBRA.subtract(M5, M5); }
@Test public void testSubtract() { Matrix<?> m = ALGEBRA.subtract(M1, M2); assertTrue(m instanceof DoubleMatrix1D); assertMatrixEquals(m, new DoubleMatrix1D(new double[] {-2, -2 })); m = ALGEBRA.subtract(M3, M4); assertTrue(m instanceof DoubleMatrix2D); assertMatrixEquals(m, new DoubleMatrix2D(new double[][] {new double[] {-4, -4 }, new double[] {-4, -4 } })); }