gt22

Untitled

Jun 9th, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. from big_ol_pile_of_manim_imports import *
  2.  
  3.  
  4. def p(x=0, y=0):
  5.     if type(x) == list or type(x) == tuple or type(x) == np.ndarray:
  6.         return np.array(x)
  7.     return np.array((x, y, 0))
  8.  
  9.  
  10. class MMScene(Scene):
  11.  
  12.     m1 = np.array([[1, 2], [2, 9]])
  13.     m2 = np.array([[2, 4, 3], [6, 8, 3]])
  14.     res_data = None
  15.     x, y, res = None, None, None
  16.  
  17.     def construct(self):
  18.         if self.m1.shape[1] != self.m2.shape[0]:
  19.             raise ValueError("Invalid matrices")
  20.         self.x = x = IntegerMatrix(self.m1)
  21.         x.move_to(p(-0.1), RIGHT)
  22.         self.y = y = IntegerMatrix(self.m2)
  23.         y.move_to(p(0.1), LEFT)
  24.         self.play(FadeIn(x), FadeIn(y))
  25.         y = self.move_mobj(y, p(0.1, x.get_height() / 2 + 0.1), LEFT + DOWN)
  26.         self.res_data = np.zeros((self.m1.shape[0], self.m2.shape[1]), dtype=int)
  27.         self.res = res = IntegerMatrix(self.res_data)
  28.         res.move_to(p(y.get_center()[0], x.get_center()[1]))
  29.         self.play(FadeIn(res))
  30.         self.circle_matrix(res, 0, 'green')
  31.         self.wait(2)
  32.         for i in range(self.res_data.shape[0]):
  33.             for j in range(self.res_data.shape[1]):
  34.                 self.calc_res_elem(i, j)
  35.         res = self.res
  36.         self.wait(2)
  37.         rc = self.res.copy()
  38.         rc.move_to(ORIGIN)
  39.         self.play(FadeOut(x), FadeOut(y), Transform(res, rc))
  40.         self.remove(x, y, res)
  41.         self.add(rc)
  42.         self.res = res = rc
  43.         self.wait(3)
  44.  
  45.     def circle_matrix(self, m: Matrix, elem: int, color='red'):
  46.         e = m.get_entries()[elem]
  47.         c = Circle(radius=max(e.get_width(), e.get_height()) / 2 + 0.2, color=color)
  48.         c.move_to(e)
  49.         return c
  50.  
  51.     def calc_res_elem(self, x, y):
  52.         elem = self.res_data.shape[1] * x + y
  53.         res_tex = TexMobject(f"{self.res_data[x, y]}")
  54.         tex_pos = p(y=self.x.get_critical_point(DOWN)[1] - 0.1)
  55.         res_tex.set_color(GREEN)
  56.         res_tex.move_to(tex_pos, UP)
  57.         c = self.circle_matrix(self.res, elem, 'green')
  58.         self.play(ShowCreation(c))
  59.         self.play(Write(res_tex))
  60.         xcp, ycp = None, None
  61.         for step in range(self.m1.shape[1]):
  62.             x_elem = self.m1.shape[1] * x + step
  63.             y_elem = self.m2.shape[1] * step + y
  64.             xcp, ycp = self.calc_step(x_elem, y_elem, xcp, ycp)
  65.             self.wait(0.5)
  66.             a, b = self.m1[x, step], self.m2[step, y]
  67.             dr = self.m1[x, step] * self.m2[step, y]
  68.             self.res_data[x, y] += dr
  69.             cur_res = res_tex.get_tex_string()
  70.             rtm = TexMobject(f"_{{}}{cur_res}+{a}_{{}}\\times{b}^{{}}", tex_to_color_map={
  71.                 f'{a}_{{}}': RED,
  72.                 f'{b}^{{}}': YELLOW,
  73.                 f'_{{}}{cur_res}': GREEN
  74.             })
  75.             rtm.move_to(tex_pos, UP)
  76.             rta = TexMobject(f"{cur_res}^{{}}+{dr}_{{}}", tex_to_color_map={
  77.                 f"{cur_res}^{{}}": GREEN,
  78.                 f"{dr}_{{}}": ORANGE
  79.             })
  80.             rta.move_to(tex_pos, UP)
  81.             rt = TexMobject(f'{self.res_data[x, y]}')
  82.             rt.set_color(GREEN)
  83.             rt.move_to(tex_pos, UP)
  84.             self.chain_transorm([res_tex, rtm, rta, rt], 0.5)
  85.             res_tex = rt
  86.  
  87.             rm = IntegerMatrix(self.res_data)
  88.             rm.move_to(self.res)
  89.             cc = self.circle_matrix(rm, elem, 'green')
  90.             self.play(Transform(self.res, rm), Transform(c, cc))
  91.             self.remove(self.res, c)
  92.             self.add(rm, cc)
  93.             self.res = rm
  94.             c = cc
  95.  
  96.         self.play(*[FadeOut(g) for g in [c, xcp, ycp, res_tex]])
  97.         self.remove(c, xcp, ycp, res_tex)
  98.  
  99.     def chain_transorm(self, mobjs, wait_time, **kwargs):
  100.         for a, b in zip(mobjs, mobjs[1:]):
  101.             self.play(Transform(a, b, **kwargs))
  102.             self.remove(a)
  103.             self.add(b)
  104.             self.wait(wait_time)
  105.  
  106.     def calc_step(self, x_elem, y_elem, xcp, ycp):
  107.  
  108.         if xcp is None or ycp is None:
  109.             xc = self.circle_matrix(self.x, x_elem)
  110.             yc = self.circle_matrix(self.y, y_elem, 'yellow')
  111.             self.play(ShowCreation(xc), ShowCreation(yc))
  112.         else:
  113.             xc = xcp.copy()
  114.             yc = ycp.copy()
  115.             xc.move_to(self.x.get_entries()[x_elem])
  116.             yc.move_to(self.y.get_entries()[y_elem])
  117.             self.play(Transform(xcp, xc), Transform(ycp, yc))
  118.             self.remove(xcp, ycp)
  119.             self.add(xc, yc)
  120.         return xc, yc
  121.  
  122.     def move_mobj(self, m, point, edge=ORIGIN, **kwargs):
  123.         mc = m.copy()
  124.         mc.move_to(point, edge)
  125.         self.play(Transform(m, mc, lag_ratio=0, **kwargs))
  126.         self.remove(m)
  127.         self.add(mc)
  128.         return mc
Advertisement
Add Comment
Please, Sign In to add comment