矩阵¶
限定名称: 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_entries和include_background_rectangle的使用。
示例:MatrixExamples ¶
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 ¶
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)方法
向矩阵添加一个黑色背景的矩形,参见上面的示例。
返回括号对象。
返回矩阵的列作为VGroups。
返回矩阵的各个条目。
返回底层的 mob 矩阵 mobjects。
将矩阵的行返回为VGroups。
为矩阵的每一列设置单独的颜色。
为矩阵的每一行设置单独的颜色。
属性
animate用于动画化
self的任何方法的应用。animation_overridescolordepthmobject的深度。
fill_color如果有多种颜色(用于渐变),则返回第一个颜色
heightmobject的高度。
n_points_per_curvesheen_factorstroke_colorwidthmobject的宽度。
- _add_brackets(left='[', right=']', **kwargs)[来源]¶
将括号添加到矩阵对象。
请参阅Latex文档以了解各种括号类型。
- Parameters:
left (str) – 左括号,默认为“[”
right (str) – 右括号,默认为“]”
- Returns:
当前的矩阵对象(self)。
- Return type:
- _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 (字典)
- get_brackets()[source]¶
返回括号对象。
- Returns:
每个VGroup包含一个括号
- Return type:
列表[
VGroup]
示例
示例:GetBracketsExample ¶
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 ¶
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:
示例
示例:GetEntriesExample ¶
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_rows()[source]¶
将矩阵的行返回为VGroups。
- Returns:
每个VGroup包含矩阵的一行。
- Return type:
列表[
VGroup]
示例
示例:GetRowsExample ¶
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:
示例
示例:SetColumnColorsExample ¶
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:
示例
示例:SetRowColorsExample ¶
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)