class 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();
  }

}