Skip to content

Instantly share code, notes, and snippets.

@programaths
Created October 8, 2022 12:12
Show Gist options
  • Save programaths/58610f8bf8e2db404cdc3964d39244f7 to your computer and use it in GitHub Desktop.
Save programaths/58610f8bf8e2db404cdc3964d39244f7 to your computer and use it in GitHub Desktop.
Code pour générer une vidéo sur la multiplication de matrices
from manim import *
class CreateCircle(Scene):
def construct(self):
mat1_data = [[1, 3, 5], [7, 9, 11], [13, 15, 17]]
mat1 = Matrix(mat1_data, left_bracket="(",
right_bracket=")")
mat2_data = [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
mat2 = Matrix(mat2_data, left_bracket="(",
right_bracket=")")
times = MathTex(r"\times", font_size=36)
equal = MathTex(r"=", font_size=36)
mat3_data = [[MathTex(""), MathTex(""), MathTex("")], [MathTex(""), MathTex(""), MathTex("")],
[MathTex(""), MathTex(""), MathTex("")]]
mat3 = MobjectMatrix(mat3_data, left_bracket="(",
right_bracket=")", element_alignment_corner=DL)
times.next_to(mat1)
mat2.next_to(times)
equal.next_to(mat2)
mat3.next_to(equal)
g = VGroup(mat1, mat2, times, equal, mat3)
g.scale(0.5)
g.move_to(ORIGIN).shift(LEFT * 1.7)
self.play(Create(g))
# self.wait()
for r in range(3):
if r == 0:
sr1 = SurroundingRectangle(mat1.get_rows()[r])
self.play(Create(sr1))
if r > 0:
psr1 = sr1
sr1 = SurroundingRectangle(mat1.get_rows()[r])
self.play(ReplacementTransform(psr1, sr1))
for c in range(3):
if c == 0:
sr2 = SurroundingRectangle(mat2.get_columns()[c])
self.play(Create(sr2))
if c > 0:
psr2 = sr2
sr2 = SurroundingRectangle(mat2.get_columns()[c])
self.play(ReplacementTransform(psr2, sr2))
sm = 0
for k in range(3):
if k == 0:
kr1 = BackgroundRectangle(mat1.get_entries()[r * 3 + k], color=YELLOW, fill_opacity=0.5)
kr2 = BackgroundRectangle(mat2.get_entries()[k * 3 + c], color=YELLOW, fill_opacity=0.5)
self.play(Create(kr1), Create(kr2))
if k > 0:
pkr1 = kr1
kr1 = BackgroundRectangle(mat1.get_entries()[r * 3 + k], color=YELLOW, fill_opacity=0.5)
pkr2 = kr2
kr2 = BackgroundRectangle(mat2.get_entries()[k * 3 + c], color=YELLOW, fill_opacity=0.5)
self.play(ReplacementTransform(pkr1, kr1), ReplacementTransform(pkr2, kr2))
pmat3 = mat3
mat3_data[r][c] = MathTex(str(sm) + "+" + str(mat1_data[r][k]) + r"\times" + str(mat2_data[k][c]))
sm = sm + mat1_data[r][k] * mat2_data[k][c]
mat3 = MobjectMatrix(mat3_data, left_bracket="(",
right_bracket=")", element_alignment_corner=DL)
# mat3.scale(0.5)
mat3.next_to(equal)
self.play(ReplacementTransform(pmat3, mat3))
if k > 1:
self.play(FadeOut(kr1), FadeOut(kr2))
self.wait()
pmat3 = mat3
mat3_data[r][c] = Tex(str(sm))
mat3 = MobjectMatrix(mat3_data, left_bracket="(",
right_bracket=")", element_alignment_corner=DL)
# mat3.scale(0.5)
mat3.next_to(equal)
self.play(ReplacementTransform(pmat3, mat3))
self.wait()
if c > 1:
self.play(FadeOut(sr2))
pass
if r > 1:
self.play(FadeOut(sr1))
self.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment