Why does the animation take the same values twice for matrix multiplication

52 Views Asked by At

So I got a assignment in school to do a visualisation about something regarding linjer algebra. One of the options to do this was Manim. I tried many ways myself but nothing seemed to work out. I found a video that made a animation of matris multiplication and tried to implement that. However the animation was not complete so i resolved to do the rest of the code myself.

In the original animation that the code formed was only the first half of the Matris Multiplication. I then tried to extend the animation but it only took the same first multiplication and transformed it into the second answer.

I am not familiar with how the Zip function is used so the "for" loop in the code is alien to me.

Original code referenced

Below is my version of the code

from manim import *
import numpy as np


class matrixTest(Scene):
    


    def construct(self):
        A = np.array([[-1, 5], [7, 11]])
        B = np.array([[2, 3], [-8, 0]])
        C = np.dot(A, B)
        


        stuff = VGroup(Matrix(A), Matrix(B), Matrix(C))
        matrixA = stuff[0]
        matrixB = stuff[1]
        matrixC = stuff[2]
        matrixA.height = 2.5
        matrixB.height = 2.5
        matrixA.color = PURPLE
        matrixB.color = RED
        Dot = Tex(".", color=WHITE, font_size = 200)
        Equals = Tex("=", color=WHITE, font_size = 100)
        bOpen = Tex("[", color=WHITE, font_size = 100)
        bClose = Tex("]", color=WHITE, font_size=100)
        bOpen1 = Tex("[", color=WHITE, font_size=200)
        bClose1 = Tex("]", color=WHITE, font_size=200)
        aOpen = Tex("[", color=WHITE, font_size = 100)
        aClose = Tex("]", color=WHITE, font_size=100)
        aOpen1 = Tex("[", color=WHITE, font_size=200)
        aClose1 = Tex("]", color=WHITE, font_size=200)
        


        self.play(Write(matrixA))
        self.play(matrixA.animate.scale(1).to_corner(UP+LEFT*2))
        
        Dot.next_to(matrixA, RIGHT)
        self.play(Write(Dot))
        
        self.play(Write(matrixB))
        self.play(matrixB.animate.scale(1).next_to(Dot, RIGHT))
        
        Equals.next_to(matrixB, RIGHT)
        self.play(Write(Equals))
        
        matrixC.next_to(Equals)
        C_elements = VGroup(*matrixC)
        for i in C_elements[1:]:
            i.height = 2.5
            self.play(Write(i))
        C_elements = VGroup(*C_elements[0])
        A_rows = matrixA.get_rows()
        A = VGroup(A_rows[0], A_rows[0], A_rows[1], A_rows[1])
        B_columns = matrixB.get_columns()
        B = VGroup(B_columns[0], B_columns[1], B_columns[0], B_columns[1])
        A2_rows = matrixA.get_rows()
        A2 = VGroup(A2_rows[1],A2_rows[1],A2_rows[0],A2_rows[0])
        B2_columns = matrixB.get_columns()
        B2 = VGroup(B2_columns[1], B2_columns[0], )
        
        for r, c, q, f, ans in zip(A.copy(), B.copy(), A2.copy(), B2.copy(), C_elements.copy()):
            _bOpen = bOpen.copy()
            _bClose = bClose.copy()
            _bOpen1 = bOpen1.copy()
            _bClose1 = bClose1.copy()
            _aOpen = aOpen.copy()
            _aClose = aClose.copy()
            _aOpen1 = aOpen1.copy()
            _aClose1 = aClose1.copy()
            _Dot = Dot.copy()
            Dot_ = Dot.copy()
            _r = r.copy()
            _c = c.copy()
            _q = q.copy()
            _f = f.copy()


            _bOpen.next_to(matrixA, DOWN*3)
            self.play(Write(_bOpen))
            self.play(_r.set_color(BLUE).animate.next_to(_bOpen))
            _bClose.next_to(_r, RIGHT)
            self.play(Write(_bClose))
            _Dot.next_to(_bClose, RIGHT)
            self.play(Write(_Dot))
            _bOpen1.next_to(_Dot, RIGHT)
            self.play(Write(_bOpen1))
            self.play(_c.set_color(YELLOW).animate.next_to(_bOpen1))
            _bClose1.next_to(_c, RIGHT)
            self.play(Write(_bClose1))
            g = VGroup(_bOpen, _r, _bClose, _Dot, _bOpen1, _c, _bClose1)
            ans.font_size = 60
            ans.set_color(PURE_GREEN)
            self.play(Transform(g, ans))


            _aOpen.next_to(matrixA, DOWN*3)
            self.play(Write(_aOpen))
            self.play(_q.set_color(BLUE).animate.next_to(_aOpen))
            _aClose.next_to(_q, RIGHT)
            self.play(Write(_aClose))
            Dot_.next_to(_aClose, RIGHT)
            self.play(Write(Dot_))
            _aOpen1.next_to(Dot_, RIGHT)
            self.play(Write(_aOpen1))
            self.play(_f.set_color(YELLOW).animate.next_to(_aOpen1))
            _aClose1.next_to(_f, RIGHT)
            self.play(Write(_aClose1))
            g2 = VGroup(_aOpen, _q, _aClose, Dot_, _aOpen1, _f, _aClose1)
            ans.font_size = 60
            ans.set_color(PURE_GREEN)
            self.play(Transform(g2, ans))

            
        self.wait()

So what I tried to do in the code above was to add the second rows and columns from the matrix and then later in the code use those to complete the animation. The code ran but the numbers taken was wrong and transformed into an already existing answer.

0

There are 0 best solutions below