diff --git a/active_projects/wallis_g.py b/active_projects/wallis_g.py index 2af8f406..493aa117 100644 --- a/active_projects/wallis_g.py +++ b/active_projects/wallis_g.py @@ -26,15 +26,30 @@ def get_chord_f_label(chord, arg="f", direction=DOWN): return chord_f +class WallisNumeratorDenominatorGenerator(object): + def __init__(self): + self.n = 0 + + def __iter__(self): + return self + + def __next__(self): + return self.next() + + def next(self): + n = self.n + self.n += 1 + if n % 2 == 0: + return (n + 2, n + 1) + else: + return (n + 1, n + 2) + + def get_wallis_product(n_terms=6, show_result=True): tex_mob_args = [] + nd_generator = WallisNumeratorDenominatorGenerator() for x in range(n_terms): - if x % 2 == 0: - numerator = x + 2 - denominator = x + 1 - else: - numerator = x + 1 - denominator = x + 2 + numerator, denominator = nd_generator.next() tex_mob_args += [ "{%d" % numerator, "\\over", "%d}" % denominator, "\\cdot" ] @@ -45,9 +60,346 @@ def get_wallis_product(n_terms=6, show_result=True): result = TexMobject(*tex_mob_args) return result + +def get_wallis_product_numerical_terms(n_terms=20): + result = [] + nd_generator = WallisNumeratorDenominatorGenerator() + for x in range(n_terms): + n, d = nd_generator.next() + result.append(float(n) / d) + return result + + # Scenes +class Introduction(Scene): + def construct(self): + n_terms = 10 + + number_line = NumberLine( + x_min=0, + x_max=2, + unit_size=5, + tick_frequency=0.25, + numbers_with_elongated_ticks=[0, 1, 2], + color=LIGHT_GREY, + ) + number_line.add_numbers() + number_line.move_to(DOWN) + + numerical_terms = get_wallis_product_numerical_terms(400) + partial_products = np.cumprod(numerical_terms) + curr_product = partial_products[0] + + arrow = Vector(DOWN, color=YELLOW) + + def get_arrow_update(): + return ApplyFunction( + lambda mob: mob.next_to( + number_line.number_to_point(curr_product), + UP, SMALL_BUFF + ), + arrow, + ) + get_arrow_update().update(1) + decimal = DecimalNumber(curr_product, num_decimal_points=5, show_ellipsis=True) + decimal.next_to(arrow, UP, SMALL_BUFF, submobject_to_align=decimal[:5]) + decimal_anim = ChangingDecimal( + decimal, + lambda a: number_line.point_to_number(arrow.get_center()), + tracked_mobject=arrow + ) + + product_mob = get_wallis_product(n_terms) + product_mob.to_edge(UP) + + rects = VGroup(*[ + SurroundingRectangle(product_mob[:n]) + for n in range(3, 4 * n_terms, 4) + [4 * n_terms] + ]) + rect = rects[0].copy() + + pi_halves_arrow = Vector(UP, color=BLUE) + pi_halves_arrow.next_to( + number_line.number_to_point(np.pi / 2), DOWN, SMALL_BUFF + ) + pi_halves_term = TexMobject("\\pi / 2") + pi_halves_term.next_to(pi_halves_arrow, DOWN) + + self.add(product_mob, number_line, rect, arrow, decimal) + self.add(pi_halves_arrow, pi_halves_term) + for n in range(1, len(rects)): + curr_product = partial_products[n] + self.play( + get_arrow_update(), + decimal_anim, + Transform(rect, rects[n]), + run_time=0.5 + ) + self.wait(0.5) + + for n in range(len(rects), len(numerical_terms), 31): + curr_product = partial_products[n] + self.play( + get_arrow_update(), + decimal_anim, + run_time=0.25 + ) + curr_product = np.pi / 2 + self.play( + get_arrow_update(), + decimal_anim, + run_time=0.5 + ) + self.wait() + + +class SourcesOfOriginality(TeacherStudentsScene): + def construct(self): + self.mention_excitement() + self.break_down_value_of_math_presentations() + self.where_we_fit_in() + + def mention_excitement(self): + self.teacher_says( + "This one came about \\\\ a bit differently...", + target_mode="speaking", + run_time=1 + ) + self.change_student_modes("happy", "confused", "erm") + self.wait(2) + + def break_down_value_of_math_presentations(self): + title = TextMobject("The value of a", "math", "presentation") + title.to_edge(UP, buff=MED_SMALL_BUFF) + value_of, math, presentation = title + + MATH_COLOR = YELLOW + COMMUNICATION_COLOR = BLUE + + big_rect = self.big_rect = Rectangle( + width=title.get_width() + 2 * MED_LARGE_BUFF, + height=3.5, + color=WHITE + ) + big_rect.next_to(title, DOWN) + + left_rect, right_rect = self.left_rect, self.right_rect = [ + Rectangle( + height=big_rect.get_height() - 2 * SMALL_BUFF, + width=0.5 * big_rect.get_width() - 2 * SMALL_BUFF, + color=color + ) + for color in MATH_COLOR, COMMUNICATION_COLOR + ] + right_rect.flip() + left_rect.next_to(big_rect.get_left(), RIGHT, SMALL_BUFF) + right_rect.next_to(big_rect.get_right(), LEFT, SMALL_BUFF) + + underlying_math = TextMobject("Underlying", "math") + underlying_math.set_color(MATH_COLOR) + communication = TextMobject("Communication") + communication.set_color(COMMUNICATION_COLOR) + VGroup(underlying_math, communication).scale(0.75) + underlying_math.next_to(left_rect.get_top(), DOWN, SMALL_BUFF) + communication.next_to(right_rect.get_top(), DOWN, SMALL_BUFF) + + formula = TexMobject( + "\\sum_{n = 1}^\\infty \\frac{1}{n^2} = \\frac{\\pi^2}{2}", + ) + formula.scale(0.75) + formula.next_to(underlying_math, DOWN) + + based_on_wastlund = TextMobject( + "Previous video based on\\\\", + "a paper by Johan W\\\"{a}stlund" + ) + based_on_wastlund.scale_to_fit_width(left_rect.get_width() - MED_SMALL_BUFF) + based_on_wastlund.next_to(formula, DOWN, MED_LARGE_BUFF) + + communication_parts = TextMobject("Visuals, narrative, etc.") + communication_parts.scale(0.75) + communication_parts.next_to(communication, DOWN, MED_LARGE_BUFF) + lighthouse = Lighthouse(height=0.5) + lighthouse.next_to(communication_parts, DOWN, LARGE_BUFF) + ambient_light = AmbientLight( + num_levels=200, + radius=5, + opacity_function=DEFAULT_OPACITY_FUNCTION, + ) + ambient_light.move_source_to(lighthouse.get_top()) + + big_rect.save_state() + big_rect.stretch(0, 1) + big_rect.stretch(0.5, 0) + big_rect.move_to(title) + + self.play( + FadeInFromDown(title), + RemovePiCreatureBubble( + self.teacher, + target_mode="raise_right_hand", + look_at_arg=title, + ), + self.get_student_changes( + *["pondering"] * 3, + look_at_arg=title + ) + ) + self.play(big_rect.restore) + self.play(*map(ShowCreation, [left_rect, right_rect])) + self.wait() + self.play( + math.match_color, left_rect, + ReplacementTransform(VGroup(math.copy()), underlying_math) + ) + self.play(FadeIn(formula)) + self.play( + presentation.match_color, right_rect, + ReplacementTransform(presentation.copy(), communication) + ) + self.play( + FadeIn(communication_parts), + FadeIn(lighthouse), + SwitchOn(ambient_light) + ) + self.play(self.teacher.change, "tease") + self.wait() + + self.play( + FadeIn(based_on_wastlund), + self.get_student_changes( + "sassy", "erm", "plain", + look_at_arg=based_on_wastlund + ), + ) + self.wait() + + self.math_content = VGroup(formula, based_on_wastlund) + + def where_we_fit_in(self): + right_rect = self.right_rect + left_rect = self.left_rect + + points = [ + right_rect.get_left() + SMALL_BUFF * RIGHT, + right_rect.get_corner(UL), + right_rect.get_corner(UR), + right_rect.get_right() + SMALL_BUFF * LEFT, + right_rect.get_corner(DR), + right_rect.get_bottom() + SMALL_BUFF * UP, + right_rect.get_corner(DL), + ] + added_points = [ + left_rect.get_bottom(), + left_rect.get_corner(DL), + left_rect.get_corner(DL) + 1.25 * UP, + left_rect.get_bottom() + 1.25 * UP, + ] + + blob1, blob2 = VMobject(), VMobject() + blob1.set_points_smoothly(points + [points[0]]) + blob1.add_control_points(3 * len(added_points) * [points[0]]) + blob2.set_points_smoothly(points + added_points + [points[0]]) + for blob in blob1, blob2: + blob.set_stroke(width=0) + blob.set_fill(BLUE, opacity=0.5) + + our_contribution = TextMobject("Our target \\\\ contribution") + our_contribution.scale(0.75) + our_contribution.to_corner(UR) + arrow = Arrow( + our_contribution.get_bottom(), + right_rect.get_right() + MED_LARGE_BUFF * LEFT, + color=BLUE + ) + + wallis_product = get_wallis_product(n_terms=4) + wallis_product.scale_to_fit_width(left_rect.get_width() - 2 * MED_LARGE_BUFF) + wallis_product.move_to(self.math_content, UP) + wallis_product_name = TextMobject("``Wallis product''") + wallis_product_name.scale(0.75) + wallis_product_name.next_to(wallis_product, DOWN, MED_SMALL_BUFF) + + new_proof = TextMobject("New proof") + new_proof.next_to(wallis_product_name, DOWN, MED_LARGE_BUFF) + + self.play( + DrawBorderThenFill(blob1), + Write(our_contribution), + GrowArrow(arrow), + ) + self.wait(2) + self.play(FadeOut(self.math_content)) + self.play( + FadeIn(wallis_product), + Write(wallis_product_name, run_time=1) + ) + self.wait(2) + self.play( + Transform(blob1, blob2, path_arc=-90 * DEGREES), + FadeIn(new_proof), + self.teacher.change, "hooray", + ) + self.change_all_student_modes("hooray", look_at_arg=new_proof) + self.wait(5) + + +class SridharWatchingScene(PiCreatureScene): + CONFIG = { + "default_pi_creature_kwargs": { + "color": YELLOW_E, + "flip_at_start": False, + }, + } + + def construct(self): + laptop = Laptop() + laptop.scale(1.8) + laptop.to_corner(DR) + sridhar = self.pi_creature + sridhar.next_to(laptop, LEFT, SMALL_BUFF, DOWN) + bubble = ThoughtBubble() + bubble.flip() + bubble.pin_to(sridhar) + + basel = TexMobject( + "{1", "\\over", "1^2}", "+" + "{1", "\\over", "2^2}", "+" + "{1", "\\over", "3^2}", "+", "\\cdots", + "= \\frac{\\pi^2}{6}" + ) + wallis = get_wallis_product(n_terms=4) + VGroup(basel, wallis).scale(0.7) + basel.move_to(bubble.get_bubble_center()) + basel.to_edge(UP, buff=MED_SMALL_BUFF) + wallis.next_to(basel, DOWN, buff=0.75) + arrow = TexMobject("\\updownarrow") + arrow.move_to(VGroup(basel, wallis)) + basel.set_color(YELLOW) + wallis.set_color(BLUE) + + self.play(LaggedStart(DrawBorderThenFill, laptop)) + self.play(sridhar.change, "pondering", laptop.screen) + self.wait() + self.play(ShowCreation(bubble)) + self.play(LaggedStart(FadeIn, basel)) + self.play( + ReplacementTransform(basel.copy(), wallis), + GrowFromPoint(arrow, arrow.get_top()) + ) + self.wait(4) + self.play(sridhar.change, "thinking", wallis) + self.wait(4) + self.play(LaggedStart( + ApplyFunction, + VGroup(*list(laptop) + [bubble, basel, arrow, wallis, sridhar]), + lambda mob: (lambda m: m.set_color(BLACK).fade(1).scale(0.8), mob), + run_time=3, + )) + + class DistanceProductScene(MovingCameraScene): CONFIG = { "ambient_light_config": { @@ -73,6 +425,7 @@ class DistanceProductScene(MovingCameraScene): "numeric_distance_label_height": 0.25, "default_product_column_top": FRAME_WIDTH * RIGHT / 4 + 1.5 * UP, "include_lighthouses": True, + "include_distance_labels_background_rectangle": True, } def setup(self): @@ -168,7 +521,7 @@ class DistanceProductScene(MovingCameraScene): line.get_length() / radius, num_decimal_points=num_decimal_points, show_ellipsis=show_ellipsis, - include_background_rectangle=True, + include_background_rectangle=self.include_distance_labels_background_rectangle, ) label.scale_to_fit_height(self.numeric_distance_label_height) max_width = 0.5 * max(line.get_length(), 0.1) @@ -207,7 +560,7 @@ class DistanceProductScene(MovingCameraScene): self.get_distance_product(fraction), num_decimal_points=3, show_ellipsis=True, - include_background_rectangle=True, + include_background_rectangle=self.include_distance_labels_background_rectangle, ) product_decimal.scale_to_fit_height(self.numeric_distance_label_height) product_decimal.next_to(h_line, DOWN) @@ -299,17 +652,14 @@ class DistanceProductScene(MovingCameraScene): class IntroduceDistanceProduct(DistanceProductScene): CONFIG = { - "ambient_light_config": { - # "num_levels": 10, - # "radius": 1, - "color": YELLOW, - }, + "ambient_light_config": {"color": YELLOW}, } def construct(self): self.draw_circle_with_points() self.turn_into_lighthouses_and_observer() self.show_sum_of_inverse_squares() + self.transition_to_lemma_1() def draw_circle_with_points(self): circle = self.circle @@ -528,15 +878,44 @@ class IntroduceDistanceProduct(DistanceProductScene): )) self.wait() + def transition_to_lemma_1(self): + self.lighthouse_height = Lemma1.CONFIG["lighthouse_height"] + self.circle_radius = Lemma1.CONFIG["circle_radius"] + self.observer_fraction = Lemma1.CONFIG["observer_fraction"] + + self.ambient_light_config["color"] = BLUE + + circle = self.circle + lighthouses = self.lighthouses + lights = self.lights + + circle.generate_target() + circle.target.scale_to_fit_width(2 * self.circle_radius) + circle.target.to_corner(DL) + self.circle = circle.target + + new_lighthouses = self.get_lighthouses() + new_lights = self.get_lights() + + self.clear() + self.play( + MoveToTarget(circle), + Transform(lighthouses, new_lighthouses), + Transform(lights, new_lights), + ApplyMethod( + self.observer_dot.move_to, + self.get_circle_point_at_proportion( + self.observer_fraction / self.num_lighthouses + ) + ), + MaintainPositionRelativeTo(self.observer, self.observer_dot), + ) + class Lemma1(DistanceProductScene): CONFIG = { "circle_radius": 2.5, "observer_fraction": 0.5, - # "ambient_light_config": { - # "num_levels": 5, - # "radius": 1, - # }, "lighthouse_height": 0.25, "lemma_text": "distance product = 2", } @@ -2142,11 +2521,13 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): CONFIG = { "num_lighthouses": 9, "circle_radius": 2.75, - "ambient_light_config": CHEAP_AMBIENT_LIGHT_CONFIG, - "add_lights_in_foreground": False, - # "add_lights_in_foreground": True, + # "ambient_light_config": CHEAP_AMBIENT_LIGHT_CONFIG, + "add_lights_in_foreground": False, # Keep this way "text_scale_val": 0.7, "observer_fraction": 0.5, + "keeper_color": BLUE, + "sailor_color": YELLOW_D, + "include_distance_labels_background_rectangle": False, } def setup(self): @@ -2157,14 +2538,16 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): def construct(self): self.place_lighthouses() self.introduce_observers() - # self.write_distance_product_fraction() + self.write_distance_product_fraction() self.break_down_distance_product_by_parts() + self.grow_circle_and_N() self.show_limit_for_each_fraction() + self.show_limit_of_lhs() def place_lighthouses(self): circle = self.circle circle.to_corner(DL) - circle.shift(SMALL_BUFF * UP) + circle.shift(MED_SMALL_BUFF * UR) circle.set_color(RED) lighthouses = self.get_lighthouses() @@ -2272,8 +2655,8 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): # stacked_labels, h_line, times, product_decimal = column # Define result fraction - equals = TexMobject("=") - result_fraction = TexMobject( + equals = self.distance_product_equals = TexMobject("=") + result_fraction = self.result_fraction = TexMobject( "{N", "{\\text{distance} \\choose \\text{between obs.}}", "\\over", "2}" ) N, dist, frac_line, two = result_fraction @@ -2313,10 +2696,11 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): ShowCreation(fraction[1]) ) self.wait() - self.play( - LaggedStart(FadeIn, sailor_line_lengths), - FadeIn(sailor_dp_column) - ) + self.play(LaggedStart(FadeIn, sailor_line_lengths)) + self.play(ReplacementTransform( + sailor_line_lengths.copy(), sailor_dp_column[0] + )) + self.play(FadeIn(sailor_dp_column[1:])) self.play(ShowCreation(sailor_dp_decimal_rect)) self.play( fraction.next_to, equals, LEFT, @@ -2331,10 +2715,13 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): self.remove(*list(sailor_lines) + list(sailor_line_lengths)) self.play( FadeOut(sailor_dp_column), - FadeIn(keeper_dp_column), ReplacementTransform(sailor_lines.deepcopy(), keeper_lines), ReplacementTransform(sailor_line_lengths.deepcopy(), keeper_line_lengths), ) + self.play(ReplacementTransform( + keeper_line_lengths.copy(), keeper_dp_column[0] + )) + self.play(FadeIn(keeper_dp_column[1:])) self.wait() self.play( ShowCreation(keeper_dp_decimal_rect), @@ -2368,9 +2755,12 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): self.play( ReplacementTransform(keeper_lines.copy(), sailor_lines), ReplacementTransform(keeper_line_lengths.copy(), sailor_line_lengths), - FadeOut(keeper_dp_column), + FadeOut(keeper_dp_column[:-1]), FadeOut(new_keeper_dp_decimal), ) + self.play( + Rotate(sailor_line_lengths[0], 30 * DEGREES, rate_func=wiggle) + ) self.wait() self.play( ReplacementTransform(sailor_lines[0].copy(), dist), @@ -2391,51 +2781,30 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): self.wait() def break_down_distance_product_by_parts(self): - product_parts = TexMobject( + result_fraction = self.result_fraction + result_fraction_rect = SurroundingRectangle(result_fraction) + + product_parts = self.product_parts = TexMobject( "{|L_1 - K|", "\\over", "|L_1 - S|}", "\\cdot", "{|L_2 - K|", "\\over", "|L_2 - S|}", "\\cdot", "{|L_3 - K|", "\\over", "|L_3 - S|}", "\\cdots", - # "{|L_{N-1} - K|", "\\over", "|L_{N-1}- S|}", ) product_parts.set_color_by_tex_to_color_map({ - "K": YELLOW, - "S": BLUE, + "K": BLUE, + "S": YELLOW, }) product_parts.scale_to_fit_width(0.4 * FRAME_WIDTH) - product_parts.move_to(self.observers) - product_parts.to_edge(RIGHT) + product_parts.next_to(result_fraction, DOWN, LARGE_BUFF, RIGHT) + product_parts.shift(MED_SMALL_BUFF * RIGHT) - center = self.circle.get_center() - lighthouse_labels = VGroup() - for i, point in enumerate(self.get_lh_points()): - label = TexMobject("L_%d" % i) - label.scale(0.8) - label.move_to(center + 1.15 * (point - center)) - # label.move_to(center + 0.87 * (point - center)) - lighthouse_labels.add(label) + lighthouse_labels = self.get_light_labels() + self.lighthouse_labels = lighthouse_labels - sailor_lines = self.get_distance_lines(self.get_sailor_point()) - sailor_lines.set_stroke(BLUE_C, 3) + sailor_lines = self.get_sailor_lines() sailor_lines.save_state() - keeper_lines = self.get_distance_lines(self.get_keeper_point()) - keeper_lines.set_stroke(YELLOW, 3) + keeper_lines = self.get_keeper_lines() keeper_lines.save_state() - # sailor_line_braces = VGroup() - # keeper_line_braces = VGroup() - # triplets = [ - # (sailor_line_braces, sailor_lines, DOWN), - # (keeper_line_braces, keeper_lines, UP), - # ] - # for brace_group, line_group, vect in triplets: - # for line in line_group: - # angle = line.get_angle() - # line.rotate(-angle) - # brace = Brace(line, vect, buff=SMALL_BUFF) - # brace.match_color(line) - # VGroup(line, brace).rotate(angle, about_point=line.get_center()) - # brace_group.add(brace) - sailor_length_braces = VGroup(VMobject()) # Add fluff first object keeper_length_braces = VGroup(VMobject()) # Add fluff first object triplets = [ @@ -2444,10 +2813,11 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): ] for char, brace_group, vect in triplets: for part in product_parts.get_parts_by_tex(char): - brace = Brace(part, vect) + brace = Brace(part, vect, buff=SMALL_BUFF) brace.match_color(part) brace_group.add(brace) + # Animations self.remove(self.lights[0], self.lighthouses[0]) if self.add_lights_in_foreground: self.add_foreground_mobjects(lighthouse_labels[1:]) @@ -2464,22 +2834,547 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): keeper_lines.restore() self.wait() + keeper_line = self.keeper_line = keeper_lines[1].copy() + sailor_line = self.sailor_line = sailor_lines[1].copy() + keeper_brace = keeper_length_braces[1].copy() + sailor_brace = sailor_length_braces[1].copy() self.play( - ShowCreation(keeper_lines[1]), - GrowFromCenter(keeper_length_braces[1]), + ShowCreation(keeper_line), + GrowFromCenter(keeper_brace), ) self.wait() self.play( - ShowCreation(sailor_lines[1]), - GrowFromCenter(sailor_length_braces[1]), + ShowCreation(sailor_line), + GrowFromCenter(sailor_brace), ) self.wait() + for i in range(2, 4): + self.play( + Transform(keeper_line, keeper_lines[i]), + Transform(keeper_brace, keeper_length_braces[i]), + ) + self.play( + Transform(sailor_line, sailor_lines[i]), + Transform(sailor_brace, sailor_length_braces[i]), + ) + self.wait() + for i in range(4, self.num_lighthouses): + anims = [ + Transform(keeper_line, keeper_lines[i]), + Transform(sailor_line, sailor_lines[i]), + ] + if i == 4: + anims += [ + FadeOut(sailor_brace), + FadeOut(keeper_brace), + ] + self.play(*anims) + self.play(FocusOn(result_fraction)) + self.play(ShowPassingFlash(result_fraction_rect)) + self.wait(3) + + def grow_circle_and_N(self, circle_scale_factor=2, N_multiple=3): + circle = self.circle + lights = self.lights + labels = self.lighthouse_labels + keeper_line = self.keeper_line + sailor_line = self.sailor_line + keeper = self.keeper + sailor = self.sailor + half_N = self.num_lighthouses / 2 + + circle.generate_target() + for pi in keeper, sailor: + for mob in pi, pi.dot, pi.title: + mob.generate_target() + + circle.target.scale(circle_scale_factor) + circle.target.move_to(FRAME_WIDTH * LEFT / 2 + SMALL_BUFF * RIGHT) + self.circle = circle.target + self.num_lighthouses = int(N_multiple * self.num_lighthouses) + new_lights = self.get_lights() + for light in new_lights: + light.scale(1.0 / circle_scale_factor) + new_labels = self.get_light_labels() + self.keeper_lines = self.get_keeper_lines() + self.sailor_lines = self.get_sailor_lines() + + for group in lights, labels, new_lights, new_labels: + group[0].fade(1) + + keeper.dot.target.move_to(self.get_keeper_point()) + sailor.dot.target.move_to(self.get_sailor_point()) + for pi in keeper, sailor: + pi.target.scale(0) + pi.target.move_to(pi.dot.target) + pi.title.target.scale(0.85) + pi.title.target.next_to(pi.dot.target, RIGHT, SMALL_BUFF) + + self.circle = circle + + for mob in lights, labels: + for x in range(len(new_lights) - len(mob)): + mob.submobjects.insert(half_N + 1, VectorizedPoint(circle.get_left())) + + light_anim = ReplacementTransform(lights, new_lights) + self.play( + MoveToTarget(circle), + Transform(keeper_line, self.keeper_lines[-1]), + Transform(sailor_line, self.sailor_lines[-1]), + ReplacementTransform(labels, new_labels), + light_anim, + *[ + MoveToTarget(part) + for pi in self.observers + for part in pi, pi.dot, pi.title + ], + run_time=2 + ) + if self.add_lights_in_foreground: + self.remove_foreground_mobjects(*self.lights) + self.remove_foreground_mobjects(*self.lighthouse_labels) + self.add_foreground_mobjects(new_lights, new_labels) + self.wait() + self.lights = new_lights + self.lighthouse_labels = new_labels def show_limit_for_each_fraction(self): - pass + product_parts = self.product_parts + keeper_line = self.keeper_line + keeper_lines = self.keeper_lines + sailor_line = self.sailor_line + sailor_lines = self.sailor_lines + labels = self.lighthouse_labels + + center = self.circle.get_center() + center_dot = Dot(center) + lh_points = self.get_lh_points() + sailor_point = self.get_sailor_point() + keeper_point = self.get_keeper_point() + + def get_angle_mob(p1, p2): + angle1 = angle_of_vector(p1 - center) + angle2 = angle_of_vector(p2 - center) + arc = Arc(start_angle=angle1, angle=(angle2 - angle1), radius=1) + arc.shift(center) + return VGroup( + center_dot, + Line(center, p1), + Line(center, p2), + arc, + ) + + angle_mob = get_angle_mob(lh_points[1], keeper_point) + + ratios = VGroup(*[ + product_parts[i:i + 3] + for i in [0, 4, 8] + ]) + term_rects = self.get_term_rects(ratios) + + limit_fractions = VGroup( + TexMobject("{2", "\\over", "1}"), + TexMobject("{4", "\\over", "3}"), + TexMobject("{6", "\\over", "5}"), + ) + limit_arrows = VGroup() + for rect, fraction in zip(term_rects, limit_fractions): + fraction.next_to(rect, DOWN, LARGE_BUFF) + arrow = Arrow(rect, fraction, color=WHITE) + limit_arrows.add(arrow) + + approx = TexMobject("\\approx") + approx.scale(1.5) + approx.rotate(90 * DEGREES) + approx.move_to(limit_arrows[0]) + + braces = self.get_all_circle_braces() + + # Show first lighthouse + term_rect = term_rects[0].copy() + self.play( + Transform(keeper_line, keeper_lines[1]), + Transform(sailor_line, sailor_lines[1]), + FadeIn(term_rect), + path_arc=-180 * DEGREES + ) + self.wait(2) + self.play( + FadeOut(VGroup(keeper_line, sailor_line)), + FadeIn(braces[:2]), + FadeIn(angle_mob) + ) + self.wait() + self.play(Transform(angle_mob, get_angle_mob(lh_points[1], sailor_point))) + self.wait(2) + self.play( + Write(approx), + ReplacementTransform(ratios[0].copy(), limit_fractions[0]), + FadeOut(angle_mob) + ) + self.wait() + self.play(ReplacementTransform(approx, limit_arrows[0])) + self.let_N_approach_infinity(braces[:2]) + + # Show second lighthouse + self.play( + Transform(term_rect, term_rects[1]), + ReplacementTransform(limit_arrows[0].copy(), limit_arrows[1]), + FadeIn(braces[2:4]) + ) + for group, color in (braces[:4], self.keeper_color), (braces[1:4], self.sailor_color): + self.play( + group.scale, 0.95, {"about_point": center}, + group.set_color, color, + rate_func=there_and_back + ) + self.wait(0.5) + self.play( + ReplacementTransform(ratios[1].copy(), limit_fractions[1]) + ) + self.wait() + + # Show third lighthouse + braces[4:6].set_color(YELLOW) + self.play( + Transform(term_rect, term_rects[2]), + ReplacementTransform(limit_arrows[1].copy(), limit_arrows[2]), + FadeIn(braces[4:6]), + braces[1:4].set_color, YELLOW, + ReplacementTransform(limit_fractions[1].copy(), limit_fractions[2]) + ) + self.let_N_approach_infinity(braces[:6]) + self.wait() + + # Set up for lighthouse "before" keeper + ccw_product_group = VGroup(product_parts, limit_arrows, limit_fractions) + cw_product_parts = TexMobject( + "\\cdots", "{|L_{-3} - K|", "\\over", "|L_{-3} - S|}", + "\\cdot", "{|L_{-2} - K|", "\\over", "|L_{-2} - S|}", + "\\cdot", "{|L_{-1} - K|", "\\over", "|L_{-1} - S|}", + ) + cw_product_parts.match_height(product_parts) + cw_product_parts.set_color_by_tex_to_color_map({ + "K": BLUE, + "S": YELLOW, + }) + cw_product_parts.move_to(ratios, RIGHT) + cw_ratios = VGroup(*[cw_product_parts[i:i + 3] for i in 9, 5, 1]) + cw_term_rects = self.get_term_rects(cw_ratios) + cw_limit_fractions = VGroup( + TexMobject("{2", "\\over", "3}"), + TexMobject("{4", "\\over", "5}"), + TexMobject("{6", "\\over", "7}"), + ) + cw_limit_arrows = VGroup() + for rect, fraction in zip(cw_term_rects, cw_limit_fractions): + fraction.next_to(rect, DOWN, LARGE_BUFF) + arrow = Arrow(rect, fraction, color=WHITE) + cw_limit_arrows.add(arrow) + + cw_product_parts.save_state() + cw_product_parts.next_to(product_parts, RIGHT, LARGE_BUFF) + + cw_label_rects = self.get_term_rects(labels[-1:-5:-1]) + cw_label_rects.set_color(RED) + + braces[-8:].set_color(BLUE) + braces[0].set_color(YELLOW) + + def show_braces(n): + cw_group = braces[-2 * n:] + for group in cw_group, VGroup(braces[0], *cw_group): + self.play( + group.scale, 0.95, {"about_point": center}, + rate_func=there_and_back + ) + self.wait(0.5) + + # Animated clockwise-from-keeper terms + self.play( + ccw_product_group.scale, 0.5, {"about_edge": UL}, + ccw_product_group.to_corner, UL, + FadeOut(term_rect), + FadeOut(braces[:6]), + cw_product_parts.restore, + ) + term_rect = cw_term_rects[0].copy() + self.play(LaggedStart(ShowCreationThenDestruction, cw_label_rects)) + self.wait() + self.play( + FadeIn(term_rect), + FadeIn(braces[-2:]), + FadeIn(braces[0]), + ) + show_braces(1) + self.play( + GrowArrow(cw_limit_arrows[0]), + FadeIn(cw_limit_fractions[0]) + ) + self.wait() + + # Second and third lighthouse before + self.play( + Transform(term_rect, cw_term_rects[1]), + ReplacementTransform(cw_limit_arrows[0].copy(), cw_limit_arrows[1]), + FadeIn(braces[-4:-2]), + Write(cw_limit_fractions[1]) + ) + show_braces(2) + self.wait() + self.play( + Transform(term_rect, cw_term_rects[2]), + ReplacementTransform(cw_limit_arrows[1].copy(), cw_limit_arrows[2]), + FadeIn(braces[-6:-4]), + Write(cw_limit_fractions[2]) + ) + show_braces(3) + self.let_N_approach_infinity(VGroup(braces[0], *braces[-6:])) + self.wait() + + # Organize fractions + fractions = VGroup(*it.chain(*zip( + limit_fractions, cw_limit_fractions, + ))) + fractions.generate_target() + wallis_product = VGroup() + dots = VGroup() + for fraction in fractions.target: + fraction.match_height(cw_limit_fractions[0]) + wallis_product.add(fraction) + dot = TexMobject("\\cdot") + wallis_product.add(dot) + dots.add(dot) + final_dot = TexMobject("\\cdots") + for group in wallis_product, dots: + group.submobjects[-1] = final_dot + wallis_product.arrange_submobjects(RIGHT, buff=MED_SMALL_BUFF) + wallis_product.to_edge(RIGHT) + + self.play( + FadeOut(limit_arrows), + FadeOut(cw_limit_arrows), + FadeOut(braces[-6:]), + FadeOut(braces[0]), + FadeOut(term_rect), + ) + self.play( + cw_product_parts.scale, 0.5, + cw_product_parts.next_to, product_parts, DOWN, {"aligned_edge": LEFT}, + MoveToTarget(fractions), + Write(dots), + run_time=2, + path_arc=90 * DEGREES + ) + self.wait() + + self.wallis_product = VGroup(dots, fractions) + self.observers_brace = braces[0] + + def show_limit_of_lhs(self): + brace = self.observers_brace + wallis_product = self.wallis_product + result_fraction = self.result_fraction + N, dist, over, two = result_fraction + distance_product_equals = self.distance_product_equals + + result_rect = SurroundingRectangle(result_fraction) + result_rect.set_color(WHITE) + + equals = TexMobject("=") + equals.next_to(brace, LEFT, SMALL_BUFF) + approx1, approx2, approx3 = [TexMobject("\\approx") for x in range(3)] + approx1.next_to(brace, LEFT, SMALL_BUFF) + half_two_pi_over_N = TexMobject( + "{1", "\\over", "2}", "{2", "\\pi", "\\over", "N}", + ) + pi = half_two_pi_over_N.get_part_by_tex("\\pi") + half_two_pi_over_N.next_to(approx1, LEFT) + approx2.next_to(half_two_pi_over_N, LEFT, SMALL_BUFF) + + approx3.move_to(distance_product_equals) + + pi_over_N = TexMobject("(", "\\pi", "/", "N", ")") + pi_over_N.next_to(N, RIGHT) + N_shift = MED_LARGE_BUFF * RIGHT + pi_over_N.shift(N_shift) + + pi_halves = TexMobject("{\\pi", "\\over", "2}") + pi_halves.next_to(result_rect, DOWN, LARGE_BUFF) + pi_halves.shift(RIGHT) + pi_halves_arrow = Arrow( + result_rect.get_bottom(), + pi_halves.get_top(), + color=WHITE, + buff=SMALL_BUFF + ) + + last_equals = TexMobject("=") + last_equals.next_to(pi_halves, LEFT) + + self.play(ShowCreation(result_rect)) + self.wait() + self.play( + dist.next_to, equals, LEFT, + FadeIn(equals), + GrowFromCenter(brace), + ) + self.wait() + approx2.next_to(dist, LEFT, SMALL_BUFF) + half_two_pi_over_N.next_to(approx2, LEFT) + self.play( + Write(half_two_pi_over_N), + FadeIn(approx2) + ) + self.wait() + self.play( + FadeOut(half_two_pi_over_N[:4]), + pi.shift, SMALL_BUFF * LEFT, + ) + self.wait() + self.play( + ReplacementTransform( + half_two_pi_over_N[-3:].copy(), + pi_over_N[1:4] + ), + FadeIn(pi_over_N[0]), + FadeIn(pi_over_N[-1]), + N.shift, N_shift * RIGHT, + ReplacementTransform(distance_product_equals, approx3) + ) + self.wait() + self.play( + GrowArrow(pi_halves_arrow), + wallis_product.shift, DOWN, + ) + self.play(Write(pi_halves)) + self.wait(2) + self.play( + wallis_product.next_to, last_equals, LEFT, 2 * SMALL_BUFF, + FadeIn(last_equals) + ) + final_rect = SurroundingRectangle( + VGroup(wallis_product, pi_halves), + buff=MED_SMALL_BUFF + ) + final_rect.set_color(YELLOW) + self.play(ShowCreation(final_rect)) + self.wait(2) # + def let_N_approach_infinity(self, braces=None, factor=4, run_time=5, zoom_in_after=False): + lights = self.lights + labels = self.lighthouse_labels + keeper, sailor = self.observers + circle = self.circle + + if braces is None: + braces = VGroup() + + start_fraction = 1.0 / self.num_lighthouses + target_fraction = start_fraction / factor + half_N = self.num_lighthouses / 2 + + fraction_tracker = ValueTracker(start_fraction) + + def get_fraction(): + return fraction_tracker.get_value() + + def get_ks_distance(): + return np.linalg.norm(keeper.dot.get_center() - sailor.dot.get_center()) + + def update_title_heights(*titles): + for title in titles: + if not hasattr(title, "original_height"): + title.original_height = title.get_height() + title.scale_to_fit_height(min( + title.original_height, + 0.8 * get_ks_distance(), + )) + if len(titles) > 1: + return titles + else: + return titles[0] + + initial_light_width = lights[0].get_width() + + def update_lights(lights): + for k in range(-half_N, half_N + 1): + if k == 0: + continue + light = lights[k] + light = light.scale_to_fit_width( + (get_fraction() / start_fraction) * initial_light_width + ) + point = self.get_circle_point_at_proportion(k * get_fraction()) + light.move_source_to(point) + return lights + + def update_braces(braces): + for brace in braces: + f1 = brace.fraction1 * (get_fraction() / start_fraction) + f2 = brace.fraction2 * (get_fraction() / start_fraction) + new_brace = self.get_circle_brace(f1, f2) + new_brace.match_style(brace) + Transform(brace, new_brace).update(1) + return braces + + light_update_anim = UpdateFromFunc(lights, update_lights) + label_update_anim = UpdateFromFunc( + labels, + lambda ls: self.position_labels_outside_lights(update_title_heights(*ls)), + ) + sailor_dot_anim = UpdateFromFunc( + sailor.dot, + lambda d: d.move_to(self.get_circle_point_at_proportion(get_fraction() / 2)) + ) + sailor_title_anim = UpdateFromFunc( + sailor.title, + lambda m: update_title_heights(m).next_to(sailor.dot, RIGHT, SMALL_BUFF) + ) + keeper_title_anim = UpdateFromFunc( + keeper.title, + lambda m: update_title_heights(m).next_to(keeper.dot, RIGHT, SMALL_BUFF) + ) + braces_update_anim = UpdateFromFunc(braces, update_braces) + + lights[0].fade(1) + labels[0].fade(1) + + all_updates = [ + light_update_anim, + label_update_anim, + sailor_dot_anim, + sailor_title_anim, + keeper_title_anim, + braces_update_anim, + ] + + self.play( + fraction_tracker.set_value, target_fraction, + *all_updates, + run_time=run_time + ) + if zoom_in_after: + self.play( + circle.scale, factor, {"about_point": circle.get_right()}, + *all_updates, + run_time=1 + ) + self.wait() + self.play( + circle.scale, 1.0 / factor, {"about_point": circle.get_right()}, + *all_updates, + run_time=1 + ) + self.wait() + self.play( + fraction_tracker.set_value, start_fraction, + *all_updates, + run_time=run_time / 2 + ) + def get_keeper_point(self): return self.get_circle_point_at_proportion(0) @@ -2487,16 +3382,81 @@ class KeeperAndSailor(DistanceProductScene, PiCreatureScene): return self.get_circle_point_at_proportion(0.5 / self.num_lighthouses) def create_pi_creatures(self): - keeper = self.keeper = Mortimer(color=YELLOW_D) - sailor = self.sailor = Randolph().flip() + keeper = self.keeper = PiCreature(color=self.keeper_color).flip() + sailor = self.sailor = PiCreature(color=self.sailor_color).flip() observers = self.observers = VGroup(keeper, sailor) observers.scale(0.5) keeper.shift(4 * RIGHT + 2 * DOWN) sailor.shift(4 * RIGHT + 2 * UP) return VGroup(keeper, sailor) - - + def get_light_labels(self): + labels = VGroup() + for count, point in enumerate(self.get_lh_points()): + if count > self.num_lighthouses / 2: + count -= self.num_lighthouses + label = TexMobject("L_{%d}" % count) + label.scale(0.8) + labels.add(label) + self.position_labels_outside_lights(labels) + return labels + + def position_labels_outside_lights(self, labels): + center = self.circle.get_center() + for light, label in zip(self.lights, labels): + point = light[0].get_center() + vect = (point - center) + norm = np.linalg.norm(vect) + buff = label.get_height() + vect *= (norm + buff) / norm + label.move_to(center + vect) + return labels + + def get_keeper_lines(self, line_class=Line): + lines = self.get_distance_lines(self.get_keeper_point()) + lines.set_stroke(self.keeper_color, 3) + return lines + + def get_sailor_lines(self, line_class=Line): + lines = self.get_distance_lines(self.get_sailor_point()) + lines.set_stroke(self.sailor_color, 3) + return lines + + def get_term_rects(self, terms): + return VGroup(*[ + SurroundingRectangle(term, color=WHITE) + for term in terms + ]) + + def get_circle_brace(self, f1, f2): + line = Line( + self.get_circle_point_at_proportion(f1), + self.get_circle_point_at_proportion(f2), + ) + angle = (line.get_angle() + TAU / 2) % TAU + scale_factor = 1.5 + line.rotate(-angle, about_point=ORIGIN) + line.scale(scale_factor, about_point=ORIGIN) + brace = Brace(line, DOWN, buff=SMALL_BUFF) + group = VGroup(line, brace) + group.scale(1.0 / scale_factor, about_point=ORIGIN) + group.rotate(angle, about_point=ORIGIN) + + # Keep track of a fraction between -0.5 and 0.5 + if f1 > 0.5: + f1 -= 1 + if f2 > 0.5: + f2 -= 1 + brace.fraction1 = f1 + brace.fraction2 = f2 + return brace + + def get_all_circle_braces(self): + fractions = np.linspace(0, 1, 2 * self.num_lighthouses + 1) + return VGroup(*[ + self.get_circle_brace(f1, f2) + for f1, f2 in zip(fractions, fractions[1:]) + ]) diff --git a/mobject/svg/drawings.py b/mobject/svg/drawings.py index 6206c33c..e22ec529 100644 --- a/mobject/svg/drawings.py +++ b/mobject/svg/drawings.py @@ -226,6 +226,7 @@ class Laptop(VGroup): body.set_fill(self.shaded_body_color, opacity=1) body.sort_submobjects(lambda p: p[2]) body[-1].set_fill(self.body_color) + screen_plate = body.copy() keyboard = VGroup(*[ VGroup(*[ Square(**self.key_color_kwargs) @@ -243,7 +244,6 @@ class Laptop(VGroup): keyboard.shift(MED_SMALL_BUFF * UP) body.add(keyboard) - screen_plate = body.copy() screen_plate.stretch(self.screen_thickness / self.body_dimensions[2], dim=2) screen = Rectangle( diff --git a/utils/bezier.py b/utils/bezier.py index 6097e130..c0926725 100644 --- a/utils/bezier.py +++ b/utils/bezier.py @@ -3,7 +3,7 @@ import numpy as np from scipy import linalg from utils.simple_functions import choose -CLOSED_THRESHOLD = 0.0 +CLOSED_THRESHOLD = 0.001 def bezier(points): @@ -98,7 +98,8 @@ def get_smooth_handle_points(points): def solve_func(b): return linalg.solve_banded((l, u), diag, b) - if is_closed(points): + use_closed_solve_function = is_closed(points) + if use_closed_solve_function: # Get equations to relate first and last points matrix = diag_to_matrix((l, u), diag) # last row handles second derivative @@ -109,11 +110,15 @@ def get_smooth_handle_points(points): b[0] = 2 * points[0] b[-1] = np.zeros(dim) - def solve_func(b): + def closed_curve_solve_func(b): return linalg.solve(matrix, b) + handle_pairs = np.zeros((2 * num_handles, dim)) for i in range(dim): - handle_pairs[:, i] = solve_func(b[:, i]) + if use_closed_solve_function: + handle_pairs[:, i] = closed_curve_solve_func(b[:, i]) + else: + handle_pairs[:, i] = solve_func(b[:, i]) return handle_pairs[0::2], handle_pairs[1::2]