A community-maintained Python framework for creating mathematical animations.
End of eoc project
Grant Sanderson6bca9952aeb7
diff --git a/eoc/chapter10.py b/eoc/chapter10.pyindex 7affd76da..810d8d43a 100644--- a/eoc/chapter10.py+++ b/eoc/chapter10.py@@ -1121,10 +1121,10 @@ class ConstructQuadraticApproximation(ExampleApproximationWithCos):class ReflectOnQuadraticApproximation(TeacherStudentsScene):def construct(self):self.show_example_approximation()- self.add_polynomial()- self.show_c0()- self.show_c1()- self.show_c2()+ # self.add_polynomial()+ # self.show_c0()+ # self.show_c1()+ # self.show_c2()def show_example_approximation(self):approx_at_x, approx_at_point = [@@ -1138,7 +1138,7 @@ class ReflectOnQuadraticApproximation(TeacherStudentsScene):approx_rhs.next_to(approx_at_point, RIGHT)real_result = TexMobject("\\cos(", "0.1", ")", "=",- "%.7f"%np.cos(0.1)+ "%.7f\\dots"%np.cos(0.1))real_result.shift(approx_rhs.get_part_by_tex("=").get_center() -\diff --git a/eoc/footnote.py b/eoc/footnote.pyindex ec9854986..1ce75af1c 100644--- a/eoc/footnote.py+++ b/eoc/footnote.py@@ -28,6 +28,13 @@ from mobject.svg_mobject import *from mobject.tex_mobject import *from eoc.graph_scene import GraphScene+from eoc.chapter1 import Car, MoveCar+from eoc.chapter10 import derivative++#revert_to_original_skipping_status+++########class Introduce(TeacherStudentsScene):def construct(self):@@ -89,11 +96,15 @@ class SecondDerivativeGraphically(GraphScene):"second_deriv_color" : GREEN,}def construct(self):+ self.force_skipping()+self.setup_axes()self.draw_f()self.show_derivative()self.write_second_derivative()self.show_curvature()++ self.revert_to_original_skipping_status()self.contrast_big_and_small_concavity()def draw_f(self):@@ -258,7 +269,7 @@ class SecondDerivativeGraphically(GraphScene):]arg_rhs_list = [TexMobject("(", str(x0), ")", "=", str(rhs))- for rhs in 5, 0.2, 0+ for rhs in 10, 0.4, 0]for graph, arg_rhs in zip(graphs, arg_rhs_list):graph.ss_group = self.get_secant_slope_group(@@ -317,18 +328,593 @@ class SecondDerivativeGraphically(GraphScene):class IntroduceNotation(TeacherStudentsScene):def construct(self):-+ clunky_deriv = TexMobject(+ "{d", "\\big(", "{df", "\\over", "dx}", "\\big)",+ "\\over", "dx }"+ )+ over_index = clunky_deriv.index_of_part(+ clunky_deriv.get_parts_by_tex("\\over")[1]+ )+ numerator = VGroup(*clunky_deriv[:over_index])+ denominator = VGroup(*clunky_deriv[over_index+1:])+++ rp = clunky_deriv.get_part_by_tex("(")+ lp = clunky_deriv.get_part_by_tex(")")+ dfs, overs, dxs = map(clunky_deriv.get_parts_by_tex, [+ "df", "over", "dx"+ ])+ df_over_dx = VGroup(dfs[0], overs[0], dxs[0])+ d = clunky_deriv.get_part_by_tex("d")+ d_over_dx = VGroup(d, overs[1], dxs[1])++ d2f_over_dx2 = TexMobject("{d^2 f", "\\over", "dx", "^2}")+ d2f_over_dx2.highlight_by_tex("dx", YELLOW)++ for mob in clunky_deriv, d2f_over_dx2:+ mob.next_to(self.teacher, UP+LEFT)++ for mob in numerator, denominator:+ circle = Circle(color = YELLOW)+ circle.replace(mob, stretch = True)+ circle.scale_in_place(1.3)+ mob.circle = circle+ dx_to_zero = TexMobject("dx \\to 0")+ dx_to_zero.highlight(YELLOW)+ dx_to_zero.next_to(clunky_deriv, UP+LEFT)self.student_says(- "What's that notation?"+ "What's that notation?",+ target_mode = "raise_left_hand"+ )+ self.change_student_modes("confused", "raise_left_hand", "confused")+ self.play(+ FadeIn(+ clunky_deriv,+ run_time = 2,+ submobject_mode = "lagged_start"+ ),+ RemovePiCreatureBubble(self.get_students()[1]),+ self.teacher.change_mode, "raise_right_hand"+ )+ self.dither()+ self.play(ShowCreation(numerator.circle))+ self.dither()+ self.play(ReplacementTransform(+ numerator.circle,+ denominator.circle,+ ))+ self.dither()+ self.play(+ FadeOut(denominator.circle),+ Write(dx_to_zero),+ dxs.highlight, YELLOW+ )+ self.dither()+ self.play(+ FadeOut(dx_to_zero),+ *[ApplyMethod(pi.change, "plain") for pi in self.get_pi_creatures()]+ )+ self.play(+ df_over_dx.scale, dxs[1].get_height()/dxs[0].get_height(),+ df_over_dx.move_to, d_over_dx, RIGHT,+ FadeOut(VGroup(lp, rp)),+ d_over_dx.shift, 0.8*LEFT + 0.05*UP,+ )+ self.dither()+ self.play(*[+ ReplacementTransform(+ group,+ VGroup(d2f_over_dx2.get_part_by_tex(tex))+ )+ for group, tex in [+ (VGroup(d, dfs[0]), "d^2"),+ (overs, "over"),+ (dxs, "dx"),+ (VGroup(dxs[1].copy()), "^2}"),+ ]+ ])+ self.dither(2)+ self.student_says(+ "How does one... \\\\ read that?",+ student_index = 0,+ )+ self.play(self.teacher.change, "happy")+ self.dither(2)++class HowToReadNotation(GraphScene, ReconfigurableScene):+ CONFIG = {+ "x_max" : 5,+ "dx" : 0.4,+ "x" : 2,+ "graph_origin" : 2.5*DOWN + 5*LEFT,+ }+ def setup(self):+ for base in self.__class__.__bases__:+ base.setup(self)++ def construct(self):+ self.force_skipping()++ self.add_graph()+ self.take_two_steps()+ self.change_step_size()+ self.show_dfs()+ self.show_ddf()++ self.revert_to_original_skipping_status()+ self.show_proportionality_to_dx_squared()+ return++ self.write_second_derivative()++ def add_graph(self):+ self.setup_axes()+ graph = self.get_graph(lambda x : x**2)+ graph_label = self.get_graph_label(+ graph, "f(x)",+ direction = LEFT,+ x_val = 3.3)- self.change_student_modes("confused", None, "confused")+ self.add(graph, graph_label)+ self.graph = graph+ def take_two_steps(self):+ v_lines = [+ self.get_vertical_line_to_graph(+ self.x + i*self.dx, self.graph,+ line_class = DashedLine,+ color = WHITE+ )+ for i in range(3)+ ]+ braces = [+ Brace(VGroup(*v_lines[i:i+2]), buff = 0)+ for i in range(2)+ ]+ for brace in braces:+ brace.dx = TexMobject("dx")+ max_width = 0.7*brace.get_width()+ if brace.dx.get_width() > max_width:+ brace.dx.scale_to_fit_width(max_width)+ brace.dx.next_to(brace, DOWN, SMALL_BUFF)++ self.play(ShowCreation(v_lines[0]))+ self.dither()+ for brace, line in zip(braces, v_lines[1:]):+ self.play(+ ReplacementTransform(+ VectorizedPoint(brace.get_corner(UP+LEFT)),+ brace,+ ),+ Write(brace.dx, run_time = 1),+ )+ self.play(ShowCreation(line))+ self.dither()++ self.v_lines = v_lines+ self.braces = braces++ def change_step_size(self):+ self.transition_to_alt_config(dx = 0.6)+ self.transition_to_alt_config(dx = 0.01, run_time = 3)++ def show_dfs(self):+ dx_lines = VGroup()+ df_lines = VGroup()+ df_dx_groups = VGroup()+ df_labels = VGroup()+ for i, v_line1, v_line2 in zip(it.count(1), self.v_lines, self.v_lines[1:]):+ dx_line = Line(+ v_line1.get_bottom(),+ v_line2.get_bottom(),+ color = GREEN+ )+ dx_line.move_to(v_line1.get_top(), LEFT)+ dx_lines.add(dx_line)+ df_line = Line(+ dx_line.get_right(),+ v_line2.get_top(),+ color = YELLOW+ )+ df_lines.add(df_line)+ df_label = TexMobject("df_%d"%i)+ df_label.highlight(YELLOW)+ df_label.scale(0.8)+ df_label.next_to(df_line.get_center(), UP+LEFT, MED_LARGE_BUFF)+ df_arrow = Arrow(+ df_label.get_bottom(),+ df_line.get_center(),+ buff = SMALL_BUFF,+ )+ df_line.label = df_label+ df_line.arrow = df_arrow+ df_labels.add(df_label)+ df_dx_groups.add(VGroup(df_line, dx_line))++ for brace, dx_line, df_line in zip(self.braces, dx_lines, df_lines):+ self.play(+ VGroup(brace, brace.dx).next_to,+ dx_line, DOWN, SMALL_BUFF,+ FadeIn(dx_line),+ )+ self.play(ShowCreation(df_line))+ self.play(+ ShowCreation(df_line.arrow),+ Write(df_line.label)+ )+ self.dither(2)++ self.df_dx_groups = df_dx_groups+ self.df_labels = df_labels++ def show_ddf(self):+ df_dx_groups = self.df_dx_groups.copy()+ df_dx_groups.generate_target()+ df_dx_groups.target.arrange_submobjects(+ RIGHT,+ buff = MED_LARGE_BUFF,+ aligned_edge = DOWN+ )+ df_dx_groups.target.next_to(+ self.df_dx_groups, RIGHT,+ buff = 3,+ aligned_edge = DOWN+ )++ df_labels = self.df_labels.copy()+ df_labels.generate_target()+ h_lines = VGroup()+ for group, label in zip(df_dx_groups.target, df_labels.target):+ label.next_to(group.get_right(), LEFT, SMALL_BUFF)+ width = df_dx_groups.target.get_width() + MED_SMALL_BUFF+ h_line = DashedLine(ORIGIN, width*RIGHT)+ h_line.move_to(+ group.get_corner(UP+RIGHT)[1]*UP + \+ df_dx_groups.target.get_right()[0]*RIGHT,+ RIGHT+ )+ h_lines.add(h_line)+ max_height = 0.8*group.get_height()+ if label.get_height() > max_height:+ label.scale_to_fit_height(max_height)+++ ddf_brace = Brace(h_lines, LEFT, buff = SMALL_BUFF)+ ddf = ddf_brace.get_tex("d(df)", buff = SMALL_BUFF)+ ddf.scale(+ df_labels[0].get_height()/ddf.get_height(),+ about_point = ddf.get_right()+ )+ ddf.highlight(MAROON_B)++ self.play(+ *map(MoveToTarget, [df_dx_groups, df_labels]),+ run_time = 2+ )+ self.play(ShowCreation(h_lines, run_time = 2))+ self.play(GrowFromCenter(ddf_brace))+ self.play(Write(ddf))+ self.dither(2)++ self.ddf = ddf++ def show_proportionality_to_dx_squared(self):+ ddf = self.ddf.copy()+ ddf.generate_target()+ ddf.target.next_to(self.ddf, UP, LARGE_BUFF)+ rhs = TexMobject(+ "\\approx", "(\\text{Some constant})", "(dx)^2"+ )+ rhs.scale(0.8)+ rhs.next_to(ddf.target, RIGHT)++ example_dx = TexMobject(+ "dx = 0.01 \\Rightarrow (dx)^2 = 0.0001"+ )+ example_dx.scale(0.8)+ example_dx.to_corner(UP+RIGHT)++ self.play(MoveToTarget(ddf))+ self.play(Write(rhs))+ self.dither()+ self.play(Write(example_dx))+ self.dither(2)+ self.play(FadeOut(example_dx))++ self.ddf = ddf+ self.dx_squared = rhs.get_part_by_tex("dx")++ def write_second_derivative(self):+ ddf_over_dx_squared = TexMobject(+ "{d(df)", "\\over", "(dx)^2}"+ )+ ddf_over_dx_squared.scale(0.8)+ ddf_over_dx_squared.move_to(self.ddf, RIGHT)+ ddf_over_dx_squared.highlight_by_tex("df", self.ddf.get_color())+ parens = VGroup(+ ddf_over_dx_squared[0][1],+ ddf_over_dx_squared[0][4],+ ddf_over_dx_squared[2][0],+ ddf_over_dx_squared[2][3],+ )++ right_shifter = ddf_over_dx_squared[0][0]+ left_shifter = ddf_over_dx_squared[2][4]++ exp_two = TexMobject("2")+ exp_two.highlight(self.ddf.get_color())+ exp_two.scale(0.5)+ exp_two.move_to(right_shifter.get_corner(UP+RIGHT), LEFT)+ exp_two.shift(MED_SMALL_BUFF*RIGHT)+ pre_exp_two = VGroup(ddf_over_dx_squared[0][2])++ self.play(+ Write(ddf_over_dx_squared.get_part_by_tex("over")),+ *[+ ReplacementTransform(+ mob,+ ddf_over_dx_squared.get_part_by_tex(tex),+ path_arc = -np.pi/2,+ )+ for mob, tex in (self.ddf, "df"), (self.dx_squared, "dx")+ ]+ )+ self.dither(2)+ self.play(FadeOut(parens))+ self.play(+ left_shifter.shift, 0.2*LEFT,+ right_shifter.shift, 0.2*RIGHT,+ ReplacementTransform(pre_exp_two, exp_two),+ ddf_over_dx_squared.get_part_by_tex("over").scale_in_place, 0.8+ )+ self.dither(2)++class Footnote(Scene):+ def construct(self):+ self.add(TextMobject("""+ Interestingly, there is a notion in math+ called the ``exterior derivative'' which+ treats this ``d'' as having a more independent+ meaning, though it's less related to the+ intuitions I've introduced in this series.+ """, alignment = ""))++class TrajectoryGraphScene(GraphScene):+ CONFIG = {+ "x_min" : 0,+ "x_max" : 10,+ "x_axis_label" : "t",+ "y_axis_label" : "s",+ # "func" : lambda x : 10*smooth(x/10.0),+ "func" : lambda t : 10*bezier([0, 0, 0, 1, 1, 1])(t/10.0),+ "color" : BLUE,+ }+ def construct(self):+ self.setup_axes()+ self.graph = self.get_graph(+ self.func,+ color = self.color+ )+ self.add(self.graph)++class SecondDerivativeAsAcceleration(Scene):+ CONFIG = {+ "car_run_time" : 6,+ }+ def construct(self):+ self.init_car_and_line()+ self.introduce_acceleration()+ self.show_functions()++ def init_car_and_line(self):+ line = Line(5.5*LEFT, 4.5*RIGHT)+ line.shift(2*DOWN)+ car = Car()+ car.move_to(line.get_left())+ self.add(line, car)+ self.car = car+ self.start_car_copy = car.copy()+ self.line = line++ def introduce_acceleration(self):+ a_words = TexMobject(+ "{d^2 s \\over dt^2}(t)", "\\Leftrightarrow",+ "\\text{Acceleration}"+ )+ a_words.highlight_by_tex("d^2 s", MAROON_B)+ a_words.highlight_by_tex("Acceleration", YELLOW)+ a_words.to_corner(UP+RIGHT )+ self.add(a_words)+ self.show_car_movement()+ self.dither()++ self.a_words = a_words++ def show_functions(self):+ def get_deriv(n):+ return lambda x : derivative(+ s_scene.graph.underlying_function, x, n+ )+ s_scene = TrajectoryGraphScene()+ v_scene = TrajectoryGraphScene(+ func = get_deriv(1),+ color = GREEN,+ y_max = 4,+ y_axis_label = "v",+ )+ a_scene = TrajectoryGraphScene(+ func = get_deriv(2),+ color = MAROON_B,+ y_axis_label = "a",+ y_min = -2,+ y_max = 2,+ )+ j_scene = TrajectoryGraphScene(+ func = get_deriv(3),+ color = PINK,+ y_axis_label = "j",+ y_min = -2,+ y_max = 2,+ )+ s_graph, v_graph, a_graph, j_graph = graphs = [+ VGroup(*scene.get_top_level_mobjects())+ for scene in s_scene, v_scene, a_scene, j_scene+ ]+ for i, graph in enumerate(graphs):+ graph.scale_to_fit_height(SPACE_HEIGHT)+ graph.to_corner(UP+LEFT)+ graph.shift(i*DOWN/2.0)++ s_words = TexMobject(+ "s(t)", "\\Leftrightarrow", "\\text{Displacement}"+ )+ s_words.highlight_by_tex("s(t)", s_scene.graph.get_color())+ v_words = TexMobject(+ "\\frac{ds}{dt}(t)", "\\Leftrightarrow",+ "\\text{Velocity}"+ )+ v_words.highlight_by_tex("ds", v_scene.graph.get_color())+ j_words = TexMobject(+ "\\frac{d^3 s}{dt^3}(t)", "\\Leftrightarrow",+ "\\text{Jerk}"+ )+ j_words.highlight_by_tex("d^3", j_scene.graph.get_color())+ self.a_words.generate_target()+ words_group = VGroup(s_words, v_words, self.a_words.target, j_words)+ words_group.arrange_submobjects(+ DOWN,+ buff = MED_LARGE_BUFF,+ aligned_edge = LEFT+ )+ words_group.to_corner(UP+RIGHT)+ j_graph.scale(0.3).next_to(j_words, LEFT)++ positive_rect = Rectangle()+ positive_rect.set_stroke(width = 0)+ positive_rect.set_fill(GREEN, 0.5)+ positive_rect.replace(+ Line(+ a_scene.coords_to_point(0, -1),+ a_scene.coords_to_point(5, 1),+ ),+ stretch = True+ )+ negative_rect = Rectangle()+ negative_rect.set_stroke(width = 0)+ negative_rect.set_fill(RED, 0.5)+ negative_rect.replace(+ Line(+ a_scene.coords_to_point(5, 1),+ a_scene.coords_to_point(10, -1),+ ),+ stretch = True+ )++ self.show_car_movement(+ MoveToTarget(self.a_words),+ FadeIn(s_words),+ FadeIn(s_graph),+ )+ self.play(+ s_graph.scale, 0.3,+ s_graph.next_to, s_words, LEFT+ )+ self.play(*map(FadeIn, [v_graph, v_words]) )+ self.dither(2)+ self.play(+ v_graph.scale, 0.3,+ v_graph.next_to, v_words, LEFT+ )+ self.dither(2)+ self.play(+ Indicate(self.a_words),+ FadeIn(a_graph),+ )+ self.dither()+ self.play(FadeIn(positive_rect))+ for x in range(2):+ self.show_car_movement(+ run_time = 3,+ rate_func = lambda t : smooth(t/2.0)+ )+ self.dither()+ self.play(FadeIn(negative_rect))+ self.dither()+ self.play(MoveCar(+ self.car, self.line.get_end(),+ run_time = 3,+ rate_func = lambda t : 2*smooth((t+1)/2.0) - 1+ ))+ self.dither()+ self.play(+ a_graph.scale, 0.3,+ a_graph.next_to, self.a_words, LEFT,+ *map(FadeOut, [positive_rect, negative_rect])+ )+ self.play(+ FadeOut(self.car),+ FadeIn(j_words),+ FadeIn(j_graph),+ self.line.scale, 0.5, self.line.get_left(),+ self.line.shift, LEFT,+ )+ self.car.scale(0.5)+ self.car.move_to(self.line.get_start())+ self.play(FadeIn(self.car))+ self.show_car_movement()+ self.dither(2)++ ##########++ def show_car_movement(self, *added_anims, **kwargs):+ distance = np.linalg.norm(+ self.car.get_center() - self.start_car_copy.get_center()+ )+ if distance > 1:+ self.play(FadeOut(self.car))+ self.car.move_to(self.line.get_left())+ self.play(FadeIn(self.car))++ kwargs["run_time"] = kwargs.get("run_time", self.car_run_time)+ self.play(+ MoveCar(self.car, self.line.get_right(), **kwargs),+ *added_anims+ )++class NextVideo(Scene):+ def construct(self):+ title = TextMobject("Chapter 10: Taylor series")+ title.to_edge(UP)+ rect = ScreenRectangle(height = 6)+ rect.next_to(title, DOWN)+ self.add(rect)+ self.play(Write(title))+ self.dither()++class Thumbnail(SecondDerivativeGraphically):+ CONFIG = {+ "graph_origin" : 5*LEFT + 3*DOWN,+ "x_axis_label" : "",+ "y_axis_label" : "",+ }+ def construct(self):+ self.setup_axes()+ self.force_skipping()+ self.draw_f()+ self.remove(self.graph_label)+ self.graph.set_stroke(GREEN, width = 8)+ tex = TexMobject("{d^n f", "\\over", "dx^n}")+ tex.highlight_by_tex("d^n", YELLOW)+ tex.highlight_by_tex("dx", BLUE)+ tex.scale_to_fit_height(4)+ tex.to_edge(UP)+ self.add(tex)diff --git a/mobject/tex_mobject.py b/mobject/tex_mobject.pyindex ba107cfb8..a69c96cf7 100644--- a/mobject/tex_mobject.py+++ b/mobject/tex_mobject.py@@ -143,6 +143,16 @@ class TexMobject(SVGMobject):part.highlight(color)return self+ def index_of_part(self, part):+ split_self = self.split()+ if part not in split_self:+ raise Exception("Trying to get index of part not in TexMobject")+ return self.split().index(part)++ def index_of_part_by_tex(self, tex, **kwargs):+ part = self.get_part_by_tex(tex, **kwargs)+ return self.index_of_part(part)+def organize_submobjects_left_to_right(self):self.submobjects.sort(lambda m1, m2 : int((m1.get_left()-m2.get_left())[0])@@ -213,11 +223,15 @@ class Brace(TexMobject):self.put_at_tip(text_mob, **kwargs)return text_mob+ def get_tex(self, *tex, **kwargs):+ tex_mob = TexMobject(*tex)+ self.put_at_tip(tex_mob, **kwargs)+ return tex_mob+def get_tip(self):# Very specific to the LaTeX representation# of a brace, but it's the only way I can think# of to get the tip regardless of orientation.- # return self.submobjects[2].get_anchors()[7]return self.get_all_points()[self.tip_point_index]def get_direction(self):diff --git a/topics/geometry.py b/topics/geometry.pyindex 177050b41..49a7b0727 100644--- a/topics/geometry.py+++ b/topics/geometry.py@@ -205,6 +205,19 @@ class Arrow(Line):self.add_tip()def add_tip(self, add_at_end = True):+ tip = VMobject(+ close_new_points = True,+ mark_paths_closed = True,+ fill_color = self.color,+ fill_opacity = 1,+ stroke_color = self.color,+ )+ self.set_tip_points(tip, add_at_end)+ self.tip = tip+ self.add(self.tip)+ self.init_colors()++ def set_tip_points(self, tip, add_at_end = True):start, end = self.get_start_and_end()anchors = self.get_anchors()vect = anchors[-1] - anchors[-2]@@ -216,19 +229,11 @@ class Arrow(Line):end+rotate_vector(vect, u*self.tip_angle)for u in 1, -1]- self.tip = VMobject(- close_new_points = True,- mark_paths_closed = True,- fill_color = self.color,- fill_opacity = 1,- stroke_color = self.color,- )- self.tip.set_anchor_points(+ tip.set_anchor_points([tip_points[0], end, tip_points[1]],mode = "corners")- self.add(self.tip)- self.init_colors()+ return selfdef get_end(self):if hasattr(self, "tip"):@@ -241,9 +246,8 @@ class Arrow(Line):def scale(self, scale_factor, **kwargs):Line.scale(self, scale_factor, **kwargs)- if self.preserve_tip_size_when_scaling:- self.remove(self.tip)- self.add_tip()+ if self.preserve_tip_size_when_scaling and self.get_length() > self.tip_length:+ self.set_tip_points(self.tip)return selfclass Vector(Arrow):