Skip to content

Commit 7bfabe3

Browse files
authored
add api docs and examples for Matrix mobjects (#1047)
* add api docs and examples for Matrix mobjects * incorporate review suggestions * add :class: prefix in front of type annotations
1 parent 68d33d9 commit 7bfabe3

File tree

1 file changed

+186
-4
lines changed

1 file changed

+186
-4
lines changed

manim/mobject/matrix.py

Lines changed: 186 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
"""Mobjects representing matrices."""
1+
r"""Mobjects representing matrices.
2+
3+
Examples
4+
--------
5+
6+
.. manim:: MatrixExamples
7+
:save_last_frame:
8+
9+
class MatrixExamples(Scene):
10+
def construct(self):
11+
m0 = Matrix([[2, 0], [-1, 1]])
12+
m1 = Matrix([[1, 0], [0, 1]],
13+
left_bracket="\\big(",
14+
right_bracket="\\big)")
15+
m2 = DecimalMatrix(
16+
[[3.456, 2.122], [33.2244, 12.33]],
17+
element_to_mobject_config={"num_decimal_places": 2},
18+
left_bracket="\\{",
19+
right_bracket="\\}")
20+
21+
self.add(m0.shift(LEFT - (3, 0, 0)))
22+
self.add(m1)
23+
self.add(m2.shift(RIGHT + (3, 0, 0)))
24+
25+
"""
226

327
__all__ = [
428
"Matrix",
@@ -64,6 +88,8 @@ def vector_coordinate_label(vector_mob, integer_labels=True, n_dim=2, color=WHIT
6488

6589

6690
class Matrix(VMobject):
91+
"""A mobject that displays a matrix on the screen."""
92+
6793
def __init__(
6894
self,
6995
matrix,
@@ -81,9 +107,36 @@ def __init__(
81107
**kwargs,
82108
):
83109
"""
84-
Matrix can either either include numbers, tex_strings,
85-
or mobjects
110+
111+
Parameters
112+
----------
113+
matrix : :class:`typing.Iterable`
114+
A numpy 2d array or list of lists
115+
v_buff : :class:`float`, optional
116+
vertical buffer, by default 0.8
117+
h_buff : :class:`float`, optional
118+
horizontal buffer, by default 1.3
119+
bracket_h_buff : :class:`float`, optional
120+
bracket horizonal buffer, by default MED_SMALL_BUFF
121+
bracket_v_buff : :class:`float`, optional
122+
bracket veritical buffer, by default MED_SMALL_BUFF
123+
add_background_rectangles_to_entries : :class:`bool`, optional
124+
`True` if should add backgraound rectangles to entries, by default False
125+
include_background_rectangle : :class:`bool`, optional
126+
`True` if should include background rectangle, by default False
127+
element_to_mobject : :class:`~.Mobject`, optional
128+
element to mobject, by default MathTex
129+
element_to_mobject_config : Dict[:class:`str`, :class:`~.Mobject`], optional
130+
element to mobject config, by default {}
131+
element_alignment_corner : :class:`np.ndarray`, optional
132+
the element alignment corner, by default DR
133+
left_bracket : :class:`str`, optional
134+
the left bracket type, by default "\\\\big["
135+
right_bracket : :class:`str`, optional
136+
the right bracket type, by default "\\\\big]"
137+
86138
"""
139+
87140
self.v_buff = v_buff
88141
self.h_buff = h_buff
89142
self.bracket_h_buff = bracket_h_buff
@@ -121,7 +174,7 @@ def matrix_to_mob_matrix(self, matrix):
121174

122175
def organize_mob_matrix(self, matrix):
123176
for i, row in enumerate(matrix):
124-
for j, elem in enumerate(row):
177+
for j, _ in enumerate(row):
125178
mob = matrix[i][j]
126179
mob.move_to(
127180
i * self.v_buff * DOWN + j * self.h_buff * RIGHT,
@@ -130,6 +183,23 @@ def organize_mob_matrix(self, matrix):
130183
return self
131184

132185
def add_brackets(self, left="\\big[", right="\\big]"):
186+
"""Add the brackets to the Matrix mobject
187+
188+
See Latex document for various bracket types.
189+
190+
Parameters
191+
----------
192+
left : :class:`str`, optional
193+
the left bracket, by default "\\\\big["
194+
right : :class:`str`, optional
195+
the right bracket, by default "\\\\big]"
196+
197+
Returns
198+
-------
199+
:class:`Matrix`
200+
The current matrix object (self).
201+
"""
202+
133203
bracket_pair = MathTex(left, right)
134204
bracket_pair.scale(2)
135205
bracket_pair.stretch_to_fit_height(self.height + 2 * self.bracket_v_buff)
@@ -141,11 +211,30 @@ def add_brackets(self, left="\\big[", right="\\big]"):
141211
return self
142212

143213
def get_columns(self):
214+
"""Return columns of the matrix as VGroups
215+
216+
Returns
217+
--------
218+
List[:class:`~.VGroup`]
219+
Each VGroup contains a column of the matrix.
220+
"""
144221
return VGroup(
145222
*[VGroup(*self.mob_matrix[:, i]) for i in range(self.mob_matrix.shape[1])]
146223
)
147224

148225
def set_column_colors(self, *colors):
226+
"""Set individual colors for each columns of the matrix
227+
228+
Parameters
229+
----------
230+
colors : :class:`str`
231+
The list of colors; each color specified corresponds to a column.
232+
233+
Returns
234+
-------
235+
:class:`Matrix`
236+
The current matrix object (self).
237+
"""
149238
columns = self.get_columns()
150239
for color, column in zip(colors, columns):
151240
column.set_color(color)
@@ -187,23 +276,58 @@ def add_background_to_entries(self):
187276
return self
188277

189278
def get_mob_matrix(self):
279+
"""Return the underlying mob matrix mobjects
280+
281+
Returns
282+
--------
283+
List[:class:`~.VGroup`]
284+
Each VGroup contains a row of the matrix.
285+
"""
190286
return self.mob_matrix
191287

192288
def get_entries(self):
289+
"""Return the individual entries of the matrix
290+
291+
Returns
292+
--------
293+
:class:`~.VGroup`
294+
VGroup containing entries of the matrix
295+
"""
193296
return VGroup(*self.get_mob_matrix().flatten())
194297

195298
def get_brackets(self):
299+
"""Return the bracket mobjects
300+
301+
Returns
302+
--------
303+
List[:class:`~.VGroup`]
304+
Each VGroup contains a bracket
305+
"""
196306
return self.brackets
197307

198308

199309
class DecimalMatrix(Matrix):
310+
"""A mobject that displays a matrix with decimal entries on the screen."""
311+
200312
def __init__(
201313
self,
202314
matrix,
203315
element_to_mobject=DecimalNumber,
204316
element_to_mobject_config={"num_decimal_places": 1},
205317
**kwargs,
206318
):
319+
"""
320+
Will round/truncate the decimal places as per the provided config.
321+
322+
Parameters
323+
----------
324+
matrix : :class:`typing.Iterable`
325+
A numpy 2d array or list of lists
326+
element_to_mobject : :class:`~.Mobject`, optional
327+
Mobject to use, by default DecimalNumber
328+
element_to_mobject_config : Dict[:class:`str`, :class:`~.Mobject`], optional
329+
Config for the desired mobject, by default {"num_decimal_places": 1}
330+
"""
207331
Matrix.__init__(
208332
self,
209333
matrix,
@@ -214,18 +338,76 @@ def __init__(
214338

215339

216340
class IntegerMatrix(Matrix):
341+
"""A mobject that displays a matrix with integer entries on the screen."""
342+
217343
def __init__(self, matrix, element_to_mobject=Integer, **kwargs):
344+
"""
345+
Note- Will round if there are decimal entries in the matrix.
346+
347+
Parameters
348+
----------
349+
matrix : :class:`typing.Iterable`
350+
A numpy 2d array or list of lists
351+
element_to_mobject : :class:`~.Mobject`, optional
352+
Mobject to use, by default Integer
353+
"""
218354
Matrix.__init__(self, matrix, element_to_mobject=element_to_mobject, **kwargs)
219355

220356

221357
class MobjectMatrix(Matrix):
358+
"""A mobject that displays a matrix of mobject entries on the screen."""
359+
222360
def __init__(self, matrix, element_to_mobject=lambda m: m, **kwargs):
223361
Matrix.__init__(self, matrix, element_to_mobject=element_to_mobject, **kwargs)
224362

225363

226364
def get_det_text(
227365
matrix, determinant=None, background_rect=False, initial_scale_factor=2
228366
):
367+
r"""Helper function to create determinant
368+
369+
Parameters
370+
----------
371+
matrix : :class:`~.Matrix`
372+
The matrix whose determinant is to be created
373+
374+
determinant : :class:`int|str`
375+
The value of the determinant of the matrix
376+
377+
background_rect : :class:`bool`
378+
The background rectangle
379+
380+
initial_scale_factor : :class:`float`
381+
The scale of the text `det` w.r.t the matrix
382+
383+
Returns
384+
--------
385+
:class:`~.VGroup`
386+
A VGroup containing the determinant
387+
388+
Examples
389+
--------
390+
391+
.. manim:: DeterminantOfAMatrix
392+
:save_last_frame:
393+
394+
class DeterminantOfAMatrix(Scene):
395+
def construct(self):
396+
matrix = Matrix([
397+
[2, 0],
398+
[-1, 1]
399+
])
400+
401+
# scaling down the `det` string
402+
det = get_det_text(matrix,
403+
determinant=3,
404+
initial_scale_factor=1)
405+
406+
# must add the matrix
407+
self.add(matrix)
408+
self.add(det)
409+
410+
"""
229411
parens = MathTex("(", ")")
230412
parens.scale(initial_scale_factor)
231413
parens.stretch_to_fit_height(matrix.height)

0 commit comments

Comments
 (0)