DiagonalMatrix extends AbstractRealMatrix implements Serializable { public DiagonalMatrix add(final DiagonalMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] + m.data[i]; } return new DiagonalMatrix(outData, false); } DiagonalMatrix(final int dimension);  DiagonalMatrix(final double[] d);  DiagonalMatrix(final double[] d, final boolean copyArray); @Override RealMatrix createMatrix(final int rowDimension,
                                   final int columnDimension); @Override RealMatrix copy(); DiagonalMatrix add(final DiagonalMatrix m); DiagonalMatrix subtract(final DiagonalMatrix m); DiagonalMatrix multiply(final DiagonalMatrix m); @Override RealMatrix multiply(final RealMatrix m); @Override double[][] getData(); double[] getDataRef(); @Override double getEntry(final int row, final int column); @Override void setEntry(final int row, final int column, final double value); @Override void addToEntry(final int row,
                           final int column,
                           final double increment); @Override void multiplyEntry(final int row,
                              final int column,
                              final double factor); @Override int getRowDimension(); @Override int getColumnDimension(); @Override double[] operate(final double[] v); @Override double[] preMultiply(final double[] v); @Override RealVector preMultiply(final RealVector v); DiagonalMatrix inverse(); DiagonalMatrix inverse(double threshold); boolean isSingular(double threshold);  }
@Test public void testAdd() { final double[] data1 = { -1.2, 3.4, 5 }; final DiagonalMatrix m1 = new DiagonalMatrix(data1); final double[] data2 = { 10.1, 2.3, 45 }; final DiagonalMatrix m2 = new DiagonalMatrix(data2); final DiagonalMatrix result = m1.add(m2); Assert.assertEquals(m1.getRowDimension(), result.getRowDimension()); for (int i = 0; i < result.getRowDimension(); i++) { for (int j = 0; j < result.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(data1[i] + data2[i], result.getEntry(i, j), 0d); } else { Assert.assertEquals(0d, result.getEntry(i, j), 0d); } } } }