Skip to content

Commit 7963ccf

Browse files
style: Apply clang-format fixes
1 parent d4af9fc commit 7963ccf

File tree

2 files changed

+18
-67
lines changed

2 files changed

+18
-67
lines changed

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private StrassenMatrixMultiplication() {
4747
* @param matrixB the second matrix (must be square, n x n)
4848
* @return the product of the two matrices
4949
* @throws IllegalArgumentException if matrices are not square, not the
50-
* same size, or cannot be multiplied.
50+
* same size, or cannot be multiplied.
5151
*/
5252
public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
5353
// --- 1. VALIDATION ---
@@ -60,9 +60,7 @@ public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
6060

6161
int n = matrixA.length;
6262
if (n != matrixA[0].length || n != matrixB.length || n != matrixB[0].length) {
63-
throw new IllegalArgumentException(
64-
"Strassen's algorithm requires square matrices of the same dimension (n x n)."
65-
);
63+
throw new IllegalArgumentException("Strassen's algorithm requires square matrices of the same dimension (n x n).");
6664
}
6765

6866
// --- 2. PADDING ---
@@ -172,34 +170,18 @@ private static double[][] subtract(double[][] matrixA, double[][] matrixB) {
172170
/**
173171
* Splits a parent matrix into a new sub-matrix.
174172
*/
175-
private static double[][] split(
176-
double[][] matrix,
177-
int rowStart,
178-
int colStart,
179-
int size
180-
) {
173+
private static double[][] split(double[][] matrix, int rowStart, int colStart, int size) {
181174
double[][] subMatrix = new double[size][size];
182175
for (int i = 0; i < size; i++) {
183-
System.arraycopy(
184-
matrix[i + rowStart],
185-
colStart,
186-
subMatrix[i],
187-
0,
188-
size
189-
);
176+
System.arraycopy(matrix[i + rowStart], colStart, subMatrix[i], 0, size);
190177
}
191178
return subMatrix;
192179
}
193180

194181
/**
195182
* Joins four sub-matrices into one larger matrix.
196183
*/
197-
private static double[][] join(
198-
double[][] c11,
199-
double[][] c12,
200-
double[][] c21,
201-
double[][] c22
202-
) {
184+
private static double[][] join(double[][] c11, double[][] c12, double[][] c21, double[][] c22) {
203185
int n = c11.length;
204186
int newSize = n * 2;
205187
double[][] result = new double[newSize][newSize];

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

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ class StrassenMatrixMultiplicationTest {
1414
private static final double[][] MATRIX_2X2_B = {{5, 6}, {7, 8}};
1515
private static final double[][] EXPECTED_2X2_PRODUCT = {{19, 22}, {43, 50}};
1616

17-
private static final double[][] MATRIX_4X4_A = {
18-
{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
19-
private static final double[][] MATRIX_4X4_B = {
20-
{5, 8, 1, 2}, {6, 7, 3, 0}, {4, 5, 9, 1}, {2, 6, 10, 14}};
21-
private static final double[][] EXPECTED_4X4_PRODUCT = {
22-
{37, 61, 74, 61}, {105, 165, 166, 129}, {173, 269, 258, 197}, {241, 373, 350, 265}};
17+
private static final double[][] MATRIX_4X4_A = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
18+
private static final double[][] MATRIX_4X4_B = {{5, 8, 1, 2}, {6, 7, 3, 0}, {4, 5, 9, 1}, {2, 6, 10, 14}};
19+
private static final double[][] EXPECTED_4X4_PRODUCT = {{37, 61, 74, 61}, {105, 165, 166, 129}, {173, 269, 258, 197}, {241, 373, 350, 265}};
2320

2421
private static final double[][] MATRIX_3X3_A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
2522
private static final double[][] MATRIX_3X3_B = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
@@ -39,12 +36,7 @@ class StrassenMatrixMultiplicationTest {
3936
private void assertMatrixEquals(double[][] expected, double[][] actual) {
4037
assertEquals(expected.length, actual.length, "Number of rows differ");
4138
for (int i = 0; i < expected.length; i++) {
42-
assertArrayEquals(
43-
expected[i],
44-
actual[i],
45-
DELTA,
46-
"Row " + i + " differs"
47-
);
39+
assertArrayEquals(expected[i], actual[i], DELTA, "Row " + i + " differs");
4840
}
4941
}
5042

@@ -84,7 +76,8 @@ void testMultiplyByZero() {
8476
double[][] result2 = StrassenMatrixMultiplication.multiply(MATRIX_ZERO_2X2, MATRIX_2X2_A);
8577
assertMatrixEquals(MATRIX_ZERO_2X2, result2);
8678
}
87-
@Test
79+
80+
@Test
8881
void testMultiply1x1() {
8982
double[][] a = {{5.0}};
9083
double[][] b = {{6.0}};
@@ -93,42 +86,21 @@ void testMultiply1x1() {
9386
assertMatrixEquals(expected, result);
9487
}
9588

96-
9789
@Test
9890
void testNullInput() {
99-
assertThrows(
100-
IllegalArgumentException.class,
101-
() -> StrassenMatrixMultiplication.multiply(null, MATRIX_2X2_B),
102-
"Multiplying with null matrix A should throw exception"
103-
);
104-
assertThrows(
105-
IllegalArgumentException.class,
106-
() -> StrassenMatrixMultiplication.multiply(MATRIX_2X2_A, null),
107-
"Multiplying with null matrix B should throw exception"
108-
);
91+
assertThrows(IllegalArgumentException.class, () -> StrassenMatrixMultiplication.multiply(null, MATRIX_2X2_B), "Multiplying with null matrix A should throw exception");
92+
assertThrows(IllegalArgumentException.class, () -> StrassenMatrixMultiplication.multiply(MATRIX_2X2_A, null), "Multiplying with null matrix B should throw exception");
10993
}
11094

11195
@Test
11296
void testNonSquareInput() {
113-
assertThrows(
114-
IllegalArgumentException.class,
115-
() -> StrassenMatrixMultiplication.multiply(MATRIX_NON_SQUARE, MATRIX_2X2_B),
116-
"Multiplying non-square matrix A should throw exception"
117-
);
118-
assertThrows(
119-
IllegalArgumentException.class,
120-
() -> StrassenMatrixMultiplication.multiply(MATRIX_2X2_A, MATRIX_NON_SQUARE),
121-
"Multiplying non-square matrix B should throw exception"
122-
);
97+
assertThrows(IllegalArgumentException.class, () -> StrassenMatrixMultiplication.multiply(MATRIX_NON_SQUARE, MATRIX_2X2_B), "Multiplying non-square matrix A should throw exception");
98+
assertThrows(IllegalArgumentException.class, () -> StrassenMatrixMultiplication.multiply(MATRIX_2X2_A, MATRIX_NON_SQUARE), "Multiplying non-square matrix B should throw exception");
12399
}
124100

125101
@Test
126102
void testDifferentSquareDimensions() {
127-
assertThrows(
128-
IllegalArgumentException.class,
129-
() -> StrassenMatrixMultiplication.multiply(MATRIX_2X2_A, MATRIX_3X3_A),
130-
"Multiplying matrices of different square dimensions should throw exception"
131-
);
103+
assertThrows(IllegalArgumentException.class, () -> StrassenMatrixMultiplication.multiply(MATRIX_2X2_A, MATRIX_3X3_A), "Multiplying matrices of different square dimensions should throw exception");
132104
}
133105

134106
@Test
@@ -138,10 +110,7 @@ void testEmptyMatrix() {
138110
assertEquals(0, result.length, "Multiplying empty matrices should result in an empty matrix");
139111

140112
double[][] emptyRows = {{}};
141-
assertThrows(
142-
IllegalArgumentException.class, // Or handle as empty depending on strictness
143-
() -> StrassenMatrixMultiplication.multiply(emptyRows, emptyRows),
144-
"Multiplying matrices with zero columns might throw or return empty"
145-
);
113+
assertThrows(IllegalArgumentException.class, // Or handle as empty depending on strictness
114+
() -> StrassenMatrixMultiplication.multiply(emptyRows, emptyRows), "Multiplying matrices with zero columns might throw or return empty");
146115
}
147116
}

0 commit comments

Comments
 (0)