TheAlgorithms/Python · error · Exception
change_component: indices out of bounds
Error message
change_component: indices out of bounds
What it means
Raised by Matrix.component(x, y) in linear_algebra/src/lib.py:372 when the requested cell lies outside 0 <= x < height, 0 <= y < width. The message text 'change_component: indices out of bounds' is misleading — this is the getter, and the message was copy-pasted from the setter — but the cause is simply an out-of-bounds (x, y) pair. Indices here are 0-based and negative indices are NOT supported (unlike Vector.component).
Source
Thrown at linear_algebra/src/lib.py:372
"""
getter for the height
"""
return self.__height
def width(self) -> int:
"""
getter for the width
"""
return self.__width
def component(self, x: int, y: int) -> float:
"""
returns the specified (x,y) component
"""
if 0 <= x < self.__height and 0 <= y < self.__width:
return self.__matrix[x][y]
else:
raise Exception("change_component: indices out of bounds")
def change_component(self, x: int, y: int, value: float) -> None:
"""
changes the x-y component of this matrix
"""
if 0 <= x < self.__height and 0 <= y < self.__width:
self.__matrix[x][y] = value
else:
raise Exception("change_component: indices out of bounds")
def minor(self, x: int, y: int) -> float:
"""
returns the minor along (x, y)
"""
if self.__height != self.__width:
raise Exception("Matrix is not square")
minor = self.__matrix[:x] + self.__matrix[x + 1 :]
for i in range(len(minor)):View on GitHub (pinned to f5988cc097)
Solutions
- Clamp the indices or bounds-check 0 <= x < m.height() and 0 <= y < m.width() before calling.
- Convert 1-based indices: x0, y0 = x - 1, y - 1.
- Fix loop ranges to range(m.height()) / range(m.width()).
- Do not use negative indices with this API — wrap them first: x % m.height().
Example fix
// before
val = m.component(2, 2) # on a 2x2 matrix: Exception
// after
if 0 <= x < m.height() and 0 <= y < m.width():
val = m.component(x, y)
else:
raise IndexError(f"({x}, {y}) outside {m.height()}x{m.width()}") Defensive patterns
Strategy: validation
Validate before calling
def in_bounds(m, x: int, y: int) -> bool:
return 0 <= x < m.height() and 0 <= y < m.width()
if not in_bounds(m, x, y):
raise IndexError(f"({x}, {y}) outside {m.height()}x{m.width()}")
value = m.component(x, y) Type guard
def is_valid_cell(m, x, y) -> bool:
return (
isinstance(x, int) and isinstance(y, int)
and 0 <= x < m.height() and 0 <= y < m.width()
) Try / catch
try:
value = m.component(x, y)
except Exception as e:
if "indices out of bounds" in str(e):
raise IndexError(f"cell ({x},{y}) outside {m.height()}x{m.width()}") from e
raise Prevention
- This API is 0-based and does NOT accept negative indices — convert any negative or 1-based coordinates first.
- Loop with range(m.height()) / range(m.width()); inclusive bounds are the usual trigger.
- The message says 'change_component' but this is the getter — do not be misled when debugging.
- Bounds-check indices from external sources (spreadsheets, specs, math notation) before use.
When it happens
Trigger: Calling m.component(2, 0) on a 2x2 matrix, or using 1-based indices from a spec/paper directly (m.component(1, 1) is the top-left cell, not the second row/col), or negative indices like m.component(-1, -1) which the 0 <= x check rejects.
Common situations: Translating 1-based mathematical notation into code, off-by-one loop bounds, or indices coming from another system that uses negative indexing.
Related errors
- index out of range
- Indices out of bounds
- determinant modular {req_l} of encryption key({det}) is not
- 'table' has to be of square shaped array but got a {rows}x{c
- matrix must have the same dimension!
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/0a13fe38c1d8123f.
Report an issue: GitHub.