矩阵

限定名称: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。

参数:
  • matrix (Iterable) – 一个numpy 2D数组或列表的列表。

  • 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, r"\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=r"\{",
            right_bracket=r"\}")
        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=r"\langle",
            right_bracket=r"\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, r"\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=r"\{",
            right_bracket=r"\}")
        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=r"\langle",
            right_bracket=r"\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

返回括号mobject。

get_columns

将矩阵的列作为VGroup返回。

get_entries

返回矩阵的单个条目。

get_mob_matrix

返回底层mobject矩阵mobject。

get_rows

将矩阵的行作为VGroup返回。

set_column_colors

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

set_row_colors

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

属性

animate (动画)

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

animation_overrides (动画覆盖)

颜色

depth (深度)

mobject 的深度。

fill_color (填充颜色)

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

height (高度)

mobject 的高度。

n_points_per_curve (每条曲线的点数)

sheen_factor (光泽因子)

stroke_color (描边颜色)

width (宽度)

mobject 的宽度。

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

将括号添加到Matrix mobject。

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

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

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

返回:

当前矩阵对象(self)。

返回类型:

矩阵

_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))。

参数:
  • matrix (Iterable)

  • 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 (type[MathTex])

  • element_to_mobject_config (dict)

  • element_alignment_corner (Sequence[float])

  • left_bracket (str)

  • right_bracket (str)

  • stretch_brackets (bool)

  • bracket_config (dict)

add_background_to_entries()[source]

为矩阵添加黑色背景矩形,示例见上文。

返回:

当前矩阵对象(self)。

返回类型:

矩阵

get_brackets()[source]

返回括号mobject。

返回:

每个VGroup包含一个括号

返回类型:

List[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()[source]

将矩阵的列作为VGroup返回。

返回:

每个VGroup包含矩阵的一列。

返回类型:

List[VGroup]

示例

示例:GetColumnsExample

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

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

get_entries()[source]

返回矩阵的单个条目。

返回:

包含矩阵条目的VGroup。

返回类型:

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()[source]

返回底层mobject矩阵mobject。

返回:

每个VGroup包含矩阵的一行。

返回类型:

List[VGroup]

get_rows()[source]

将矩阵的行作为VGroup返回。

返回:

每个VGroup包含矩阵的一行。

返回类型:

List[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]

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

参数:

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

返回:

当前矩阵对象(self)。

返回类型:

矩阵

示例

示例: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)[source]

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

参数:

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

返回:

当前矩阵对象(self)。

返回类型:

矩阵

示例

示例: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)