### Question:
Matrix extends Tensor<Matrix> { @Override public Matrix add(Matrix b) { if (rows() != b.rows() || cols() != b.cols()) { throw new ArithmeticException(formatWithLocale( "Matrix dimensions must match! Got dimensions (%d, %d) + (%d, %d)", rows(), cols(), b.rows(), b.cols() )); } var sum = zeros(); double[] localData = this.data; for (int i = 0; i < localData.length; ++i) { sum.data[i] = localData[i] + b.data[i]; } return sum; } Matrix(double[] data, int rows, int cols); static Matrix fill(double v, int rows, int cols); @Override Matrix zeros(); @Override Matrix copy(); @Override Matrix add(Matrix b);  }### Answer:
@Test void addsMatrix() { var matrix = new Matrix(new double[] { 1D, 2D }, 1, 2); var matrixToAdd = new Matrix(new double[] { 10D, 12D }, 1, 2); Matrix sum = matrix.add(matrixToAdd); assertNotSame(matrix, sum); assertNotSame(matrixToAdd, sum); assertArrayEquals(new double[]{ 11D, 14D}, sum.data); assertArrayEquals(matrix.dimensions, sum.dimensions); assertArrayEquals(matrixToAdd.dimensions, sum.dimensions); }
@Test void addsMatrixDifferentDimensions() { var matrix = new Matrix(new double[] { 1D, 2D }, 1, 2); var matrixToAdd = new Matrix(new double[] { 10D, 12D }, 2, 1); assertThrows(ArithmeticException.class, () -> matrix.add(matrixToAdd)); }