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
+ """
2
26
3
27
__all__ = [
4
28
"Matrix" ,
@@ -64,6 +88,8 @@ def vector_coordinate_label(vector_mob, integer_labels=True, n_dim=2, color=WHIT
64
88
65
89
66
90
class Matrix (VMobject ):
91
+ """A mobject that displays a matrix on the screen."""
92
+
67
93
def __init__ (
68
94
self ,
69
95
matrix ,
@@ -81,9 +107,36 @@ def __init__(
81
107
** kwargs ,
82
108
):
83
109
"""
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
+
86
138
"""
139
+
87
140
self .v_buff = v_buff
88
141
self .h_buff = h_buff
89
142
self .bracket_h_buff = bracket_h_buff
@@ -121,7 +174,7 @@ def matrix_to_mob_matrix(self, matrix):
121
174
122
175
def organize_mob_matrix (self , matrix ):
123
176
for i , row in enumerate (matrix ):
124
- for j , elem in enumerate (row ):
177
+ for j , _ in enumerate (row ):
125
178
mob = matrix [i ][j ]
126
179
mob .move_to (
127
180
i * self .v_buff * DOWN + j * self .h_buff * RIGHT ,
@@ -130,6 +183,23 @@ def organize_mob_matrix(self, matrix):
130
183
return self
131
184
132
185
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
+
133
203
bracket_pair = MathTex (left , right )
134
204
bracket_pair .scale (2 )
135
205
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]"):
141
211
return self
142
212
143
213
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
+ """
144
221
return VGroup (
145
222
* [VGroup (* self .mob_matrix [:, i ]) for i in range (self .mob_matrix .shape [1 ])]
146
223
)
147
224
148
225
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
+ """
149
238
columns = self .get_columns ()
150
239
for color , column in zip (colors , columns ):
151
240
column .set_color (color )
@@ -187,23 +276,58 @@ def add_background_to_entries(self):
187
276
return self
188
277
189
278
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
+ """
190
286
return self .mob_matrix
191
287
192
288
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
+ """
193
296
return VGroup (* self .get_mob_matrix ().flatten ())
194
297
195
298
def get_brackets (self ):
299
+ """Return the bracket mobjects
300
+
301
+ Returns
302
+ --------
303
+ List[:class:`~.VGroup`]
304
+ Each VGroup contains a bracket
305
+ """
196
306
return self .brackets
197
307
198
308
199
309
class DecimalMatrix (Matrix ):
310
+ """A mobject that displays a matrix with decimal entries on the screen."""
311
+
200
312
def __init__ (
201
313
self ,
202
314
matrix ,
203
315
element_to_mobject = DecimalNumber ,
204
316
element_to_mobject_config = {"num_decimal_places" : 1 },
205
317
** kwargs ,
206
318
):
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
+ """
207
331
Matrix .__init__ (
208
332
self ,
209
333
matrix ,
@@ -214,18 +338,76 @@ def __init__(
214
338
215
339
216
340
class IntegerMatrix (Matrix ):
341
+ """A mobject that displays a matrix with integer entries on the screen."""
342
+
217
343
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
+ """
218
354
Matrix .__init__ (self , matrix , element_to_mobject = element_to_mobject , ** kwargs )
219
355
220
356
221
357
class MobjectMatrix (Matrix ):
358
+ """A mobject that displays a matrix of mobject entries on the screen."""
359
+
222
360
def __init__ (self , matrix , element_to_mobject = lambda m : m , ** kwargs ):
223
361
Matrix .__init__ (self , matrix , element_to_mobject = element_to_mobject , ** kwargs )
224
362
225
363
226
364
def get_det_text (
227
365
matrix , determinant = None , background_rect = False , initial_scale_factor = 2
228
366
):
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
+ """
229
411
parens = MathTex ("(" , ")" )
230
412
parens .scale (initial_scale_factor )
231
413
parens .stretch_to_fit_height (matrix .height )
0 commit comments