Skip to content

Commit 414200e

Browse files
committed
clean the code
1 parent 1a58232 commit 414200e

2 files changed

Lines changed: 31 additions & 43 deletions

File tree

src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ private MatrixMultiplication() {
77
/**
88
* Multiplies two matrices.
99
*
10-
* @param matrixA the first matrix rowsA x colsA
11-
* @param matrixB the second matrix rowsB x colsB
12-
* @return the product of the two matrices rowsA x colsB
10+
* @param matrixA the first matrix rowsA x colsA
11+
* @param matrixB the second matrix rowsB x colsB
12+
* @return the product of the two matrices rowsA x colsB
1313
* @throws IllegalArgumentException if the matrices cannot be multiplied
1414
*/
1515
public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
@@ -19,8 +19,10 @@ public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
1919
}
2020

2121
// Check for empty matrices
22-
if (matrixA.length == 0 || matrixB.length == 0 ||
23-
matrixA[0].length == 0 || matrixB[0].length == 0) {
22+
if (matrixA.length == 0
23+
|| matrixB.length == 0
24+
|| matrixA[0].length == 0
25+
|| matrixB[0].length == 0) {
2426
throw new IllegalArgumentException("Input matrices must not be empty");
2527
}
2628

src/test/java/com/thealgorithms/matrix/MatrixMultiplicationTest.java

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@
22

33
import org.junit.jupiter.api.Test;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
5-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
65
import static org.junit.jupiter.api.Assertions.assertEquals;
7-
8-
import static org.junit.jupiter.api.Assertions.*;
9-
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
107

118
public class MatrixMultiplicationTest {
129

1310
private static final double EPSILON = 1e-9; // for floating point comparison
1411

15-
1612
@Test
17-
void testMultiply2by2(){
18-
double[][] matrixA = {{1.0,2.0},{3.0,4.0}};
19-
double[][] matrixB = {{5.0,6.0},{7.0,8.0}};
13+
void testMultiply2by2() {
14+
double[][] matrixA = {{1.0, 2.0}, {3.0, 4.0}};
15+
double[][] matrixB = {{5.0, 6.0}, {7.0, 8.0}};
2016
double[][] expected = {{19.0, 22.0}, {43.0, 50.0}};
2117

2218
double[][] result = MatrixMultiplication.multiply(matrixA, matrixB);
23-
assertMatrixEquals(expected, result); // Because assertEquals can fails due to floating point precision issues, Therfore use assertMatrixEquals
19+
assertMatrixEquals(expected, result); // Use custom method due to floating point issues
2420
}
2521

2622
@Test
27-
void testMultiply3by2and2by1(){
28-
double[][] matrixA = {{1.0,2.0},{3.0,4.0},{5.0,6.0}};
29-
double[][] matrixB = {{7.0},{8.0}};
23+
void testMultiply3by2and2by1() {
24+
double[][] matrixA = {{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};
25+
double[][] matrixB = {{7.0}, {8.0}};
3026
double[][] expected = {{23.0}, {53.0}, {83.0}};
3127

3228
double[][] result = MatrixMultiplication.multiply(matrixA, matrixB);
@@ -35,61 +31,51 @@ void testMultiply3by2and2by1(){
3531

3632
@Test
3733
void testNullMatrixA() {
38-
double[][] B = {{1, 2}, {3, 4}};
39-
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(null, B));
34+
double[][] b = {{1, 2}, {3, 4}};
35+
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(null, b));
4036
}
4137

4238
@Test
4339
void testNullMatrixB() {
44-
double[][] A = {{1, 2}, {3, 4}};
45-
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(A, null));
40+
double[][] a = {{1, 2}, {3, 4}};
41+
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, null));
4642
}
4743

48-
4944
@Test
50-
void testMultiplyNull(){
51-
double[][] matrixA = {{1.0,2.0},{3.0,4.0}};
45+
void testMultiplyNull() {
46+
double[][] matrixA = {{1.0, 2.0}, {3.0, 4.0}};
5247
double[][] matrixB = null;
5348

54-
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
55-
MatrixMultiplication.multiply(matrixA, matrixB);
56-
});
49+
Exception exception = assertThrows(IllegalArgumentException.class,
50+
() -> MatrixMultiplication.multiply(matrixA, matrixB));
5751

5852
String expectedMessage = "Input matrices cannot be null";
5953
String actualMessage = exception.getMessage();
6054

6155
assertTrue(actualMessage.contains(expectedMessage));
6256
}
6357

64-
6558
@Test
6659
void testIncompatibleDimensions() {
67-
double[][] A = {
68-
{1.0, 2.0}
69-
};
70-
double[][] B = {
71-
{1.0, 2.0}
72-
};
73-
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(A, B));
60+
double[][] a = {{1.0, 2.0}};
61+
double[][] b = {{1.0, 2.0}};
62+
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, b));
7463
}
7564

7665
@Test
7766
void testEmptyMatrices() {
78-
double[][] A = new double[0][0];
79-
double[][] B = new double[0][0];
80-
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(A, B));
67+
double[][] a = new double[0][0];
68+
double[][] b = new double[0][0];
69+
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, b));
8170
}
8271

83-
8472
private void assertMatrixEquals(double[][] expected, double[][] actual) {
8573
assertEquals(expected.length, actual.length, "Row count mismatch");
8674
for (int i = 0; i < expected.length; i++) {
87-
assertEquals(expected[i].length, actual[i].length, "Column count mismatch at row " + i); // Check if the number of columns in each row matches
75+
assertEquals(expected[i].length, actual[i].length, "Column count mismatch at row " + i);
8876
for (int j = 0; j < expected[i].length; j++) {
89-
assertEquals(expected[i][j], actual[i][j], EPSILON,
90-
"Mismatch at (" + i + "," + j + ")");
77+
assertEquals(expected[i][j], actual[i][j], EPSILON, "Mismatch at (" + i + "," + j + ")");
9178
}
9279
}
9380
}
94-
9581
}

0 commit comments

Comments
 (0)