{"record":{"id":"0a13fe38c1d8123f","repo":"TheAlgorithms/Python","slug":"change-component-indices-out-of-bounds","errorCode":null,"errorMessage":"change_component: indices out of bounds","messagePattern":"change_component: indices out of bounds","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/lib.py","lineNumber":372,"sourceCode":"        \"\"\"\n        getter for the height\n        \"\"\"\n        return self.__height\n\n    def width(self) -> int:\n        \"\"\"\n        getter for the width\n        \"\"\"\n        return self.__width\n\n    def component(self, x: int, y: int) -> float:\n        \"\"\"\n        returns the specified (x,y) component\n        \"\"\"\n        if 0 <= x < self.__height and 0 <= y < self.__width:\n            return self.__matrix[x][y]\n        else:\n            raise Exception(\"change_component: indices out of bounds\")\n\n    def change_component(self, x: int, y: int, value: float) -> None:\n        \"\"\"\n        changes the x-y component of this matrix\n        \"\"\"\n        if 0 <= x < self.__height and 0 <= y < self.__width:\n            self.__matrix[x][y] = value\n        else:\n            raise Exception(\"change_component: indices out of bounds\")\n\n    def minor(self, x: int, y: int) -> float:\n        \"\"\"\n        returns the minor along (x, y)\n        \"\"\"\n        if self.__height != self.__width:\n            raise Exception(\"Matrix is not square\")\n        minor = self.__matrix[:x] + self.__matrix[x + 1 :]\n        for i in range(len(minor)):","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/lib.py#L354-L390","documentation":"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).","triggerScenarios":"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.","commonSituations":"Translating 1-based mathematical notation into code, off-by-one loop bounds, or indices coming from another system that uses negative indexing.","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()."],"exampleFix":"// before\nval = m.component(2, 2)  # on a 2x2 matrix: Exception\n\n// after\nif 0 <= x < m.height() and 0 <= y < m.width():\n    val = m.component(x, y)\nelse:\n    raise IndexError(f\"({x}, {y}) outside {m.height()}x{m.width()}\")","handlingStrategy":"validation","validationCode":"def in_bounds(m, x: int, y: int) -> bool:\n    return 0 <= x < m.height() and 0 <= y < m.width()\n\nif not in_bounds(m, x, y):\n    raise IndexError(f\"({x}, {y}) outside {m.height()}x{m.width()}\")\nvalue = m.component(x, y)","typeGuard":"def is_valid_cell(m, x, y) -> bool:\n    return (\n        isinstance(x, int) and isinstance(y, int)\n        and 0 <= x < m.height() and 0 <= y < m.width()\n    )","tryCatchPattern":"try:\n    value = m.component(x, y)\nexcept Exception as e:\n    if \"indices out of bounds\" in str(e):\n        raise IndexError(f\"cell ({x},{y}) outside {m.height()}x{m.width()}\") from e\n    raise","preventionTips":["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."],"tags":["linear-algebra","matrix","index-out-of-range","off-by-one","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}