Skip to content

Commit efecad4

Browse files
fix(matrix): inline matrix validation to avoid cross-package dependency
MatrixUtil is in com.thealgorithms.matrix.utils package which causes compilation errors when referenced from com.thealgorithms.matrix. Inlined the validation logic (validateInputMatrix, hasValidRows, isJaggedMatrix) directly into QRDecomposition to resolve the issue.
1 parent 4ba2b77 commit efecad4

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public double[][] getR() {
4242
* @throws IllegalArgumentException if the matrix is null, empty, or has invalid rows
4343
*/
4444
public static QR decompose(double[][] matrix) {
45-
MatrixUtil.validateInputMatrix(matrix);
45+
validateInputMatrix(matrix);
4646

4747
int m = matrix.length;
4848
int n = matrix[0].length;
@@ -112,4 +112,38 @@ private static double[] scalarMultiply(double[] v, double scalar) {
112112
private static double norm(double[] v) {
113113
return Math.sqrt(dotProduct(v, v));
114114
}
115+
116+
private static void validateInputMatrix(double[][] matrix) {
117+
if (matrix == null) {
118+
throw new IllegalArgumentException("The input matrix cannot be null");
119+
}
120+
if (matrix.length == 0) {
121+
throw new IllegalArgumentException("The input matrix cannot be empty");
122+
}
123+
if (!hasValidRows(matrix)) {
124+
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
125+
}
126+
if (isJaggedMatrix(matrix)) {
127+
throw new IllegalArgumentException("The input matrix cannot be jagged");
128+
}
129+
}
130+
131+
private static boolean hasValidRows(double[][] matrix) {
132+
for (double[] row : matrix) {
133+
if (row == null || row.length == 0) {
134+
return false;
135+
}
136+
}
137+
return true;
138+
}
139+
140+
private static boolean isJaggedMatrix(double[][] matrix) {
141+
int numColumns = matrix[0].length;
142+
for (double[] row : matrix) {
143+
if (row.length != numColumns) {
144+
return true;
145+
}
146+
}
147+
return false;
148+
}
115149
}

0 commit comments

Comments
 (0)