{"record":{"id":"9ad08fce916d66fa","repo":"TheAlgorithms/Go","slug":"matrices-cannot-be-multiplied-column-count-of-the","errorCode":null,"errorMessage":"matrices cannot be multiplied: column count of the first matrix must match row count of the second matrix","messagePattern":"matrices cannot be multiplied: column count of the first matrix must match row count of the second matrix","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/matrix/multiply.go","lineNumber":18,"sourceCode":"// multiply.go\n// description: Implementation of matrix multiplication\n// time complexity: O(n^3) where n is the number of rows in the first matrix\n// space complexity: O(n^2) where n is the number of rows in the first matrix\n\npackage matrix\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"sync\"\n)\n\n// Multiply multiplies the current matrix (m1) with another matrix (m2) and returns the result as a new matrix.\nfunc (m1 Matrix[T]) Multiply(m2 Matrix[T]) (Matrix[T], error) {\n\t// Check if the matrices can be multiplied.\n\tif m1.Columns() != m2.Rows() {\n\t\treturn Matrix[T]{}, errors.New(\"matrices cannot be multiplied: column count of the first matrix must match row count of the second matrix\")\n\t}\n\n\t// Create a new matrix to store the result.\n\tvar zeroVal T\n\tresult := New(m1.Rows(), m2.Columns(), zeroVal)\n\n\tctx, cancel := context.WithCancel(context.Background())\n\tdefer cancel() // Make sure it's called to release resources even if no errors\n\n\tvar wg sync.WaitGroup\n\terrCh := make(chan error, 1)\n\n\tfor i := 0; i < m1.Rows(); i++ {\n\t\tfor j := 0; j < m2.Columns(); j++ {\n\t\t\ti, j := i, j // Capture the loop variable for the goroutine\n\t\t\twg.Add(1)\n\t\t\tgo func() {\n\t\t\t\tdefer wg.Done()","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/matrix/multiply.go#L1-L36","documentation":"Multiply returns this error when the inner dimensions disagree: m1.Columns() != m2.Rows(). Matrix multiplication is only defined when the first matrix's column count equals the second matrix's row count.","triggerScenarios":"Calling m1.Multiply(m2) with shapes like (2x3)·(2x3) or (3x2)·(3x4); multiplying a matrix by its own transpose without transposing first.","commonSituations":"Stacking operations where an intermediate result changed shape; multiplying A·Aᵀ instead of A·Aᵀ with an explicit transpose; dimension mismatches from differently sized datasets.","solutions":["Ensure m2.Rows() equals m1.Columns(); transpose or reshape one operand if needed.","Verify dimensions with m1.Columns() == m2.Rows() before multiplying.","Check that prior pipeline steps (add/subtract/submatrix) did not change the expected shape."],"exampleFix":"// before\n// m1: 2x3, m2: 2x3\nr, err := m1.Multiply(m2) // error\n\n// after\nt, _ := m2.Transpose() // 3x2\nr, err := m1.Multiply(t) // 2x2","handlingStrategy":"validation","validationCode":"if m1.Columns() != m2.Rows() {\n    return fmt.Errorf(\"cannot multiply %dx%d by %dx%d\",\n        m1.Rows(), m1.Columns(), m2.Rows(), m2.Columns())\n}\nres, err := m1.Multiply(m2)","typeGuard":"func multipliable[T constraints.Integer](m1, m2 math.Matrix[T]) bool {\n    return m1.Columns() == m2.Rows()\n}","tryCatchPattern":"res, err := m1.Multiply(m2)\nif err != nil {\n    return Matrix{}, fmt.Errorf(\"multiply shapes %dx%d * %dx%d: %w\",\n        m1.Rows(), m1.Columns(), m2.Rows(), m2.Columns(), err)\n}","preventionTips":["Record expected shapes in comments/types through a computation pipeline.","Transpose explicitly when multiplying by Aᵀ.","Add a table test with mismatched shapes asserting the error."],"tags":["matrix","multiplication","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"}