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.svg.tex_mobject.MathTex'>, element_to_mobject_config={}, element_alignment_corner=array([ 1., -1., 0.]), left_bracket='[', right_bracket=']', **kwargs)[source]

Bases: manim.mobject.types.vectorized_mobject.VMobject

A mobject that displays a matrix on the screen.

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

../_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)

Example: 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)
Parameters
  • matrix (typing.Iterable) – A numpy 2d array or list of lists

  • v_buff (float, optional) – vertical buffer, by default 0.8

  • h_buff (float, optional) – horizontal buffer, by default 1.3

  • bracket_h_buff (float, optional) – bracket horizontal buffer, by default MED_SMALL_BUFF

  • bracket_v_buff (float, optional) – bracket vertical buffer, by default MED_SMALL_BUFF

  • add_background_rectangles_to_entries (bool, optional) – True if should add backgraound rectangles to entries, by default False

  • include_background_rectangle (bool, optional) – True if should include background rectangle, by default False

  • element_to_mobject (Mobject, optional) – element to mobject, by default MathTex

  • element_to_mobject_config (Dict[str, Mobject], optional) – element to mobject config, by default {}

  • element_alignment_corner (np.ndarray, optional) – the element alignment corner, by default DR

  • left_bracket (str, optional) – the left bracket type, by default “[”

  • right_bracket (str, optional) – the right bracket type, by default “]”

Methods

add_background_to_entries

Add a black background rectangle to the matrix, see above for an example.

add_brackets

Used internally.

get_brackets

Return the bracket mobjects.

get_columns

Return columns of the matrix as VGroups.

get_entries

Return the individual entries of the matrix.

get_mob_matrix

Return the underlying mob matrix mobjects.

get_rows

Return rows of the matrix as VGroups.

matrix_to_mob_matrix

Used internally.

organize_mob_matrix

Used internally.

set_column_colors

Set individual colors for each columns of the matrix.

set_row_colors

Set individual colors for each row of the matrix.

Attributes

animate

Used to animate the application of a method.

animation_overrides

depth

The depth of the mobject.

height

The height of the mobject.

width

The width of the mobject.

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

Matrix

add_brackets(left='[', right=']')[source]

Used internally. Adds the brackets to the Matrix mobject.

See Latex document for various bracket types.

Parameters
  • left (str, optional) – the left bracket, by default “[”

  • right (str, optional) – the right bracket, by default “]”

Returns

The current matrix object (self).

Return type

Matrix

get_brackets()[source]

Return the bracket mobjects.

Returns

Each VGroup contains a bracket

Return type

List[VGroup]

Examples

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

Return columns of the matrix as VGroups.

Returns

Each VGroup contains a column of the matrix.

Return type

List[VGroup]

Examples

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

Return the individual entries of the matrix.

Returns

VGroup containing entries of the matrix.

Return type

VGroup

Examples

Example: 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)
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

Each VGroup contains a row of the matrix.

Return type

List[VGroup]

Examples

Example: 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)
matrix_to_mob_matrix(matrix)[source]

Used internally.

organize_mob_matrix(matrix)[source]

Used internally.

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

Matrix

Examples

Example: 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)
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

Matrix

Examples

Example: 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)