TheAlgorithms/Go · error
Matrix rows and columns must equal in order to find the dete
Error message
Matrix rows and columns must equal in order to find the determinant.
What it means
Returned by Matrix.Determinant when mat.rows != mat.columns; the determinant via cofactor expansion is defined only for square matrices, so non-square input is rejected before the base cases are evaluated.
Source
Thrown at math/matrix/determinant.go:24
// space complexity: O(n^2) where n is the number of rows and columns in the matrix.
// author [Carter907](https://github.com/Carter907)
// see determinant_test.go
package matrix
import (
"errors"
)
// Calculates the determinant of the matrix.
// This method only works for square matrices (e.i. matrices with equal rows and columns).
func (mat Matrix[T]) Determinant() (T, error) {
var determinant T = 0
var elements = mat.elements
if mat.rows != mat.columns {
return 0, errors.New("Matrix rows and columns must equal in order to find the determinant.")
}
// Specify base cases for different sized matrices.
switch mat.rows {
case 1:
return elements[0][0], nil
case 2:
return elements[0][0]*elements[1][1] - elements[1][0]*elements[0][1], nil
default:
for i := 0; i < mat.rows; i++ {
var initialValue T = 0
minor := New(mat.rows-1, mat.columns-1, initialValue)
// Fill the contents of minor excluding the 0th row and the ith column.
for j, minor_i := 1, 0; j < mat.rows && minor_i < minor.rows; j, minor_i = j+1, minor_i+1 {
for k, minor_j := 0, 0; k < mat.rows && minor_j < minor.rows; k, minor_j = k+1, minor_j+1 {
if k != i {
minor.elements[minor_i][minor_j] = elements[j][k]View on GitHub (pinned to 5ba447ec5f)
Solutions
- Verify the matrix is square before calling Determinant (assert mat.Rows() == mat.Columns()).
- Fix the construction site so rows == columns (correct dimensions passed to New or the element literal).
- If non-square input is possible, return a clear error to your caller instead of calling Determinant.
Example fix
// before m := math.New(2, 3, 0) det, err := m.Determinant() // error // after m := math.New(3, 3, 0) det, err := m.Determinant()
Defensive patterns
Strategy: validation
Validate before calling
func canDeterminant[T constraints.Integer](m math.Matrix[T]) bool {
return m.Rows() == m.Columns()
}
if !canDeterminant(m) {
return fmt.Errorf("determinant requires a square matrix, got %dx%d", m.Rows(), m.Columns())
}
det, err := m.Determinant() Type guard
func isSquare[T constraints.Integer](m math.Matrix[T]) bool {
return m.Rows() == m.Columns()
} Prevention
- Track row and column counts together when constructing matrices.
- Add a unit test asserting Determinant errors on a known non-square matrix.
- Prefer NewFromElements with a square literal so the shape is visible at the call site.
When it happens
Trigger: Calling Matrix.Determinant() on a matrix constructed with differing rows and columns counts, e.g. New(2,3,0) or NewFromElements with a non-square (but valid, rectangular) literal like [][]T{{1,2,3},{4,5,6}}.
Common situations: Building a matrix from parsed CSV/JSON rows that are rectangular but not square; hard-coding dimensions and forgetting to update one of them; dynamically grown matrices that end up m x n.
Related errors
- matrices cannot be multiplied: column count of the first mat
- matrices are not compatible for subtraction
- rows have different numbers of columns
- index out of range
- index out of bounds
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/34d873fbf47a9cd8.
Report an issue: GitHub.