TheAlgorithms/Go · error
negative dimensions are not allowed
Error message
negative dimensions are not allowed
What it means
SubMatrix returns this error when any of rowStart, colStart, numRows, numCols is negative. Negative coordinates or sizes are meaningless for an extraction window, so the call is rejected before any bounds math runs.
Source
Thrown at math/matrix/submatrix.go:12
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)View on GitHub (pinned to 5ba447ec5f)
Solutions
- Clamp offsets: if rowStart < 0 { rowStart = 0 } (same for colStart) and ensure numRows/numCols >= 0.
- Validate all four arguments are >= 0 before calling SubMatrix.
- Fix the computation that produced the negative value (order of operands in subtraction).
Example fix
// before sub, err := m.SubMatrix(i-pad, j-pad, h, w) // error when i < pad // after rs, cs := max(0, i-pad), max(0, j-pad) sub, err := m.SubMatrix(rs, cs, h, w)
Defensive patterns
Strategy: validation
Validate before calling
if rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 {
rowStart, colStart = max(rowStart, 0), max(colStart, 0)
numRows, numCols = max(numRows, 0), max(numCols, 0)
}
sub, err := m.SubMatrix(rowStart, colStart, numRows, numCols) Try / catch
sub, err := m.SubMatrix(rs, cs, nr, nc)
if err != nil {
return Matrix{}, fmt.Errorf("submatrix(%d,%d,%d,%d): %w", rs, cs, nr, nc, err)
} Prevention
- Clamp computed offsets with max(0, x) before slicing.
- Keep padding arithmetic explicit and tested near boundaries.
- Distinguish start offsets from sizes in variable names.
When it happens
Trigger: Calling m.SubMatrix with a negative argument, e.g. SubMatrix(-1, 0, 2, 2) or SubMatrix(0, 0, -3, 3); computed offsets that underflow (start - padding < 0).
Common situations: Subtracting a padding/margin from an index without clamping to zero; loop variables that go negative near boundaries; swapped sign in a computed offset.
Related errors
- submatrix dimensions exceed matrix bounds
- Matrix rows and columns must equal in order to find the dete
- 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/9c2c4328f4d4c44d.
Report an issue: GitHub.