{"record":{"id":"34d873fbf47a9cd8","repo":"TheAlgorithms/Go","slug":"matrix-rows-and-columns-must-equal-in-order-to-fin","errorCode":null,"errorMessage":"Matrix rows and columns must equal in order to find the determinant.","messagePattern":"Matrix rows and columns must equal in order to find the determinant\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/matrix/determinant.go","lineNumber":24,"sourceCode":"// space complexity: O(n^2) where n is the number of rows and columns in the matrix.\n// author [Carter907](https://github.com/Carter907)\n// see determinant_test.go\n\npackage matrix\n\nimport (\n\t\"errors\"\n)\n\n// Calculates the determinant of the matrix.\n// This method only works for square matrices (e.i. matrices with equal rows and columns).\nfunc (mat Matrix[T]) Determinant() (T, error) {\n\n\tvar determinant T = 0\n\tvar elements = mat.elements\n\tif mat.rows != mat.columns {\n\n\t\treturn 0, errors.New(\"Matrix rows and columns must equal in order to find the determinant.\")\n\t}\n\n\t// Specify base cases for different sized matrices.\n\tswitch mat.rows {\n\tcase 1:\n\t\treturn elements[0][0], nil\n\tcase 2:\n\t\treturn elements[0][0]*elements[1][1] - elements[1][0]*elements[0][1], nil\n\tdefault:\n\t\tfor i := 0; i < mat.rows; i++ {\n\n\t\t\tvar initialValue T = 0\n\t\t\tminor := New(mat.rows-1, mat.columns-1, initialValue)\n\t\t\t// Fill the contents of minor excluding the 0th row and the ith column.\n\t\t\tfor j, minor_i := 1, 0; j < mat.rows && minor_i < minor.rows; j, minor_i = j+1, minor_i+1 {\n\t\t\t\tfor k, minor_j := 0, 0; k < mat.rows && minor_j < minor.rows; k, minor_j = k+1, minor_j+1 {\n\t\t\t\t\tif k != i {\n\t\t\t\t\t\tminor.elements[minor_i][minor_j] = elements[j][k]","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/matrix/determinant.go#L6-L42","documentation":"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.","triggerScenarios":"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}}.","commonSituations":"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.","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."],"exampleFix":"// before\nm := math.New(2, 3, 0)\ndet, err := m.Determinant() // error\n\n// after\nm := math.New(3, 3, 0)\ndet, err := m.Determinant()","handlingStrategy":"validation","validationCode":"func canDeterminant[T constraints.Integer](m math.Matrix[T]) bool {\n    return m.Rows() == m.Columns()\n}\n\nif !canDeterminant(m) {\n    return fmt.Errorf(\"determinant requires a square matrix, got %dx%d\", m.Rows(), m.Columns())\n}\ndet, err := m.Determinant()","typeGuard":"func isSquare[T constraints.Integer](m math.Matrix[T]) bool {\n    return m.Rows() == m.Columns()\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["matrix","determinant","dimension-mismatch","go"],"backgroundTag":"matrix-dimension-mismatch","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}