TheAlgorithms/Go · error
matrices cannot be multiplied: column count of the first mat
Error message
matrices cannot be multiplied: column count of the first matrix must match row count of the second matrix
What it means
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.
Source
Thrown at math/matrix/multiply.go:18
// multiply.go
// description: Implementation of matrix multiplication
// time complexity: O(n^3) where n is the number of rows in the first matrix
// space complexity: O(n^2) where n is the number of rows in the first matrix
package matrix
import (
"context"
"errors"
"sync"
)
// Multiply multiplies the current matrix (m1) with another matrix (m2) and returns the result as a new matrix.
func (m1 Matrix[T]) Multiply(m2 Matrix[T]) (Matrix[T], error) {
// Check if the matrices can be multiplied.
if m1.Columns() != m2.Rows() {
return Matrix[T]{}, errors.New("matrices cannot be multiplied: column count of the first matrix must match row count of the second matrix")
}
// Create a new matrix to store the result.
var zeroVal T
result := New(m1.Rows(), m2.Columns(), zeroVal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Make sure it's called to release resources even if no errors
var wg sync.WaitGroup
errCh := make(chan error, 1)
for i := 0; i < m1.Rows(); i++ {
for j := 0; j < m2.Columns(); j++ {
i, j := i, j // Capture the loop variable for the goroutine
wg.Add(1)
go func() {
defer wg.Done()View on GitHub (pinned to 5ba447ec5f)
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.
Example fix
// before // m1: 2x3, m2: 2x3 r, err := m1.Multiply(m2) // error // after t, _ := m2.Transpose() // 3x2 r, err := m1.Multiply(t) // 2x2
Defensive patterns
Strategy: validation
Validate before calling
if m1.Columns() != m2.Rows() {
return fmt.Errorf("cannot multiply %dx%d by %dx%d",
m1.Rows(), m1.Columns(), m2.Rows(), m2.Columns())
}
res, err := m1.Multiply(m2) Type guard
func multipliable[T constraints.Integer](m1, m2 math.Matrix[T]) bool {
return m1.Columns() == m2.Rows()
} Try / catch
res, err := m1.Multiply(m2)
if err != nil {
return Matrix{}, fmt.Errorf("multiply shapes %dx%d * %dx%d: %w",
m1.Rows(), m1.Columns(), m2.Rows(), m2.Columns(), err)
} Prevention
- 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.
When it happens
Trigger: Calling m1.Multiply(m2) with shapes like (2x3)·(2x3) or (3x2)·(3x4); multiplying a matrix by its own transpose without transposing first.
Common situations: 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.
Related errors
- Matrix rows and columns must equal in order to find the dete
- 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/9ad08fce916d66fa.
Report an issue: GitHub.