@Test public void testMultiply() { Matrix matrix1 = new Matrix(2, 3, new double[]{1, 2, 3, 4, 5, 6}); Matrix matrix2 = new Matrix(3, 2, new double[]{3, 6, 2, 5, 1, 4}); Matrix matrix3 = new Matrix(2, 2, new double[]{10, 28, 28, 73}); Matrix matrix4 = matrix1.multiply(matrix2); Assert.assertTrue(matrix3.equals(matrix4)); Matrix matrix5 = matrix1.multiplyNative(matrix2); Assert.assertTrue(matrix3.equals(matrix5)); }
public Matrix multiply(Matrix matrix) { if (width != matrix.height) { throw new IllegalArgumentException("Invalid sizes"); } int index1 = 0, index3 = 0; Matrix newmatrix = new Matrix(height, matrix.width, null); for (int i = 0; i < height; i++) { for (int j = 0; j < matrix.width; j++) { double counter = 0.0; int index2 = 0; for (int k = 0; k < width; k++) { counter += data[index1 + k] * matrix.data[index2 + j]; index2 += matrix.width; } newmatrix.data[index3 + j] = counter; } index1 += width; index3 += matrix.width; } return newmatrix; }
Matrix { public Matrix multiply(Matrix matrix) { if (width != matrix.height) { throw new IllegalArgumentException("Invalid sizes"); } int index1 = 0, index3 = 0; Matrix newmatrix = new Matrix(height, matrix.width, null); for (int i = 0; i < height; i++) { for (int j = 0; j < matrix.width; j++) { double counter = 0.0; int index2 = 0; for (int k = 0; k < width; k++) { counter += data[index1 + k] * matrix.data[index2 + j]; index2 += matrix.width; } newmatrix.data[index3 + j] = counter; } index1 += width; index3 += matrix.width; } return newmatrix; } }
Matrix { public Matrix multiply(Matrix matrix) { if (width != matrix.height) { throw new IllegalArgumentException("Invalid sizes"); } int index1 = 0, index3 = 0; Matrix newmatrix = new Matrix(height, matrix.width, null); for (int i = 0; i < height; i++) { for (int j = 0; j < matrix.width; j++) { double counter = 0.0; int index2 = 0; for (int k = 0; k < width; k++) { counter += data[index1 + k] * matrix.data[index2 + j]; index2 += matrix.width; } newmatrix.data[index3 + j] = counter; } index1 += width; index3 += matrix.width; } return newmatrix; } Matrix(int height, int width, double[] data);  Matrix(int height, int width);  Matrix(int height, int width, double value); }
Matrix { public Matrix multiply(Matrix matrix) { if (width != matrix.height) { throw new IllegalArgumentException("Invalid sizes"); } int index1 = 0, index3 = 0; Matrix newmatrix = new Matrix(height, matrix.width, null); for (int i = 0; i < height; i++) { for (int j = 0; j < matrix.width; j++) { double counter = 0.0; int index2 = 0; for (int k = 0; k < width; k++) { counter += data[index1 + k] * matrix.data[index2 + j]; index2 += matrix.width; } newmatrix.data[index3 + j] = counter; } index1 += width; index3 += matrix.width; } return newmatrix; } Matrix(int height, int width, double[] data);  Matrix(int height, int width);  Matrix(int height, int width, double value); boolean equals(Matrix matrix); Matrix multiplyWithTime(Matrix matrix); Matrix multiply(Matrix matrix); Matrix multiplyNativeWithTime(Matrix matrix); Matrix multiplyNative(Matrix matrix); Matrix powerWithTime(int k); Matrix power(int k); Matrix powerNativeWithTime(int k); Matrix powerNative(int k); }
Matrix { public Matrix multiply(Matrix matrix) { if (width != matrix.height) { throw new IllegalArgumentException("Invalid sizes"); } int index1 = 0, index3 = 0; Matrix newmatrix = new Matrix(height, matrix.width, null); for (int i = 0; i < height; i++) { for (int j = 0; j < matrix.width; j++) { double counter = 0.0; int index2 = 0; for (int k = 0; k < width; k++) { counter += data[index1 + k] * matrix.data[index2 + j]; index2 += matrix.width; } newmatrix.data[index3 + j] = counter; } index1 += width; index3 += matrix.width; } return newmatrix; } Matrix(int height, int width, double[] data);  Matrix(int height, int width);  Matrix(int height, int width, double value); boolean equals(Matrix matrix); Matrix multiplyWithTime(Matrix matrix); Matrix multiply(Matrix matrix); Matrix multiplyNativeWithTime(Matrix matrix); Matrix multiplyNative(Matrix matrix); Matrix powerWithTime(int k); Matrix power(int k); Matrix powerNativeWithTime(int k); Matrix powerNative(int k);  }