TheAlgorithms/JavaScript · error · Error
Square matrix is required.
Error message
Square matrix is required.
What it means
Thrown by determinant(matrix) (Determinant.js:62) as a plain Error when the matrix passes the structural check but is not square. The helper isMatrixSquare (Determinant.js:43) verifies that every row has length equal to the number of rows. The Laplace expansion algorithm requires a square matrix, so a non-square input is rejected. A 1x1 matrix returns its single element without throwing.
Source
Thrown at Maths/Determinant.js:62
let numRows = matrix.length
for (let i = 0; i < numRows; i++) {
if (numRows !== matrix[i].length) {
return false
}
}
return true
}
const determinant = (matrix) => {
if (
!Array.isArray(matrix) ||
matrix.length === 0 ||
!Array.isArray(matrix[0])
) {
throw new Error('Input is not a valid 2D matrix.')
}
if (!isMatrixSquare(matrix)) {
throw new Error('Square matrix is required.')
}
let numCols = matrix[0].length
if (numCols === 1) {
return matrix[0][0]
}
let result = 0
let setIndex = 0
for (let i = 0; i < numCols; i++) {
result +=
Math.pow(-1, i) *
matrix[setIndex][i] *
determinant(subMatrix(matrix, setIndex, i))
}
return result
}
export { determinant }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Verify squareness before calling: m.every(row => row.length === m.length).
- If you need the determinant of a non-square matrix, reconsider — it is undefined; you may want SVD or a different decomposition.
- Validate row lengths during data loading and reject ragged input early.
- Log matrix dimensions at the call site to catch dimension mismatches during debugging.
Example fix
// before
const d = determinant(m) // throws if m is not square
// after
const isSquare = Array.isArray(m) && m.length > 0 &&
m.every(row => Array.isArray(row) && row.length === m.length)
if (!isSquare) throw new Error('expected a square matrix')
const d = determinant(m) Defensive patterns
Strategy: validation
Validate before calling
const isSquare = Array.isArray(matrix) && matrix.length > 0 &&
matrix.every(row => Array.isArray(row) && row.length === matrix.length)
if (!isSquare) throw new Error('expected a square matrix')
const d = determinant(matrix) Type guard
const isSquareMatrix = (v) => Array.isArray(v) && v.length > 0 && v.every(row => Array.isArray(row) && row.length === v.length)
Try / catch
try {
d = determinant(matrix)
} catch (e) {
if (e instanceof Error && /Square matrix/.test(e.message)) {
// non-square — reconsider whether determinant is the right operation
} else throw e
} Prevention
- Validate row.length === matrix.length for every row before calling.
- Reject ragged data during loading rather than at the math call site.
- Determinants are undefined for non-square matrices — do not try to force one.
When it happens
Trigger: Call determinant([[1,2,3],[4,5,6]]) — a 2x3 matrix; determinant([[1,2],[3,4],[5,6]]) — a 3x2 matrix; a matrix with a ragged row like [[1,2],[3]] returns false from isMatrixSquare and throws here.
Common situations: Loading data with inconsistent row lengths from CSV; transposing or slicing that changed dimensions; passing a data table with a header row of different length; construction loop with an off-by-one column count.
Related errors
- Input is not a valid 2D matrix.
- Input is not a valid 2D matrix.
- Duplicate characters in character set are not allowed
- Not a valid character: ${digit}
- Argument is not a valid HEX code!
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/ffd62672ae3228a9.
Report an issue: GitHub.