DoubleMatrixUtils { public static DoubleMatrix2D getIdentityMatrix2D(final int dimension) { Validate.isTrue(dimension >= 0, "dimension must be >= 0"); if (dimension == 0) { return DoubleMatrix2D.EMPTY_MATRIX; } if (dimension == 1) { return new DoubleMatrix2D(new double[][] {new double[] {1}}); } final double[][] data = new double[dimension][dimension]; for (int i = 0; i < dimension; i++) { data[i][i] = 1; } return new DoubleMatrix2D(data); } private  DoubleMatrixUtils(); static DoubleMatrix2D getTranspose(final DoubleMatrix2D matrix); static DoubleMatrix2D getIdentityMatrix2D(final int dimension); static DoubleMatrix2D getTwoDimensionalDiagonalMatrix(final DoubleMatrix1D vector); static DoubleMatrix2D getTwoDimensionalDiagonalMatrix(final double[] vector);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeDimension() { DoubleMatrixUtils.getIdentityMatrix2D(-3); }
@Test public void testIdentity() { assertEquals(DoubleMatrixUtils.getIdentityMatrix2D(0), DoubleMatrix2D.EMPTY_MATRIX); assertEquals(DoubleMatrixUtils.getIdentityMatrix2D(1), new DoubleMatrix2D(new double[][] {new double[] {1}})); assertEquals(DoubleMatrixUtils.getIdentityMatrix2D(4), new DoubleMatrix2D(new double[][] {new double[] {1, 0, 0, 0}, new double[] {0, 1, 0, 0}, new double[] {0, 0, 1, 0}, new double[] {0, 0, 0, 1}})); }