Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Latest scenes for Fourier series, up to SimpleComplexExponentExample

Grant Sanderson64c960041b5b
diff --git a/active_projects/diffyq/all_part4_scenes.py b/active_projects/diffyq/all_part4_scenes.py
index bb69e6194..340d11fb5 100644
--- a/active_projects/diffyq/all_part4_scenes.py
+++ b/active_projects/diffyq/all_part4_scenes.py
@@ -3,11 +3,13 @@ from active_projects.diffyq.part4.fourier_series_scenes import *
from active_projects.diffyq.part4.pi_creature_scenes import *
from active_projects.diffyq.part4.three_d_graphs import *
from active_projects.diffyq.part4.temperature_scenes import *
+from active_projects.diffyq.part4.complex_functions import *
+
+from active_projects.diffyq.part3.staging import *
OUTPUT_DIRECTORY = "diffyq/part4"
SCENES_IN_ORDER = [
ComplexFourierSeriesExample,
- ComplexFourierSeriesExampleEnd,
FourierSeriesExampleWithRectForZoom,
ZoomedInFourierSeriesExample,
ZoomedInFourierSeriesExample10xMore,
@@ -25,8 +27,21 @@ SCENES_IN_ORDER = [
CommentOnFouriersImmortality,
HangOnThere,
ShowInfiniteSum,
- # Oldies
+ # TODO
+ BreakDownStepFunction,
+ StepFunctionSolutionFormla,
+ # How to compute
+ FourierSeriesOfLineIllustration,
+ GeneralizeToComplexFunctions,
+ ClarifyInputAndOutput,
+ GraphForFlattenedPi,
+ PiFourierSeries,
+ RealValuedFunctionFourierSeries,
+ YouSaidThisWasEasier,
+ AskAboutComplexNotVector,
+ SimpleComplexExponentExample,
+ # Oldies
# FourierSeriesIllustraiton,
# FourierNameIntro,
# CircleAnimationOfF,
diff --git a/active_projects/diffyq/part2/heat_equation.py b/active_projects/diffyq/part2/heat_equation.py
index 1339fed6e..88eb1853b 100644
--- a/active_projects/diffyq/part2/heat_equation.py
+++ b/active_projects/diffyq/part2/heat_equation.py
@@ -164,6 +164,7 @@ class BringTwoRodsTogether(Scene):
"tick_frequency": 10,
},
},
+ "y_labels": range(20, 100, 20),
"graph_x_min": 0,
"graph_x_max": 10,
"midpoint": 5,
@@ -190,9 +191,7 @@ class BringTwoRodsTogether(Scene):
y_label.to_edge(UP)
axes.y_axis.label = y_label
axes.y_axis.add(y_label)
- axes.y_axis.add_numbers(
- *range(20, 100, 20)
- )
+ axes.y_axis.add_numbers(*self.y_labels)
self.axes = axes
self.y_label = y_label
diff --git a/active_projects/diffyq/part3/staging.py b/active_projects/diffyq/part3/staging.py
index dd8c7d3b6..baa5dad86 100644
--- a/active_projects/diffyq/part3/staging.py
+++ b/active_projects/diffyq/part3/staging.py
@@ -332,7 +332,7 @@ class FourierSeriesIllustraiton(Scene):
arrow = Arrow(LEFT, RIGHT, color=WHITE)
group = VGroup(axes1, arrow, axes2)
- group.arrange(RIGHT, buff=LARGE_BUFF)
+ group.arrange(RIGHT, buff=MED_LARGE_BUFF)
return group
def get_sine_graphs(self, axes):
diff --git a/active_projects/diffyq/part3/temperature_graphs.py b/active_projects/diffyq/part3/temperature_graphs.py
index 18b5ad5a4..13d2c1967 100644
--- a/active_projects/diffyq/part3/temperature_graphs.py
+++ b/active_projects/diffyq/part3/temperature_graphs.py
@@ -379,6 +379,10 @@ class BreakDownAFunction(SimpleCosExpGraph):
"z_min": -2,
"y_max": 20,
},
+ "low_axes_config": {
+ "z_min": -3,
+ "z_axis_config": {"unit_size": 1}
+ },
"n_low_axes": 4,
"k": 0.2,
}
@@ -402,10 +406,7 @@ class BreakDownAFunction(SimpleCosExpGraph):
top_axes.center()
top_axes.to_edge(UP)
- low_axes = self.get_three_d_axes(
- z_min=-3,
- z_axis_config={"unit_size": 1}
- )
+ low_axes = self.get_three_d_axes(**self.low_axes_config)
low_axes.y_axis.set_opacity(0)
for axis in low_axes:
axis.label.fade(1)
diff --git a/active_projects/diffyq/part4/complex_functions.py b/active_projects/diffyq/part4/complex_functions.py
new file mode 100644
index 000000000..331987b5b
--- /dev/null
+++ b/active_projects/diffyq/part4/complex_functions.py
@@ -0,0 +1,674 @@
+from manimlib.imports import *
+
+
+class GeneralizeToComplexFunctions(Scene):
+ CONFIG = {
+ "axes_config": {
+ "x_min": 0,
+ "x_max": 10,
+ "x_axis_config": {
+ "stroke_width": 2,
+ },
+ "y_min": -2.5,
+ "y_max": 2.5,
+ "y_axis_config": {
+ "tick_frequency": 0.25,
+ "unit_size": 1.5,
+ "include_tip": False,
+ "stroke_width": 2,
+ },
+ },
+ "complex_plane_config": {
+ "axis_config": {
+ "unit_size": 2
+ }
+ },
+ }
+
+ def construct(self):
+ self.show_cosine_wave()
+ self.transition_to_complex_plane()
+ self.add_rotating_vectors_making_cos()
+
+ def show_cosine_wave(self):
+ axes = Axes(**self.axes_config)
+ axes.shift(2 * LEFT - axes.c2p(0, 0))
+ y_axis = axes.y_axis
+ y_labels = y_axis.get_number_mobjects(
+ *range(-2, 3),
+ number_config={"num_decimal_places": 1},
+ )
+
+ t_tracker = ValueTracker(0)
+ t_tracker.add_updater(lambda t, dt: t.increment_value(dt))
+ get_t = t_tracker.get_value
+
+ def func(x):
+ return 2 * np.cos(x)
+
+ cos_x_max = 20
+ cos_wave = axes.get_graph(func, x_max=cos_x_max)
+ cos_wave.set_color(YELLOW)
+ shown_cos_wave = cos_wave.copy()
+ shown_cos_wave.add_updater(
+ lambda m: m.pointwise_become_partial(
+ cos_wave, 0,
+ np.clip(get_t() / cos_x_max, 0, 1),
+ ),
+ )
+
+ dot = Dot()
+ dot.set_color(PINK)
+ dot.add_updater(lambda d: d.move_to(
+ y_axis.n2p(func(get_t())),
+ ))
+
+ h_line = always_redraw(lambda: Line(
+ dot.get_right(),
+ shown_cos_wave.get_end(),
+ stroke_width=1,
+ ))
+
+ real_words = TextMobject(
+ "Real number\\\\output"
+ )
+ real_words.to_edge(LEFT)
+ real_words.shift(2 * UP)
+ real_arrow = Arrow()
+ real_arrow.add_updater(
+ lambda m: m.put_start_and_end_on(
+ real_words.get_corner(DR),
+ dot.get_center(),
+ ).scale(0.9),
+ )
+
+ self.add(t_tracker)
+ self.add(axes)
+ self.add(y_labels)
+ self.add(shown_cos_wave)
+ self.add(dot)
+ self.add(h_line)
+
+ self.wait(2)
+ self.play(
+ FadeInFrom(real_words, RIGHT),
+ FadeIn(real_arrow),
+ )
+ self.wait(5)
+
+ y_axis.generate_target()
+ y_axis.target.rotate(-90 * DEGREES)
+ y_axis.target.center()
+ y_axis.target.scale(2 / 1.5)
+ y_labels.generate_target()
+ for label in y_labels.target:
+ label.next_to(
+ y_axis.target.n2p(label.get_value()),
+ DOWN, MED_SMALL_BUFF,
+ )
+ self.play(
+ FadeOut(shown_cos_wave),
+ FadeOut(axes.x_axis),
+ FadeOut(h_line),
+ )
+ self.play(
+ MoveToTarget(y_axis),
+ MoveToTarget(y_labels),
+ real_words.shift, 2 * RIGHT + UP,
+ )
+ self.wait()
+
+ self.y_axis = y_axis
+ self.y_labels = y_labels
+ self.real_words = real_words
+ self.real_arrow = real_arrow
+ self.dot = dot
+ self.t_tracker = t_tracker
+
+ def transition_to_complex_plane(self):
+ y_axis = self.y_axis
+ y_labels = self.y_labels
+
+ plane = self.get_complex_plane()
+ plane_words = plane.label
+
+ self.add(plane, *self.get_mobjects())
+ self.play(
+ FadeOut(y_labels),
+ FadeOut(y_axis),
+ ShowCreation(plane),
+ )
+ self.play(Write(plane_words))
+ self.wait()
+
+ self.plane = plane
+ self.plane_words = plane_words
+
+ def add_rotating_vectors_making_cos(self):
+ plane = self.plane
+ real_words = self.real_words
+ real_arrow = self.real_arrow
+ t_tracker = self.t_tracker
+ get_t = t_tracker.get_value
+
+ v1 = Vector(2 * RIGHT)
+ v2 = Vector(2 * RIGHT)
+ v1.set_color(BLUE)
+ v2.set_color(interpolate_color(GREY_BROWN, WHITE, 0.5))
+ v1.add_updater(
+ lambda v: v.set_angle(get_t())
+ )
+ v2.add_updater(
+ lambda v: v.set_angle(-get_t())
+ )
+ v1.add_updater(
+ lambda v: v.shift(plane.n2p(0) - v.get_start())
+ )
+ # Change?
+ v2.add_updater(
+ lambda v: v.shift(plane.n2p(0) - v.get_start())
+ )
+
+ ghost_v1 = v1.copy()
+ ghost_v1.set_opacity(0.5)
+ ghost_v1.add_updater(
+ lambda v: v.shift(
+ v2.get_end() - v.get_start()
+ )
+ )
+
+ ghost_v2 = v2.copy()
+ ghost_v2.set_opacity(0.5)
+ ghost_v2.add_updater(
+ lambda v: v.shift(
+ v1.get_end() - v.get_start()
+ )
+ )
+
+ circle = Circle(color=GREY_BROWN)
+ circle.set_stroke(width=1)
+ circle.set_width(2 * v1.get_length())
+ circle.move_to(plane.n2p(0))
+
+ formula = TexMobject(
+ # "\\cos(x) ="
+ # "{1 \\over 2}e^{ix} +"
+ # "{1 \\over 2}e^{-ix}",
+ "2\\cos(x) =",
+ "e^{ix}", "+", "e^{-ix}",
+ tex_to_color_map={
+ "e^{ix}": v1.get_color(),
+ "e^{-ix}": v2.get_color(),
+ }
+ )
+ formula.next_to(ORIGIN, UP, buff=0.75)
+ # formula.add_background_rectangle()
+ formula.set_stroke(BLACK, 3, background=True)
+ formula.to_edge(LEFT, buff=MED_SMALL_BUFF)
+ formula_brace = Brace(formula[1:], UP)
+ formula_words = formula_brace.get_text(
+ "Sum of\\\\rotations"
+ )
+ formula_words.set_stroke(BLACK, 3, background=True)
+
+ randy = Randolph()
+ randy.to_corner(DL)
+ randy.look_at(formula)
+
+ self.play(
+ FadeOut(real_words),
+ FadeOut(real_arrow),
+ )
+ self.play(
+ FadeIn(v1),
+ FadeIn(v2),
+ FadeIn(circle),
+ FadeIn(ghost_v1),
+ FadeIn(ghost_v2),
+ )
+ self.wait(3)
+ self.play(FadeInFromDown(formula))
+ self.play(
+ GrowFromCenter(formula_brace),
+ FadeIn(formula_words),
+ )
+ self.wait(2)
+ self.play(FadeIn(randy))
+ self.play(randy.change, "pleading")
+ self.play(Blink(randy))
+ self.wait()
+ self.play(randy.change, "confused")
+ self.play(Blink(randy))
+ self.wait()
+ self.play(FadeOut(randy))
+ self.wait(20)
+
+ #
+ def get_complex_plane(self):
+ plane = ComplexPlane(**self.complex_plane_config)
+ plane.add_coordinates()
+
+ plane.label = TextMobject("Complex plane")
+ plane.label.scale(1.5)
+ plane.label.to_corner(UR, buff=MED_SMALL_BUFF)
+ return plane
+
+
+class ClarifyInputAndOutput(GeneralizeToComplexFunctions):
+ CONFIG = {
+ "input_space_rect_config": {
+ "stroke_color": WHITE,
+ "stroke_width": 1,
+ "fill_color": DARKER_GREY,
+ "fill_opacity": 1,
+ "width": 6,
+ "height": 2,
+ },
+ }
+
+ def construct(self):
+ self.setup_plane()
+ self.setup_input_space()
+ self.setup_input_trackers()
+
+ self.describe_input()
+ self.describe_output()
+
+ def setup_plane(self):
+ plane = self.get_complex_plane()
+ plane.sublabel = TextMobject("(Output space)")
+ plane.sublabel.add_background_rectangle()
+ plane.sublabel.next_to(plane.label, DOWN)
+ self.add(plane, plane.label)
+ self.plane = plane
+
+ def setup_input_space(self):
+ rect = Rectangle(**self.input_space_rect_config)
+ rect.to_corner(UL, buff=SMALL_BUFF)
+
+ input_line = self.get_input_line(rect)
+ input_words = TextMobject("Input space")
+ input_words.next_to(
+ rect.get_bottom(), UP,
+ SMALL_BUFF,
+ )
+
+ self.add(rect)
+ self.add(input_line)
+
+ self.input_rect = rect
+ self.input_line = input_line
+ self.input_words = input_words
+
+ def setup_input_trackers(self):
+ plane = self.plane
+ input_line = self.input_line
+ input_tracker = ValueTracker(0)
+ get_input = input_tracker.get_value
+
+ input_dot = Dot()
+ input_dot.set_color(PINK)
+ f_always(
+ input_dot.move_to,
+ lambda: input_line.n2p(get_input())
+ )
+
+ input_decimal = DecimalNumber()
+ input_decimal.scale(0.7)
+ always(input_decimal.next_to, input_dot, UP)
+ f_always(input_decimal.set_value, get_input)
+
+ path = self.get_path()
+
+ def get_output_point():
+ return path.point_from_proportion(
+ get_input()
+ )
+
+ output_dot = Dot()
+ output_dot.match_style(input_dot)
+ f_always(output_dot.move_to, get_output_point)
+
+ output_vector = Vector()
+ output_vector.set_color(WHITE)
+ output_vector.add_updater(
+ lambda v: v.put_start_and_end_on(
+ plane.n2p(0),
+ get_output_point()
+ )
+ )
+
+ output_decimal = DecimalNumber()
+ output_decimal.scale(0.7)
+ always(output_decimal.next_to, output_dot, UR, SMALL_BUFF)
+ f_always(
+ output_decimal.set_value,
+ lambda: plane.p2n(get_output_point()),
+ )
+
+ self.input_tracker = input_tracker
+ self.input_dot = input_dot
+ self.input_decimal = input_decimal
+ self.path = path
+ self.output_dot = output_dot
+ self.output_vector = output_vector
+ self.output_decimal = output_decimal
+
+ def describe_input(self):
+ input_tracker = self.input_tracker
+
+ self.play(FadeInFrom(self.input_words, UP))
+ self.play(
+ FadeInFromLarge(self.input_dot),
+ FadeIn(self.input_decimal),
+ )
+ for value in 1, 0:
+ self.play(
+ input_tracker.set_value, value,
+ run_time=2
+ )
+ self.wait()
+
+ def describe_output(self):
+ path = self.path
+ output_dot = self.output_dot
+ output_decimal = self.output_decimal
+ input_dot = self.input_dot
+ input_tracker = self.input_tracker
+ plane = self.plane
+ real_line = plane.x_axis.copy()
+ real_line.set_stroke(RED, 4)
+ real_words = TextMobject("Real number line")
+ real_words.next_to(ORIGIN, UP)
+ real_words.to_edge(RIGHT)
+
+ traced_path = TracedPath(output_dot.get_center)
+ traced_path.match_style(path)
+
+ self.play(
+ ShowCreation(real_line),
+ FadeInFrom(real_words, DOWN)
+ )
+ self.play(
+ FadeOut(real_line),
+ FadeOut(real_words),
+ )
+ self.play(
+ FadeInFrom(plane.sublabel, UP)
+ )
+ self.play(
+ FadeIn(output_decimal),
+ TransformFromCopy(input_dot, output_dot),
+ )
+
+ kw = {
+ "run_time": 10,
+ "rate_func": lambda t: smooth(t, 1),
+ }
+ self.play(
+ ApplyMethod(input_tracker.set_value, 1, **kw),
+ ShowCreation(path.copy(), remover=True, **kw),
+ )
+ self.add(path)
+ self.add(output_dot)
+ self.wait()
+
+ # Flatten to 1d
+ real_function_word = TextMobject(
+ "Real-valued function"
+ )
+ real_function_word.next_to(ORIGIN, DOWN, MED_LARGE_BUFF)
+ path.generate_target()
+ path.target.stretch(0, 1)
+ path.target.move_to(plane.n2p(0))
+
+ self.play(
+ FadeIn(real_function_word),
+ MoveToTarget(path),
+ )
+ input_tracker.set_value(0)
+ self.play(
+ input_tracker.set_value, 1,
+ **kw
+ )
+
+ #
+ def get_input_line(self, input_rect):
+ input_line = UnitInterval()
+ input_line.move_to(input_rect)
+ input_line.shift(0.25 * UP)
+ input_line.set_width(
+ input_rect.get_width() - 1
+ )
+ input_line.add_numbers(0, 0.5, 1)
+ return input_line
+
+ def get_path(self):
+ # mob = SVGMobject("BatmanLogo")
+ mob = TexMobject("\\pi")
+ path = mob.family_members_with_points()[0]
+ path.set_height(3.5)
+ path.move_to(2 * DOWN, DOWN)
+ path.set_stroke(YELLOW, 2)
+ path.set_fill(opacity=0)
+ return path
+
+
+class GraphForFlattenedPi(ClarifyInputAndOutput):
+ def construct(self):
+ self.setup_plane()
+ plane = self.plane
+ self.remove(plane, plane.label)
+
+ path = self.get_path()
+
+ axes = Axes(
+ x_min=0,
+ x_max=1,
+ x_axis_config={
+ "unit_size": 7,
+ "include_tip": False,
+ "tick_frequency": 0.1,
+ },
+ y_min=-1.5,
+ y_max=1.5,
+ y_axis_config={
+ "include_tip": False,
+ "unit_size": 2,
+ "tick_frequency": 0.5,
+ },
+ )
+ axes.center()
+
+ axes.x_axis.add_numbers(
+ 0.5, 1.0,
+ number_config={"num_decimal_places": 1},
+ )
+ axes.y_axis.add_numbers(
+ -1.0, 1.0,
+ number_config={"num_decimal_places": 1},
+ )
+
+ def func(t):
+ return plane.y_axis.p2n(
+ path.point_from_proportion(t)
+ )
+
+ graph = axes.get_graph(func)
+ graph.set_color(PINK)
+
+ v_line = always_redraw(lambda: Line(
+ axes.x_axis.n2p(axes.x_axis.p2n(graph.get_end())),
+ graph.get_end(),
+ stroke_width=1,
+ ))
+
+ self.add(axes)
+ self.add(v_line)
+
+ kw = {
+ "run_time": 10,
+ "rate_func": lambda t: smooth(t, 1),
+ }
+ self.play(ShowCreation(graph, **kw))
+ self.wait()
+
+
+class SimpleComplexExponentExample(ClarifyInputAndOutput):
+ CONFIG = {
+ "input_space_rect_config": {
+ "width": 14,
+ "height": 1.5,
+ },
+ }
+
+ def construct(self):
+ self.setup_plane()
+ self.setup_input_space()
+ self.setup_input_trackers()
+ self.setup_output_trackers()
+
+ # Testing
+ time = 12
+ self.play(
+ self.input_tracker.set_value, time,
+ run_time=time,
+ rate_func=linear,
+ )
+
+ def setup_plane(self):
+ plane = ComplexPlane()
+ plane.scale(2)
+ plane.add_coordinates()
+ plane.shift(DOWN)
+ self.plane = plane
+ self.add(plane)
+
+ def setup_input_trackers(self):
+ input_line = self.input_line
+ input_tracker = ValueTracker(0)
+ get_input = input_tracker.get_value
+
+ input_tip = ArrowTip(start_angle=-TAU / 4)
+ input_tip.scale(0.5)
+ input_tip.set_color(PINK)
+ f_always(
+ input_tip.move_to,
+ lambda: input_line.n2p(get_input()),
+ lambda: DOWN,
+ )
+
+ input_label = VGroup(
+ TexMobject("t", "="),
+ DecimalNumber(),
+ )
+ input_label[0].set_color_by_tex("t", PINK)
+ input_label.scale(0.7)
+ input_label.add_updater(
+ lambda m: m.arrange(RIGHT, buff=SMALL_BUFF)
+ )
+ input_label.add_updater(
+ lambda m: m[1].set_value(get_input())
+ )
+ input_label.add_updater(
+ lambda m: m.next_to(input_tip, UP, SMALL_BUFF)
+ )
+
+ self.input_tracker = input_tracker
+ self.input_tip = input_tip
+ self.input_label = input_label
+
+ self.add(input_tip, input_label)
+
+ def setup_output_trackers(self):
+ plane = self.plane
+ get_input = self.input_tracker.get_value
+
+ def get_output():
+ return np.exp(complex(0, get_input()))
+
+ def get_output_point():
+ return plane.n2p(get_output())
+
+ output_label, static_output_label = [
+ TexMobject(
+ "e^{i t}" + s,
+ tex_to_color_map={"t": PINK},
+ background_stroke_width=3,
+ )
+ for s in ["", "\\approx"]
+ ]
+ output_label.scale(1.2)
+ output_label.add_updater(
+ lambda m: m.shift(
+ -m.get_bottom() +
+ get_output_point() +
+ rotate_vector(
+ 0.35 * RIGHT,
+ get_input(),
+ )
+ )
+ )
+
+ output_vector = Vector()
+ output_vector.set_opacity(0.75)
+ output_vector.add_updater(
+ lambda m: m.put_start_and_end_on(
+ plane.n2p(0), get_output_point(),
+ )
+ )
+
+ t_max = 40
+ full_output_path = ParametricFunction(
+ lambda t: plane.n2p(np.exp(complex(0, t))),
+ t_min=0,
+ t_max=t_max
+ )
+ output_path = VMobject()
+ output_path.set_stroke(YELLOW, 2)
+ output_path.add_updater(
+ lambda m: m.pointwise_become_partial(
+ full_output_path,
+ 0, get_input() / t_max,
+ )
+ )
+
+ static_output_label.next_to(plane.c2p(1, 1), UR)
+ output_decimal = DecimalNumber(
+ include_sign=True,
+ )
+ output_decimal.scale(0.8)
+ output_decimal.set_stroke(BLACK, 3, background=True)
+ output_decimal.add_updater(
+ lambda m: m.set_value(get_output())
+ )
+ output_decimal.add_updater(
+ lambda m: m.next_to(
+ static_output_label,
+ RIGHT, 2 * SMALL_BUFF,
+ aligned_edge=DOWN,
+ )
+ )
+
+ self.add(output_path)
+ self.add(output_vector)
+ self.add(output_label)
+ self.add(static_output_label)
+ self.add(BackgroundRectangle(output_decimal))
+ self.add(output_decimal)
+
+ #
+ def get_input_line(self, input_rect):
+ input_line = NumberLine(
+ unit_size=0.5,
+ x_min=0,
+ x_max=25,
+ stroke_width=2
+ )
+ input_line.move_to(input_rect)
+ input_line.set_width(
+ input_rect.get_width() - 1.5,
+ stretch=True,
+ )
+ input_line.add_numbers(*range(0, 30, 5))
+ return input_line
diff --git a/active_projects/diffyq/part4/fourier_series_scenes.py b/active_projects/diffyq/part4/fourier_series_scenes.py
index 7d4089a50..6d855e1aa 100644
--- a/active_projects/diffyq/part4/fourier_series_scenes.py
+++ b/active_projects/diffyq/part4/fourier_series_scenes.py
@@ -69,14 +69,19 @@ class ComplexFourierSeriesExample(FourierOfTrebleClef):
new_path.set_height(4)
new_path.move_to(self.path, DOWN)
new_path.shift(0.5 * UP)
+
+ self.transition_to_alt_path(new_path)
+ for n in range(self.n_cycles):
+ self.run_one_cycle()
+
+ def transition_to_alt_path(self, new_path, morph_path=False):
new_coefs = self.get_coefficients_of_path(new_path)
new_vectors = self.get_rotating_vectors(
coefficients=new_coefs
)
new_drawn_path = self.get_drawn_path(new_vectors)
- self.vector_clock.set_value(0)
- self.vector_clock.suspend_updating(0)
+ self.vector_clock.suspend_updating()
vectors = self.vectors
anims = []
@@ -101,18 +106,28 @@ class ComplexFourierSeriesExample(FourierOfTrebleClef):
*v.line.get_start_and_end()
)
)
- anims += [
- FadeOut(self.drawn_path)
- ]
+ if morph_path:
+ anims.append(
+ ReplacementTransform(
+ self.drawn_path,
+ new_drawn_path
+ )
+ )
+ else:
+ anims.append(
+ FadeOut(self.drawn_path)
+ )
self.play(*anims, run_time=3)
- self.vector_clock.resume_updating()
for vect in self.vectors:
vect.remove_updater(vect.updaters[-1])
- self.add(new_drawn_path)
- for n in range(self.n_cycles):
- self.run_one_cycle()
+ if not morph_path:
+ self.add(new_drawn_path)
+ self.vector_clock.set_value(0)
+
+ self.vector_clock.resume_updating()
+ self.drawn_path = new_drawn_path
#
def get_path(self):
@@ -192,9 +207,151 @@ class ComplexFourierSeriesExample(FourierOfTrebleClef):
return labels
-class ComplexFourierSeriesExampleEnd(ExternallyAnimatedScene):
- pass
+class PiFourierSeries(ComplexFourierSeriesExample):
+ CONFIG = {
+ "n_vectors": 101,
+ "max_circle_stroke_width": 1,
+ "top_row_copy_scale_factor": 0.6,
+ }
+
+ def construct(self):
+ self.setup_plane()
+ self.add_vectors_circles_path()
+ self.add_top_row(self.vectors, self.circles)
+
+ for n in range(self.n_cycles):
+ self.run_one_cycle()
+
+ def setup_plane(self):
+ plane = ComplexPlane(
+ axis_config={"unit_size": 2},
+ y_min=-1.25,
+ y_max=1.25,
+ x_min=-2.5,
+ x_max=2.5,
+ background_line_style={
+ "stroke_width": 1,
+ "stroke_color": LIGHT_GREY,
+ },
+ )
+ plane.shift(self.center_point)
+ # plane.fade(0.5)
+ plane.add_coordinates()
+
+ top_rect = Rectangle(
+ width=FRAME_WIDTH,
+ fill_color=BLACK,
+ fill_opacity=1,
+ stroke_width=0,
+ height=2.5
+ )
+ top_rect.to_edge(UP, buff=0)
+
+ self.plane = plane
+ self.add(plane)
+ self.add(top_rect)
+
+ def get_path(self):
+ pi = TexMobject("\\pi")
+ path = pi.family_members_with_points()[0]
+ path.set_height(3.5)
+ path.move_to(3 * DOWN, DOWN)
+ path.set_stroke(YELLOW, 0)
+ path.set_fill(opacity=0)
+ return path
+
+
+class RealValuedFunctionFourierSeries(PiFourierSeries):
+ CONFIG = {
+ "n_vectors": 101,
+ "start_drawn": True,
+ }
+
+ def construct(self):
+ self.setup_plane()
+ self.add_vectors_circles_path()
+ self.add_top_row(self.vectors, self.circles)
+
+ self.flatten_path()
+ self.focus_on_vector_pair()
+
+ def flatten_path(self):
+ new_path = self.path.copy()
+ new_path.stretch(0, 1)
+ new_path.set_y(self.plane.n2p(0)[1])
+ self.vector_clock.set_value(10)
+ self.transition_to_alt_path(new_path, morph_path=True)
+ self.run_one_cycle()
+
+ def focus_on_vector_pair(self):
+ vectors = self.vectors
+ circles = self.circles
+ top_row = self.top_row
+ top_vectors, top_circles, dots, labels = top_row
+
+ rects1, rects2, rects3 = [
+ VGroup(*[
+ SurroundingRectangle(VGroup(
+ top_circles[i],
+ labels[i],
+ ))
+ for i in pair
+ ]).set_stroke(LIGHT_GREY, 2)
+ for pair in [(1, 2), (3, 4), (5, 6)]
+ ]
+
+ def get_opacity_animation(i1, i2, alpha_func):
+ v_group = vectors[i1:i2]
+ c_group = circles[i1:i2]
+ return AnimationGroup(
+ UpdateFromAlphaFunc(
+ VectorizedPoint(),
+ lambda m, a: v_group.set_opacity(
+ alpha_func(a)
+ )
+ ),
+ UpdateFromAlphaFunc(
+ VectorizedPoint(),
+ lambda m, a: c_group.set_stroke(
+ opacity=alpha_func(a)
+ )
+ ),
+ )
+
+ self.remove(self.path)
+ self.play(
+ get_opacity_animation(
+ 3, len(vectors), lambda a: smooth(1 - a),
+ ),
+ ShowCreation(rects1, lag_ratio=0.3),
+ )
+ for n in range(2):
+ self.run_one_cycle()
+
+ self.play(
+ get_opacity_animation(3, 5, smooth),
+ get_opacity_animation(
+ 0, 3,
+ lambda a: 1 - 0.75 * smooth(a)
+ ),
+ ReplacementTransform(rects1, rects2),
+ )
+ self.run_one_cycle()
+ self.play(
+ get_opacity_animation(5, 7, smooth),
+ get_opacity_animation(
+ 3, 5,
+ lambda a: 1 - 0.75 * smooth(a)
+ ),
+ ReplacementTransform(rects2, rects3),
+ )
+ self.run_one_cycle()
+ self.run_one_cycle()
+
+# Pure fourier series with zooming.
+# Note to self, put out a single video with nothing
+# but these?
class FourierSeriesExampleWithRectForZoom(ComplexFourierSeriesExample):
CONFIG = {
diff --git a/active_projects/diffyq/part4/pi_creature_scenes.py b/active_projects/diffyq/part4/pi_creature_scenes.py
index b216f5de7..a064be480 100644
--- a/active_projects/diffyq/part4/pi_creature_scenes.py
+++ b/active_projects/diffyq/part4/pi_creature_scenes.py
@@ -137,3 +137,22 @@ class HangOnThere(TeacherStudentsScene):
"confused", "confused", "angry"
)
self.wait(3)
+
+
+class YouSaidThisWasEasier(TeacherStudentsScene):
+ def construct(self):
+ self.change_all_student_modes(
+ "confused", look_at_arg=self.screen,
+ )
+ self.student_says(
+ "I'm sorry, you said\\\\this was easier?",
+ target_mode="sassy"
+ )
+ self.play(self.teacher.change, "guilty")
+ self.wait(3)
+ self.teacher_says(
+ "Bear with\\\\me",
+ bubble_kwargs={"height": 3, "width": 3},
+ )
+ self.look_at(self.screen)
+ self.wait(3)
diff --git a/active_projects/diffyq/part4/staging.py b/active_projects/diffyq/part4/staging.py
index 06ce90c78..e7e0abbf2 100644
--- a/active_projects/diffyq/part4/staging.py
+++ b/active_projects/diffyq/part4/staging.py
@@ -1,5 +1,6 @@
from manimlib.imports import *
from active_projects.diffyq.part3.staging import FourierSeriesIllustraiton
+from active_projects.diffyq.part2.wordy_scenes import WriteHeatEquationTemplate
class FourierSeriesFormula(Scene):
@@ -687,10 +688,9 @@ class ShowInfiniteSum(FourierSeriesIllustraiton):
self.play(
Transform(brace, new_brace),
value_tracker.set_value, ps,
- run_time=0.2,
)
self.leave_mark(number_line, ps)
- self.wait(0.8)
+ self.wait()
count = 0
num_moving_values = n_braces + 8
@@ -723,7 +723,7 @@ class ShowInfiniteSum(FourierSeriesIllustraiton):
ShowIncreasingSubsets(new_marks),
term_count_tracker.set_value, len(partial_sums),
run_time=10,
- rate_func=linear,
+ rate_func=smooth,
)
self.play(LaggedStartMap(
FadeOut, VGroup(
@@ -947,8 +947,11 @@ class ShowInfiniteSum(FourierSeriesIllustraiton):
for x in range(6):
self.play(get_ps_anim())
self.play(input_tracker.set_value, 0.5)
- for x in range(10):
- self.play(get_ps_anim())
+ for x in range(40):
+ self.play(
+ get_ps_anim(),
+ run_time=0.5,
+ )
#
def get_dot_line(self, dot, axis, line_class=Line):
@@ -1026,3 +1029,85 @@ class ShowInfiniteSum(FourierSeriesIllustraiton):
eq.next_to(brace, LEFT)
return VGroup(eq, brace, values, conditions)
+
+
+class StepFunctionSolutionFormla(WriteHeatEquationTemplate):
+ CONFIG = {
+ "tex_mobject_config": {
+ "tex_to_color_map": {
+ "{1}": WHITE, # BLUE,
+ "{3}": WHITE, # GREEN,
+ "{5}": WHITE, # RED,
+ "{7}": WHITE, # YELLOW,
+ },
+ },
+ }
+
+ def construct(self):
+ formula = TexMobject(
+ "T({x}, {t}) = ",
+ "{4 \\over \\pi}",
+ "\\left(",
+ "{\\cos\\left({1}\\pi {x}\\right) \\over {1}}",
+ "e^{-\\alpha {1}^2 {t} }",
+ "-"
+ "{\\cos\\left({3}\\pi {x}\\right) \\over {3}}",
+ "e^{-\\alpha {3}^2 {t} }",
+ "+",
+ "{\\cos\\left({5}\\pi {x}\\right) \\over {5}}",
+ "e^{-\\alpha {5}^2 {t} }",
+ "- \\cdots",
+ "\\right)",
+ **self.tex_mobject_config,
+ )
+
+ formula.set_width(FRAME_WIDTH - 1)
+ formula.to_edge(UP)
+
+ self.play(FadeInFromDown(formula))
+ self.wait()
+
+
+class AskAboutComplexNotVector(Scene):
+ def construct(self):
+ c_ex = DecimalNumber(
+ complex(2, 1),
+ num_decimal_places=0
+ )
+ i_part = c_ex[-1]
+ v_ex = IntegerMatrix(
+ [[2], [1]],
+ bracket_h_buff=SMALL_BUFF,
+ bracket_v_buff=SMALL_BUFF,
+ )
+ group = VGroup(v_ex, c_ex)
+ group.scale(1.5)
+ # group.arrange(RIGHT, buff=4)
+ group.arrange(DOWN, buff=0.9)
+ group.to_corner(UL, buff=0.2)
+
+ q1, q2 = questions = VGroup(
+ TextMobject("Why not this?").set_color(BLUE),
+ TextMobject("Instead of this?").set_color(YELLOW),
+ )
+ questions.scale(1.5)
+ for q, ex in zip(questions, group):
+ ex.add_background_rectangle()
+ q.add_background_rectangle()
+ q.next_to(ex, RIGHT)
+
+ plane = NumberPlane(axis_config={"unit_size": 2})
+ vect = Vector([4, 2, 0])
+
+ self.play(
+ ShowCreation(plane, lag_ratio=0.1),
+ FadeIn(vect),
+ )
+ for q, ex in zip(questions, group):
+ self.play(
+ FadeInFrom(q, LEFT),
+ FadeIn(ex)
+ )
+ self.wait()
+ self.play(ShowCreationThenFadeAround(i_part))
+ self.wait()
diff --git a/active_projects/diffyq/part4/temperature_scenes.py b/active_projects/diffyq/part4/temperature_scenes.py
index 37027ff79..b032c8036 100644
--- a/active_projects/diffyq/part4/temperature_scenes.py
+++ b/active_projects/diffyq/part4/temperature_scenes.py
@@ -20,6 +20,7 @@ class StepFunctionExample(BringTwoRodsTogether, FourierSeriesIllustraiton):
"include_tip": False,
},
},
+ "y_labels": [-1, 1],
"graph_x_min": 0,
"graph_x_max": 1,
"midpoint": 0.5,
@@ -241,6 +242,117 @@ class StepFunctionExample(BringTwoRodsTogether, FourierSeriesIllustraiton):
)
-class NewSceneName(Scene):
+class BreakDownStepFunction(StepFunctionExample):
+ CONFIG = {
+ "axes_config": {
+ "x_axis_config": {
+ "stroke_width": 2,
+ },
+ "y_axis_config": {
+ "tick_frequency": 0.25,
+ "stroke_width": 2,
+ },
+ "y_min": -1.25,
+ "y_max": 1.25,
+ },
+ "alpha": 0.1,
+ "wait_time": 30,
+ }
+
def construct(self):
- pass
+ self.setup_axes()
+ self.setup_graph()
+ self.setup_clock()
+ self.add_rod()
+
+ self.wait()
+ self.init_updaters()
+ self.play(
+ self.get_clock_anim(self.wait_time)
+ )
+
+ def setup_axes(self):
+ super().setup_axes()
+ axes = self.axes
+ axes.to_edge(LEFT)
+
+ mini_axes = VGroup(*[
+ axes.deepcopy()
+ for x in range(4)
+ ])
+ for mob in mini_axes.get_family():
+ if isinstance(mob, Line):
+ mob.set_stroke(width=1, family=False)
+ mini_axes.arrange(DOWN, buff=2)
+ mini_axes.set_height(FRAME_HEIGHT - 1.5)
+ mini_axes.to_corner(UR)
+ self.scale_factor = fdiv(
+ mini_axes[0].get_width(),
+ axes.get_width(),
+ )
+
+ # mini_axes.arrange(RIGHT, buff=2)
+ # mini_axes.set_width(FRAME_WIDTH - 1.5)
+ # mini_axes.to_edge(LEFT)
+
+ dots = TexMobject("\\vdots")
+ dots.next_to(mini_axes, DOWN)
+ dots.shift_onto_screen()
+
+ self.add(axes)
+ self.add(mini_axes)
+ self.add(dots)
+
+ self.mini_axes = mini_axes
+
+ def setup_graph(self):
+ super().setup_graph()
+ graph = self.graph
+ self.add(graph)
+
+ mini_axes = self.mini_axes
+ mini_graphs = VGroup()
+ for axes, u, n in zip(mini_axes, it.cycle([1, -1]), it.count(1, 2)):
+ mini_graph = axes.get_graph(
+ lambda x: (4 / PI) * (u / n) * np.cos(PI * n * x),
+ )
+ mini_graph.set_stroke(width=2)
+ mini_graphs.add(mini_graph)
+ # mini_graphs.set_color_by_gradient(
+ # BLUE, GREEN, RED, YELLOW,
+ # )
+
+ self.mini_graphs = mini_graphs
+ self.add(mini_graphs)
+
+ def setup_clock(self):
+ super().setup_clock()
+ clock = self.clock
+ time_label = self.time_label
+
+ clock.move_to(3 * RIGHT)
+ clock.to_corner(UP)
+ time_label.next_to(clock, DOWN)
+
+ self.add(clock)
+ self.add(time_label)
+
+ def add_rod(self):
+ self.rod = self.get_rod(0, 1)
+ self.add(self.rod)
+
+ def init_updaters(self):
+ self.graph.add_updater(self.update_graph)
+ for mg in self.mini_graphs:
+ mg.add_updater(
+ lambda m, dt: self.update_graph(
+ m, dt,
+ alpha=self.scale_factor * self.alpha
+ )
+ )
+ self.time_label.add_updater(
+ lambda d, dt: d.increment_value(dt)
+ )
+ self.rod.add_updater(
+ lambda r: self.update_rods([r])
+ )