{"record":{"id":"10c4af724ce55f81","repo":"3b1b/manim","slug":"index-index-out-of-bound-for-matrix-with-len-se","errorCode":null,"errorMessage":"Index {index} out of bound for matrix with {len(self.columns)} columns","messagePattern":"Index (.+?) out of bound for matrix with (.+?) columns","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"manimlib/mobject/matrix.py","lineNumber":137,"sourceCode":"        else:\n            return Tex(str(element), **config)\n\n    def create_brackets(self, rows, v_buff: float, h_buff: float) -> VGroup:\n        brackets = Tex(\"\".join((\n            R\"\\left[\\begin{array}{c}\",\n            *len(rows) * [R\"\\quad \\\\\"],\n            R\"\\end{array}\\right]\",\n        )))\n        brackets.set_height(rows.get_height() + v_buff)\n        l_bracket = brackets[:len(brackets) // 2]\n        r_bracket = brackets[len(brackets) // 2:]\n        l_bracket.next_to(rows, LEFT, h_buff)\n        r_bracket.next_to(rows, RIGHT, h_buff)\n        return VGroup(l_bracket, r_bracket)\n\n    def get_column(self, index: int):\n        if not 0 <= index < len(self.columns):\n            raise IndexError(f\"Index {index} out of bound for matrix with {len(self.columns)} columns\")\n        return self.columns[index]\n\n    def get_row(self, index: int):\n        if not 0 <= index < len(self.rows):\n            raise IndexError(f\"Index {index} out of bound for matrix with {len(self.rows)} rows\")\n        return self.rows[index]\n\n    def get_columns(self) -> VGroup:\n        return self.columns\n\n    def get_rows(self) -> VGroup:\n        return self.rows\n\n    def set_column_colors(self, *colors: ManimColor) -> Self:\n        columns = self.get_columns()\n        for color, column in zip(colors, columns):\n            column.set_color(color)\n        return self","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/3b1b/manim/blob/dee01804d47b9f94402d71472674710dcac125b8/manimlib/mobject/matrix.py#L119-L155","documentation":"IndexError from Matrix.get_column (manimlib/mobject/matrix.py:137) with message 'Index {index} out of bound for matrix with {n} columns'. self.columns is the VGroup of column elements built at construction time from the matrix data; get_column validates 0 <= index < len(self.columns) and raises with the offending index and actual column count so you can see how far off you were.","triggerScenarios":"Matrix([[1,2],[3,4]]).get_column(5); negative indices (get_column(-1) fails this check even though Python slicing would accept it); index computed from user data or loop bounds that exceed the number of columns; matrices constructed via DecimalMatrix/Matrix of a shorter dataset than the index assumes.","commonSituations":"Coloring specific columns (set_column_colors is safe, but manual get_column(i) loops with wrong bounds); code written against a larger matrix later fed a smaller one; off-by-one loop bounds; assuming 1-based indexing (get_column(1) for the first column is fine, but get_column(n) for the last is not).","solutions":["Use 0-based indices strictly less than len(matrix.get_columns())","Derive bounds from the object: for i in range(len(m.get_columns())): m.get_column(i)","For the last column use index len(m.get_columns()) - 1, not -1"],"exampleFix":"# before\nm = Matrix([[1, 2], [3, 4]])\nm.get_column(2)   # raises: Index 2 out of bound ... 2 columns\nm.get_column(-1)  # also raises (no negative indexing)\n\n# after\nm = Matrix([[1, 2], [3, 4]])\nlast = m.get_column(len(m.get_columns()) - 1)","handlingStrategy":"validation","validationCode":"n_cols = len(matrix.get_columns())\nassert 0 <= col_index < n_cols, f\"col_index must be in [0, {n_cols})\"\ncolumn = matrix.get_column(col_index)","typeGuard":"def is_valid_column_index(matrix, i: int) -> bool:\n    return 0 <= i < len(matrix.get_columns())","tryCatchPattern":null,"preventionTips":["Use 0-based indices derived from len(matrix.get_columns())","Remember negative indices are rejected by this API despite being valid elsewhere in Python","After changing matrix data, re-derive hardcoded column indices"],"tags":["manim","matrix","index-error","validation"],"backgroundTag":null,"analyzedSha":"dee01804d47b9f94402d71472674710dcac125b8","analyzedAt":"2026-08-14T19:53:44.241Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}