Skip to content

Commit 643fb2a

Browse files
committed
Chapter 6 operator syntax and notebook updates.
1 parent 0ce18ea commit 643fb2a

7 files changed

Lines changed: 301 additions & 17 deletions

File tree

Chapter6/Scene1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ def construct(self):
134134
self.play(Write(edge_count))
135135
self.wait(1)
136136

137+
with self.voiceover(
138+
"""The same operation can be written more concisely using Python's OR
139+
operator. The expression A pipe B creates a lazy union, which is then
140+
passed to binary plus to produce the result."""
141+
):
142+
# Transform code to show operator syntax
143+
code2 = Code(
144+
code_string="binary.plus(A | B).new()",
145+
language="python",
146+
background="window",
147+
).scale(0.7)
148+
code2.next_to(title, DOWN, buff=0.3)
149+
self.play(Transform(code, code2))
150+
self.wait(1)
151+
137152
# Cleanup
138153
self.play(
139154
FadeOut(title), FadeOut(code),

Chapter6/Scene2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ def construct(self):
140140
self.play(Write(comparison))
141141
self.wait(1)
142142

143+
with self.voiceover(
144+
"""Similarly, Python's AND operator provides a shorthand for intersection.
145+
A ampersand B creates a lazy intersection, which binary times then
146+
materializes into the result."""
147+
):
148+
# Transform code to show operator syntax
149+
code2 = Code(
150+
code_string="binary.times(A & B).new()",
151+
language="python",
152+
background="window",
153+
).scale(0.7)
154+
code2.next_to(title, DOWN, buff=0.3)
155+
self.play(Transform(code, code2))
156+
self.wait(1)
157+
143158
# Cleanup
144159
self.play(
145160
FadeOut(title), FadeOut(code),

Chapter6/Scene3.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ def construct(self):
121121
self.play(FadeIn(result_mat), Write(result_label))
122122
self.wait(1)
123123

124+
with self.voiceover(
125+
"""Comparison operators like greater-than can create boolean masks
126+
directly. The expression W greater than 3 produces a matrix of true
127+
and false values indicating which elements pass the test."""
128+
):
129+
# Transform code to show operator syntax
130+
code2 = Code(
131+
code_string="(W > 3).new()",
132+
language="python",
133+
background="window",
134+
).scale(0.7)
135+
code2.next_to(mat, DOWN, buff=0.4)
136+
self.play(Transform(code, code2))
137+
self.wait(1)
138+
124139
# Cleanup
125140
self.play(
126141
FadeOut(title), FadeOut(graph), FadeOut(label),

Chapter6/Scene5.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ def construct(self):
105105

106106
self.wait(0.5)
107107

108+
with self.voiceover(
109+
"""This operation can also be written using Python's multiplication
110+
operator. The expression 2 times M applies the scalar on the left,
111+
producing the same result."""
112+
):
113+
# Transform code to show operator syntax
114+
code2 = Code(
115+
code_string="2 * M",
116+
language="python",
117+
background="window",
118+
).scale(0.7)
119+
code2.move_to(ORIGIN + UP * 0.5)
120+
self.play(Transform(code, code2))
121+
self.wait(0.5)
122+
108123
# Cleanup
109124
self.play(
110125
FadeOut(title), FadeOut(mat_m), FadeOut(mat_label),

Chapter6/Scene6.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ def construct(self):
115115
self.play(Write(contrast), Write(contrast_note))
116116
self.wait(1)
117117

118+
with self.voiceover(
119+
"""Division also uses Python's natural syntax. M divided by 2 applies
120+
the scalar on the right, matching the mathematical notation. All common
121+
Python mathematical operators are supported, making very natural looking
122+
code possible."""
123+
):
124+
# Transform code to show operator syntax
125+
code2 = Code(
126+
code_string="M / 2",
127+
language="python",
128+
background="window",
129+
).scale(0.7)
130+
code2.move_to(ORIGIN + UP * 0.5)
131+
self.play(Transform(code, code2))
132+
self.wait(0.5)
133+
118134
# Cleanup
119135
self.play(
120136
FadeOut(title), FadeOut(mat_m), FadeOut(mat_label),

docs/Chapter6_480p15.mp4

1.21 MB
Binary file not shown.

notebooks/06_elementwise_ops.ipynb

Lines changed: 225 additions & 17 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)