Matrix¶
Qualified name: 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]¶
Bases:
VMobjectA mobject that displays a matrix on the screen.
- Parameters:
matrix (Iterable) – A numpy 2d array or list of lists.
v_buff (float) – Vertical distance between elements, by default 0.8.
h_buff (float) – Horizontal distance between elements, by default 1.3.
bracket_h_buff (float) – Distance of the brackets from the matrix, by default
MED_SMALL_BUFF.bracket_v_buff (float) – Height of the brackets, by default
MED_SMALL_BUFF.add_background_rectangles_to_entries (bool) –
Trueif should add backgraound rectangles to entries, by defaultFalse.include_background_rectangle (bool) –
Trueif should include background rectangle, by defaultFalse.element_to_mobject (type[Mobject] | Callable[..., Mobject]) – The mobject class used to construct the elements, by default
MathTex.element_to_mobject_config (dict) – Additional arguments to be passed to the constructor in
element_to_mobject, by default{}.element_alignment_corner (Sequence[float]) – The corner to which elements are aligned, by default
DR.left_bracket (str) – The left bracket type, by default
"[".right_bracket (str) – The right bracket type, by default
"]".stretch_brackets (bool) –
Trueif should stretch the brackets to fit the height of matrix contents, by defaultTrue.bracket_config (dict) – Additional arguments to be passed to
MathTexwhen constructing the brackets.kwargs (Any)
Examples
The first example shows a variety of uses of this module while the second example exlpains the use of the options add_background_rectangles_to_entries and include_background_rectangle.
Example: MatrixExamples ¶
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)Example: 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)Methods
Add a black background rectangle to the matrix, see above for an example.
Return the bracket mobjects.
Return columns of the matrix as VGroups.
Return the individual entries of the matrix.
Return the underlying mob matrix mobjects.
Return rows of the matrix as VGroups.
Set individual colors for each columns of the matrix.
Set individual colors for each row of the matrix.
Attributes
alwaysCall a method on a mobject every frame.
animateUsed to animate the application of any method of
self.animation_overridescolordepthThe depth of the mobject.
fill_colorIf there are multiple colors (for gradient) this returns the first one
heightThe height of the mobject.
n_points_per_curvesheen_factorstroke_colorwidthThe width of the mobject.
- _add_brackets(left='[', right=']', **kwargs)[source]¶
Adds the brackets to the Matrix mobject.
See Latex document for various bracket types.
- Parameters:
left (str) – the left bracket, by default “[”
right (str) – the right bracket, by default “]”
kwargs (Any)
- Returns:
The current matrix object (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)¶
Initialize self. See help(type(self)) for accurate signature.
- Parameters:
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[Mobject] | Callable[[...], Mobject])
element_to_mobject_config (dict)
element_alignment_corner (Sequence[float])
left_bracket (str)
right_bracket (str)
stretch_brackets (bool)
bracket_config (dict)
kwargs (Any)
- add_background_to_entries()[source]¶
Add a black background rectangle to the matrix, see above for an example.
- Returns:
The current matrix object (self).
- Return type:
- get_brackets()[source]¶
Return the bracket mobjects.
- Returns:
A VGroup containing the left and right bracket.
- Return type:
Examples
Example: 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()[source]¶
Return columns of the matrix as VGroups.
- Returns:
The VGroup contains a nested VGroup for each column of the matrix.
- Return type:
Examples
Example: GetColumnsExample ¶
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]¶
Return the individual entries of the matrix.
- Returns:
VGroup containing entries of the matrix.
- Return type:
Examples
Example: 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_mob_matrix()[source]¶
Return the underlying mob matrix mobjects.
- Returns:
Each VGroup contains a row of the matrix.
- Return type:
List[
VGroup]
- get_rows()[source]¶
Return rows of the matrix as VGroups.
- Returns:
The VGroup contains a nested VGroup for each row of the matrix.
- Return type:
Examples
Example: 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]¶
Set individual colors for each columns of the matrix.
- Parameters:
colors (str) – The list of colors; each color specified corresponds to a column.
- Returns:
The current matrix object (self).
- Return type:
Examples
Example: 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)[source]¶
Set individual colors for each row of the matrix.
- Parameters:
colors (str) – The list of colors; each color specified corresponds to a row.
- Returns:
The current matrix object (self).
- Return type:
Examples
Example: 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)