mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Upated Matrix mobject
This commit is contained in:
parent
c69a21af32
commit
06535df542
2 changed files with 63 additions and 40 deletions
|
@ -2,7 +2,8 @@ from __future__ import absolute_import
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from mobject.mobject import Mobject
|
from mobject.numbers import DecimalNumber
|
||||||
|
from mobject.numbers import Integer
|
||||||
from mobject.svg.tex_mobject import TexMobject
|
from mobject.svg.tex_mobject import TexMobject
|
||||||
from mobject.types.vectorized_mobject import VGroup
|
from mobject.types.vectorized_mobject import VGroup
|
||||||
from mobject.types.vectorized_mobject import VMobject
|
from mobject.types.vectorized_mobject import VMobject
|
||||||
|
@ -55,9 +56,12 @@ def vector_coordinate_label(vector_mob, integer_labels=True,
|
||||||
|
|
||||||
class Matrix(VMobject):
|
class Matrix(VMobject):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"v_buff": 0.5,
|
"v_buff": 1,
|
||||||
"h_buff": 1,
|
"h_buff": 1.5,
|
||||||
"add_background_rectangles": False
|
"add_background_rectangles": False,
|
||||||
|
"element_to_mobject": TexMobject,
|
||||||
|
"element_to_mobject_config": {},
|
||||||
|
"element_alignment_corner": DR,
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, matrix, **kwargs):
|
def __init__(self, matrix, **kwargs):
|
||||||
|
@ -69,34 +73,32 @@ class Matrix(VMobject):
|
||||||
matrix = np.array(matrix)
|
matrix = np.array(matrix)
|
||||||
if matrix.ndim == 1:
|
if matrix.ndim == 1:
|
||||||
matrix = matrix.reshape((matrix.size, 1))
|
matrix = matrix.reshape((matrix.size, 1))
|
||||||
if not isinstance(matrix[0][0], Mobject):
|
mob_matrix = self.matrix_to_mob_matrix(matrix)
|
||||||
matrix = matrix.astype("string")
|
self.organize_mob_matrix(mob_matrix)
|
||||||
matrix = self.string_matrix_to_mob_matrix(matrix)
|
self.elements = VGroup(*mob_matrix.flatten())
|
||||||
self.organize_mob_matrix(matrix)
|
self.add(self.elements)
|
||||||
self.add(*matrix.flatten())
|
|
||||||
self.add_brackets()
|
self.add_brackets()
|
||||||
self.center()
|
self.center()
|
||||||
self.mob_matrix = matrix
|
|
||||||
|
self.mob_matrix = mob_matrix
|
||||||
if self.add_background_rectangles:
|
if self.add_background_rectangles:
|
||||||
for mob in matrix.flatten():
|
for mob in self.elements:
|
||||||
mob.add_background_rectangle()
|
mob.add_background_rectangle()
|
||||||
|
|
||||||
def string_matrix_to_mob_matrix(self, matrix):
|
def matrix_to_mob_matrix(self, matrix):
|
||||||
return np.array([
|
return np.vectorize(
|
||||||
map(TexMobject, row)
|
lambda e: self.element_to_mobject(
|
||||||
for row in matrix
|
e, **self.element_to_mobject_config)
|
||||||
]).reshape(matrix.shape)
|
)(matrix)
|
||||||
|
|
||||||
def organize_mob_matrix(self, matrix):
|
def organize_mob_matrix(self, matrix):
|
||||||
for i, row in enumerate(matrix):
|
for i, row in enumerate(matrix):
|
||||||
for j, elem in enumerate(row):
|
for j, elem in enumerate(row):
|
||||||
mob = matrix[i][j]
|
mob = matrix[i][j]
|
||||||
if i == 0 and j == 0:
|
mob.move_to(
|
||||||
continue
|
i * self.v_buff * DOWN + j * self.h_buff * RIGHT,
|
||||||
elif i == 0:
|
self.element_alignment_corner
|
||||||
mob.next_to(matrix[i][j - 1], RIGHT, self.h_buff)
|
)
|
||||||
else:
|
|
||||||
mob.next_to(matrix[i - 1][j], DOWN, self.v_buff)
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def add_brackets(self):
|
def add_brackets(self):
|
||||||
|
@ -128,3 +130,42 @@ class Matrix(VMobject):
|
||||||
|
|
||||||
def get_brackets(self):
|
def get_brackets(self):
|
||||||
return self.brackets
|
return self.brackets
|
||||||
|
|
||||||
|
|
||||||
|
class DecimalMatrix(Matrix):
|
||||||
|
CONFIG = {
|
||||||
|
"element_to_mobject": DecimalNumber,
|
||||||
|
"element_to_mobject_config": {"num_decimal_points": 1}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class IntegerMatrix(Matrix):
|
||||||
|
CONFIG = {
|
||||||
|
"element_to_mobject": Integer,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class MobjectMatrix(Matrix):
|
||||||
|
CONFIG = {
|
||||||
|
"element_to_mobject": lambda m: m,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_det_text(matrix, determinant=None, background_rect=True):
|
||||||
|
parens = TexMobject(["(", ")"])
|
||||||
|
parens.scale(2)
|
||||||
|
parens.stretch_to_fit_height(matrix.get_height())
|
||||||
|
l_paren, r_paren = parens.split()
|
||||||
|
l_paren.next_to(matrix, LEFT, buff=0.1)
|
||||||
|
r_paren.next_to(matrix, RIGHT, buff=0.1)
|
||||||
|
det = TextMobject("det").next_to(l_paren, LEFT, buff=0.1)
|
||||||
|
if background_rect:
|
||||||
|
det.add_background_rectangle()
|
||||||
|
det_text = VMobject(det, l_paren, r_paren)
|
||||||
|
if determinant is not None:
|
||||||
|
eq = TexMobject("=")
|
||||||
|
eq.next_to(r_paren, RIGHT, buff=0.1)
|
||||||
|
result = TexMobject(str(determinant))
|
||||||
|
result.next_to(eq, RIGHT, buff=0.2)
|
||||||
|
det_text.add(eq, result)
|
||||||
|
return det_text
|
||||||
|
|
|
@ -1,24 +1,6 @@
|
||||||
from big_ol_pile_of_manim_imports import *
|
from big_ol_pile_of_manim_imports import *
|
||||||
from eola.chapter3 import MatrixVectorMultiplicationAbstract
|
from eola.chapter3 import MatrixVectorMultiplicationAbstract
|
||||||
|
|
||||||
def get_det_text(matrix, determinant = None, background_rect = True):
|
|
||||||
parens = TexMobject(["(", ")"])
|
|
||||||
parens.scale(2)
|
|
||||||
parens.stretch_to_fit_height(matrix.get_height())
|
|
||||||
l_paren, r_paren = parens.split()
|
|
||||||
l_paren.next_to(matrix, LEFT, buff = 0.1)
|
|
||||||
r_paren.next_to(matrix, RIGHT, buff = 0.1)
|
|
||||||
det = TextMobject("det").next_to(l_paren, LEFT, buff = 0.1)
|
|
||||||
if background_rect:
|
|
||||||
det.add_background_rectangle()
|
|
||||||
det_text = VMobject(det, l_paren, r_paren)
|
|
||||||
if determinant is not None:
|
|
||||||
eq = TexMobject("=")
|
|
||||||
eq.next_to(r_paren, RIGHT, buff = 0.1)
|
|
||||||
result = TexMobject(str(determinant))
|
|
||||||
result.next_to(eq, RIGHT, buff = 0.2)
|
|
||||||
det_text.add(eq, result)
|
|
||||||
return det_text
|
|
||||||
|
|
||||||
class Blob(Circle):
|
class Blob(Circle):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
|
|
Loading…
Add table
Reference in a new issue