ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
19.97k stars 1.48k forks source link

IndexError: list index out of range #3605

Closed rameswari77 closed 5 months ago

rameswari77 commented 5 months ago

from manim import *

class GaussianElimination(Scene): def construct(self):

Define the augmented matrix

    matrix = [[1, 3, -2, 0, 2, 0],
              [2, 6, -5, -2, 4, -3],
              [0, 0, 5, 10, 0, 15],
              [2, 6, 0, 8, 4, 18]]

    # Create a VGroup to hold the matrix elements
    matrix_group = self.get_matrix_group(matrix)

    # Display the original matrix
    self.play(Write(matrix_group))

    # Apply Gaussian elimination to find RREF
    self.gaussian_elimination(matrix, matrix_group)

    # Highlight the pivot columns
    self.highlight_pivots(matrix_group)

def gaussian_elimination(self, matrix, matrix_group):
    for col in range(len(matrix[0]) - 1):
        if matrix[col][col] == 0:
            continue  # Skip if pivot element is already zero

        for row in range(col + 1, len(matrix)):
            factor = matrix[row][col] / matrix[col][col]
            for i in range(len(matrix[0])):
                matrix[row][i] -= factor * matrix[col][i]

            # Display the updated matrix after each step
            self.play(Transform(matrix_group, self.get_matrix_group(matrix)))

def get_matrix_group(self, matrix):
    matrix_group = VGroup()

    for i, row in enumerate(matrix):
        row_elements = [Tex(f"{elem}", color=WHITE) for elem in row]
        matrix_group.add(VGroup(*row_elements).arrange(RIGHT))

    return matrix_group

def highlight_pivots(self, matrix_group):
    num_rows = len(matrix_group)
    num_cols = len(matrix_group[0]) - 1  # Exclude the augmented column

    for col_num in range(min(num_cols, num_rows)):
        pivot_row = None
        for row_num in range(col_num, num_rows):  # Start from the current column
            if matrix_group[row_num][col_num].get_color() == WHITE and matrix_group[row_num][col_num].get_value() != "0":
                pivot_row = row_num
                break

        if pivot_row is not None:
            pivot_element = matrix_group[pivot_row][col_num]
            self.play(
                matrix_group[pivot_row].animate.set_color_by_tex_to_color(
                    str(pivot_element), YELLOW
                )
            )
uwezi commented 5 months ago

without analyzing your weirdly formatted code deeper (after putting it into executable form) I would say that something must be wrong in your Gauss elimination function:

image

behackl commented 5 months ago

Indeed, this tries to access matrix[4][4] (the element in the fifth row&column) even though the passed matrix only has four rows. This has nothing to do with Manim, closing.

uwezi commented 5 months ago

OK, I suggest you look at your augmented matrix - it is a 6 column 4 row matrix... you need at least 5 rows for an equation system like this, or 4 rows and 5 columns