MatrixAlgebra { public Matrix<?> scale(final Matrix<?> m, final double scale) { Validate.notNull(m, "m"); if (m instanceof DoubleMatrix1D) { final double[] x = ((DoubleMatrix1D) m).getData(); final int n = x.length; final double[] scaled = new double[n]; for (int i = 0; i < n; i++) { scaled[i] = x[i] * scale; } return new DoubleMatrix1D(scaled); } else if (m instanceof DoubleMatrix2D) { final double[][] x = ((DoubleMatrix2D) m).getData(); final int n = x.length; final double[][] scaled = new double[n][x[0].length]; for (int i = 0; i < n; i++) { for (int j = 0; j < x[0].length; j++) { scaled[i][j] = x[i][j] * scale; } } return new DoubleMatrix2D(scaled); } 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 = NotImplementedException.class) public void testScaleWrongType() { ALGEBRA.scale(M5, 0.5); }
@Test public void testScale() { Matrix<?> m = ALGEBRA.scale(M1, 10); assertTrue(m instanceof DoubleMatrix1D); assertMatrixEquals(m, new DoubleMatrix1D(new double[] {10, 20 })); m = ALGEBRA.scale(m, 0.1); assertMatrixEquals(m, M1); m = ALGEBRA.scale(M3, 10); assertTrue(m instanceof DoubleMatrix2D); assertMatrixEquals(m, new DoubleMatrix2D(new double[][] {new double[] {10, 20 }, new double[] {30, 40 } })); m = ALGEBRA.scale(m, 0.1); assertMatrixEquals(m, M3); }