矩阵

限定名称: manim.mobject.matrix.Matrix

class Matrix(matrix, v_buff=0.8, h_buff=1.3, bracket_h_buff=0.25, bracket_v_buff=0.25, add_background_rectangles_to_entries=False, include_background_rectangle=False, element_to_mobject=<class 'manim.mobject.text.tex_mobject.MathTex'>, element_to_mobject_config={}, element_alignment_corner=array([ 1., -1., 0.]), left_bracket='[', right_bracket=']', stretch_brackets=True, bracket_config={}, **kwargs)[source]

基础类: VMobject

一个在屏幕上显示矩阵的mobject。

Parameters:
  • matrix (Iterable) – 一个numpy二维数组或列表的列表。

  • v_buff (float) – 元素之间的垂直距离,默认为0.8。

  • h_buff (float) – 元素之间的水平距离,默认为1.3。

  • bracket_h_buff (float) – 括号与矩阵的距离,默认为 MED_SMALL_BUFF

  • bracket_v_buff (float) – 括号的高度,默认为 MED_SMALL_BUFF

  • add_background_rectangles_to_entries (bool) – True 如果应该为条目添加背景矩形,默认为 False

  • include_background_rectangle (bool) – True 如果应该包括背景矩形,默认情况下为 False

  • element_to_mobject (type[MathTex]) – 用于构造元素的mobject类,默认为 MathTex

  • element_to_mobject_config (dict) – 传递给element_to_mobject构造函数的额外参数,默认为{}

  • element_alignment_corner (Sequence[float]) – 元素对齐的角落,默认为 DR

  • left_bracket (str) – 左括号类型,默认为 "["

  • right_bracket (str) – 右括号类型,默认为 "]"

  • stretch_brackets (bool) – True 如果应该拉伸括号以适应矩阵内容的高度,默认为 True

  • bracket_config (dict) – 在构造括号时传递给MathTex的额外参数。

示例

第一个示例展示了该模块的多种用途,而第二个示例解释了选项add_background_rectangles_to_entriesinclude_background_rectangle的使用。

示例:MatrixExamples

../_images/MatrixExamples-2.png
from manim import *

class MatrixExamples(Scene):
    def construct(self):
        m0 = Matrix([[2, "\pi"], [-1, 1]])
        m1 = Matrix([[2, 0, 4], [-1, 1, 5]],
            v_buff=1.3,
            h_buff=0.8,
            bracket_h_buff=SMALL_BUFF,
            bracket_v_buff=SMALL_BUFF,
            left_bracket="\{",
            right_bracket="\}")
        m1.add(SurroundingRectangle(m1.get_columns()[1]))
        m2 = Matrix([[2, 1], [-1, 3]],
            element_alignment_corner=UL,
            left_bracket="(",
            right_bracket=")")
        m3 = Matrix([[2, 1], [-1, 3]],
            left_bracket="\\langle",
            right_bracket="\\rangle")
        m4 = Matrix([[2, 1], [-1, 3]],
        ).set_column_colors(RED, GREEN)
        m5 = Matrix([[2, 1], [-1, 3]],
        ).set_row_colors(RED, GREEN)
        g = Group(
            m0,m1,m2,m3,m4,m5
        ).arrange_in_grid(buff=2)
        self.add(g)
class MatrixExamples(Scene):
    def construct(self):
        m0 = Matrix([[2, "\pi"], [-1, 1]])
        m1 = Matrix([[2, 0, 4], [-1, 1, 5]],
            v_buff=1.3,
            h_buff=0.8,
            bracket_h_buff=SMALL_BUFF,
            bracket_v_buff=SMALL_BUFF,
            left_bracket="\{",
            right_bracket="\}")
        m1.add(SurroundingRectangle(m1.get_columns()[1]))
        m2 = Matrix([[2, 1], [-1, 3]],
            element_alignment_corner=UL,
            left_bracket="(",
            right_bracket=")")
        m3 = Matrix([[2, 1], [-1, 3]],
            left_bracket="\\langle",
            right_bracket="\\rangle")
        m4 = Matrix([[2, 1], [-1, 3]],
        ).set_column_colors(RED, GREEN)
        m5 = Matrix([[2, 1], [-1, 3]],
        ).set_row_colors(RED, GREEN)
        g = Group(
            m0,m1,m2,m3,m4,m5
        ).arrange_in_grid(buff=2)
        self.add(g)

示例:BackgroundRectanglesExample

../_images/BackgroundRectanglesExample-1.png
from manim import *

class BackgroundRectanglesExample(Scene):
    def construct(self):
        background= Rectangle().scale(3.2)
        background.set_fill(opacity=.5)
        background.set_color([TEAL, RED, YELLOW])
        self.add(background)
        m0 = Matrix([[12, -30], [-1, 15]],
            add_background_rectangles_to_entries=True)
        m1 = Matrix([[2, 0], [-1, 1]],
            include_background_rectangle=True)
        m2 = Matrix([[12, -30], [-1, 15]])
        g = Group(m0, m1, m2).arrange(buff=2)
        self.add(g)
class BackgroundRectanglesExample(Scene):
    def construct(self):
        background= Rectangle().scale(3.2)
        background.set_fill(opacity=.5)
        background.set_color([TEAL, RED, YELLOW])
        self.add(background)
        m0 = Matrix([[12, -30], [-1, 15]],
            add_background_rectangles_to_entries=True)
        m1 = Matrix([[2, 0], [-1, 1]],
            include_background_rectangle=True)
        m2 = Matrix([[12, -30], [-1, 15]])
        g = Group(m0, m1, m2).arrange(buff=2)
        self.add(g)

方法

add_background_to_entries

向矩阵添加一个黑色背景的矩形,参见上面的示例。

get_brackets

返回括号对象。

get_columns

返回矩阵的列作为VGroups。

get_entries

返回矩阵的各个条目。

get_mob_matrix

返回底层的 mob 矩阵 mobjects。

get_rows

将矩阵的行返回为VGroups。

set_column_colors

为矩阵的每一列设置单独的颜色。

set_row_colors

为矩阵的每一行设置单独的颜色。

属性

animate

用于动画化self的任何方法的应用。

animation_overrides

color

depth

mobject的深度。

fill_color

如果有多种颜色(用于渐变),则返回第一个颜色

height

mobject的高度。

n_points_per_curve

sheen_factor

stroke_color

width

mobject的宽度。

_add_brackets(left='[', right=']', **kwargs)[来源]

将括号添加到矩阵对象。

请参阅Latex文档以了解各种括号类型。

Parameters:
  • left (str) – 左括号,默认为“[”

  • right (str) – 右括号,默认为“]”

Returns:

当前的矩阵对象(self)。

Return type:

Matrix

_original__init__(matrix, v_buff=0.8, h_buff=1.3, bracket_h_buff=0.25, bracket_v_buff=0.25, add_background_rectangles_to_entries=False, include_background_rectangle=False, element_to_mobject=<class 'manim.mobject.text.tex_mobject.MathTex'>, element_to_mobject_config={}, element_alignment_corner=array([ 1., -1., 0.]), left_bracket='[', right_bracket=']', stretch_brackets=True, bracket_config={}, **kwargs)

初始化自身。有关准确的签名,请参阅 help(type(self))。

Parameters:
  • 矩阵 (可迭代对象)

  • v_buff (float)

  • h_buff (float)

  • bracket_h_buff (float)

  • bracket_v_buff (float)

  • add_background_rectangles_to_entries (bool)

  • include_background_rectangle (bool)

  • element_to_mobject (类型[MathTex])

  • element_to_mobject_config (字典)

  • element_alignment_corner (Sequence[float])

  • left_bracket (str)

  • right_bracket (str)

  • stretch_brackets (bool)

  • bracket_config (字典)

add_background_to_entries()[source]

向矩阵添加一个黑色背景的矩形, 参见上面的示例。

Returns:

当前的矩阵对象(self)。

Return type:

Matrix

get_brackets()[source]

返回括号对象。

Returns:

每个VGroup包含一个括号

Return type:

列表[VGroup]

示例

示例:GetBracketsExample

../_images/GetBracketsExample-1.png
from manim import *

class GetBracketsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 3], [1, 5]])
        bra = m0.get_brackets()
        colors = [BLUE, GREEN]
        for k in range(len(colors)):
            bra[k].set_color(colors[k])
        self.add(m0)
class GetBracketsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 3], [1, 5]])
        bra = m0.get_brackets()
        colors = [BLUE, GREEN]
        for k in range(len(colors)):
            bra[k].set_color(colors[k])
        self.add(m0)

get_columns()[来源]

返回矩阵的列作为VGroups。

Returns:

每个VGroup包含矩阵的一列。

Return type:

列表[VGroup]

示例

示例:GetColumnsExample

../_images/GetColumnsExample-1.png
from manim import *

class GetColumnsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 3], [1, 5]])
        m0.add(SurroundingRectangle(m0.get_columns()[1]))
        self.add(m0)
class GetColumnsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 3], [1, 5]])
        m0.add(SurroundingRectangle(m0.get_columns()[1]))
        self.add(m0)

get_entries()[来源]

返回矩阵的各个条目。

Returns:

包含矩阵条目的VGroup。

Return type:

VGroup

示例

示例:GetEntriesExample

../_images/GetEntriesExample-1.png
from manim import *

class GetEntriesExample(Scene):
    def construct(self):
        m0 = Matrix([[2, 3], [1, 5]])
        ent = m0.get_entries()
        colors = [BLUE, GREEN, YELLOW, RED]
        for k in range(len(colors)):
            ent[k].set_color(colors[k])
        self.add(m0)
class GetEntriesExample(Scene):
    def construct(self):
        m0 = Matrix([[2, 3], [1, 5]])
        ent = m0.get_entries()
        colors = [BLUE, GREEN, YELLOW, RED]
        for k in range(len(colors)):
            ent[k].set_color(colors[k])
        self.add(m0)

get_mob_matrix()[来源]

返回底层的 mob 矩阵 mobjects。

Returns:

每个VGroup包含矩阵的一行。

Return type:

列表[VGroup]

get_rows()[source]

将矩阵的行返回为VGroups。

Returns:

每个VGroup包含矩阵的一行。

Return type:

列表[VGroup]

示例

示例:GetRowsExample

../_images/GetRowsExample-1.png
from manim import *

class GetRowsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 3], [1, 5]])
        m0.add(SurroundingRectangle(m0.get_rows()[1]))
        self.add(m0)
class GetRowsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 3], [1, 5]])
        m0.add(SurroundingRectangle(m0.get_rows()[1]))
        self.add(m0)

set_column_colors(*colors)[source]

为矩阵的每一列设置单独的颜色。

Parameters:

colors (str) – 颜色列表;每个指定的颜色对应一列。

Returns:

当前的矩阵对象(self)。

Return type:

Matrix

示例

示例:SetColumnColorsExample

../_images/SetColumnColorsExample-1.png
from manim import *

class SetColumnColorsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 1], [-1, 3]],
        ).set_column_colors([RED,BLUE], GREEN)
        self.add(m0)
class SetColumnColorsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 1], [-1, 3]],
        ).set_column_colors([RED,BLUE], GREEN)
        self.add(m0)

set_row_colors(*colors)[来源]

为矩阵的每一行设置单独的颜色。

Parameters:

colors (str) – 颜色列表;每个指定的颜色对应一行。

Returns:

当前的矩阵对象(self)。

Return type:

Matrix

示例

示例:SetRowColorsExample

../_images/SetRowColorsExample-1.png
from manim import *

class SetRowColorsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 1], [-1, 3]],
        ).set_row_colors([RED,BLUE], GREEN)
        self.add(m0)
class SetRowColorsExample(Scene):
    def construct(self):
        m0 = Matrix([["\pi", 1], [-1, 3]],
        ).set_row_colors([RED,BLUE], GREEN)
        self.add(m0)