TheAlgorithms/Go · error

submatrix dimensions exceed matrix bounds

Error message

submatrix dimensions exceed matrix bounds

What it means

SubMatrix validates that rowStart+numRows and colStart+numCols fit within the matrix; the requested window extends past the matrix's rows or columns, so extraction is refused rather than returning a clipped matrix.

Source

Thrown at math/matrix/submatrix.go:16

package matrix

import (
	"context"
	"errors"
	"sync"
)

// SubMatrix extracts a submatrix from the current matrix.
func (m Matrix[T]) SubMatrix(rowStart, colStart, numRows, numCols int) (Matrix[T], error) {
	if rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 {
		return Matrix[T]{}, errors.New("negative dimensions are not allowed")
	}

	if rowStart+numRows > m.rows || colStart+numCols > m.columns {
		return Matrix[T]{}, errors.New("submatrix dimensions exceed matrix bounds")
	}

	var zeroVal T
	if numRows == 0 || numCols == 0 {
		return New(numRows, numCols, zeroVal), nil // Return an empty matrix
	}

	subMatrix := New(numRows, numCols, 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 < numRows; i++ {
		i := i // Capture the loop variable for the goroutine
		wg.Add(1)

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Clamp: numRows = min(numRows, m.Rows()-rowStart), numCols likewise, before calling.
  2. Validate rowStart+numRows <= m.Rows() and colStart+numCols <= m.Columns() first.
  3. Fix off-by-one where an end index was passed instead of a length (use end-start).

Example fix

// before
sub, err := m.SubMatrix(1, 1, 3, 3) // 3x3 matrix: out of bounds

// after
sub, err := m.SubMatrix(1, 1, m.Rows()-1, m.Columns()-1) // fits
Defensive patterns

Strategy: validation

Validate before calling

if rowStart+numRows > m.Rows() {
    numRows = m.Rows() - rowStart
}
if colStart+numCols > m.Columns() {
    numCols = m.Columns() - colStart
}
if numRows > 0 && numCols > 0 {
    sub, err := m.SubMatrix(rowStart, colStart, numRows, numCols)
}

Type guard

func fitsIn[T constraints.Integer](m math.Matrix[T], rowStart, colStart, numRows, numCols int) bool {
    return rowStart+numRows <= m.Rows() && colStart+numCols <= m.Columns()
}

Try / catch

sub, err := m.SubMatrix(rs, cs, nr, nc)
if err != nil {
    return Matrix{}, fmt.Errorf("submatrix exceeds %dx%d bounds: %w", m.Rows(), m.Columns(), err)
}

Prevention

When it happens

Trigger: Calling m.SubMatrix(1, 1, 3, 3) on a 3x3 matrix (needs indices up to 3, valid max 2); window size larger than the matrix; start offset plus size overflowing the edge.

Common situations: Tiling/chunking loops where the last tile is clipped but the code requests a full tile; confusing submatrix size with end coordinate (passing an exclusive end as a count); Strassen-style partitioning on non-power-of-two sizes.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/c4283d83bc39091d. Report an issue: GitHub.