forked from 3b1b/manim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborsuk.py
2656 lines (2353 loc) · 82.4 KB
/
borsuk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from manimlib.imports import *
from functools import reduce
class Jewel(VMobject):
CONFIG = {
"color" : WHITE,
"fill_opacity" : 0.75,
"stroke_width" : 0,
"height" : 0.5,
"num_equator_points" : 5,
"sun_vect" : OUT+LEFT+UP,
}
def generate_points(self):
for vect in OUT, IN:
compass_vects = list(compass_directions(self.num_equator_points))
if vect is IN:
compass_vects.reverse()
for vect_pair in adjacent_pairs(compass_vects):
self.add(Polygon(vect, *vect_pair))
self.set_height(self.height)
self.rotate(-np.pi/2-np.pi/24, RIGHT)
self.rotate(-np.pi/12, UP)
self.submobjects.sort(
key=lambda m: -m1.get_center()[2]
)
return self
class Necklace(VMobject):
CONFIG = {
"width" : FRAME_WIDTH - 1,
"jewel_buff" : MED_SMALL_BUFF,
"chain_color" : GREY,
"default_colors" : [(4, BLUE), (6, WHITE), (4, GREEN)]
}
def __init__(self, *colors, **kwargs):
digest_config(self, kwargs, locals())
if len(colors) == 0:
self.colors = self.get_default_colors()
VMobject.__init__(self, **kwargs)
def get_default_colors(self):
result = list(it.chain(*[
num*[color]
for num, color in self.default_colors
]))
random.shuffle(result)
return result
def generate_points(self):
jewels = VGroup(*[
Jewel(color = color)
for color in self.colors
])
jewels.arrange(buff = self.jewel_buff)
jewels.set_width(self.width)
jewels.center()
j_to_j_dist = (jewels[1].get_center()-jewels[0].get_center())[0]
chain = Line(
jewels[0].get_center() + j_to_j_dist*LEFT/2,
jewels[-1].get_center() + j_to_j_dist*RIGHT/2,
color = self.chain_color,
)
self.add(chain, *jewels)
self.chain = chain
self.jewels = jewels
################
class FromPreviousTopologyVideo(Scene):
def construct(self):
rect = Rectangle(height = 9, width = 16)
rect.set_height(FRAME_HEIGHT-2)
title = TextMobject("From original ``Who cares about topology'' video")
title.to_edge(UP)
rect.next_to(title, DOWN)
self.add(title)
self.play(ShowCreation(rect))
self.wait()
class CheckOutMathologer(PiCreatureScene):
CONFIG = {
"logo_height" : 1.5,
"screen_height" : 5,
"channel_name" : "Mathologer",
"logo_file" : "mathologer_logo",
"logo_color" : None,
}
def construct(self):
logo = self.get_logo()
name = TextMobject(self.channel_name)
name.next_to(logo, RIGHT)
rect = Rectangle(height = 9, width = 16)
rect.set_height(self.screen_height)
rect.next_to(logo, DOWN)
rect.to_edge(LEFT)
self.play(
self.get_logo_intro_animation(logo),
self.pi_creature.change_mode, "hooray",
)
self.play(
ShowCreation(rect),
Write(name)
)
self.wait(2)
self.change_mode("happy")
self.wait(2)
def get_logo(self):
logo = ImageMobject(self.logo_file)
logo.set_height(self.logo_height)
logo.to_corner(UP+LEFT)
if self.logo_color is not None:
logo.set_color(self.logo_color)
logo.stroke_width = 1
return logo
def get_logo_intro_animation(self, logo):
logo.save_state()
logo.shift(DOWN)
logo.set_color(BLACK)
return ApplyMethod(logo.restore)
class IntroduceStolenNecklaceProblem(ThreeDScene):
CONFIG = {
"jewel_colors" : [BLUE, GREEN, WHITE, RED],
"num_per_jewel" : [8, 10, 4, 6],
"num_shuffles" : 1,
"necklace_center" : UP,
"random_seed" : 2,
"forced_binary_choices" : (0, 1, 0, 1, 0),
}
def construct(self):
random.seed(self.random_seed)
self.add_thieves()
self.write_title()
self.introduce_necklace()
self.divvy_by_cutting_all()
self.divvy_with_n_cuts()
self.shuffle_jewels(self.necklace.jewels)
self.divvy_with_n_cuts()
def add_thieves(self):
thieves = VGroup(
Randolph(),
Mortimer()
)
thieves.arrange(RIGHT, buff = 4*LARGE_BUFF)
thieves.to_edge(DOWN)
thieves[0].make_eye_contact(thieves[1])
self.add(thieves)
self.thieves = thieves
def write_title(self):
title = TextMobject("Stolen necklace problem")
title.to_edge(UP)
self.play(
Write(title),
*[
ApplyMethod(pi.look_at, title)
for pi in self.thieves
]
)
self.title = title
def introduce_necklace(self):
necklace = self.get_necklace()
jewels = necklace.jewels
jewel_types = self.get_jewels_organized_by_type(jewels)
enumeration_labels = VGroup()
for jewel_type in jewel_types:
num_mob = TexMobject(str(len(jewel_type)))
jewel_copy = jewel_type[0].copy().scale(2)
jewel_copy.next_to(num_mob)
label = VGroup(num_mob, jewel_copy)
enumeration_labels.add(label)
enumeration_labels.arrange(RIGHT, buff = LARGE_BUFF)
enumeration_labels.to_edge(UP)
self.play(
FadeIn(
necklace,
lag_ratio = 0.5,
run_time = 3
),
*it.chain(*[
[pi.change_mode, "conniving", pi.look_at, necklace]
for pi in self.thieves
])
)
self.play(*[
ApplyMethod(
jewel.rotate_in_place, np.pi/6, UP,
rate_func = there_and_back
)
for jewel in jewels
])
self.play(Blink(self.thieves[0]))
for jewel_type in jewel_types:
self.play(
jewel_type.shift, 0.2*UP,
rate_func = wiggle
)
self.wait()
for x in range(self.num_shuffles):
self.shuffle_jewels(jewels)
self.play(FadeOut(self.title))
for jewel_type, label in zip(jewel_types, enumeration_labels):
jewel_type.submobjects.sort(
key=lambda m: m1.get
)
jewel_type.save_state()
jewel_type.generate_target()
jewel_type.target.arrange()
jewel_type.target.scale(2)
jewel_type.target.move_to(2*UP)
self.play(
MoveToTarget(jewel_type),
Write(label)
)
self.play(jewel_type.restore)
self.play(Blink(self.thieves[1]))
self.enumeration_labels = enumeration_labels
self.jewel_types = jewel_types
def divvy_by_cutting_all(self):
enumeration_labels = self.enumeration_labels
necklace = self.necklace
jewel_types = self.jewel_types
thieves = self.thieves
both_half_labels = VGroup()
for thief, vect in zip(self.thieves, [LEFT, RIGHT]):
half_labels = VGroup()
for label in enumeration_labels:
tex, jewel = label
num = int(tex.get_tex_string())
half_label = VGroup(
TexMobject(str(num/2)),
jewel.copy()
)
half_label.arrange()
half_labels.add(half_label)
half_labels.arrange(DOWN)
half_labels.set_height(thief.get_height())
half_labels.next_to(
thief, vect,
buff = MED_LARGE_BUFF,
aligned_edge = DOWN
)
both_half_labels.add(half_labels)
for half_labels in both_half_labels:
self.play(ReplacementTransform(
enumeration_labels.copy(),
half_labels
))
self.play(*[ApplyMethod(pi.change_mode, "pondering") for pi in thieves])
self.wait()
for type_index, jewel_type in enumerate(jewel_types):
jewel_type.save_state()
jewel_type_copy = jewel_type.copy()
n_jewels = len(jewel_type)
halves = [
VGroup(*jewel_type_copy[:n_jewels/2]),
VGroup(*jewel_type_copy[n_jewels/2:]),
]
for half, thief, vect in zip(halves, thieves, [RIGHT, LEFT]):
half.arrange(DOWN)
half.next_to(
thief, vect,
buff = SMALL_BUFF + type_index*half.get_width(),
aligned_edge = DOWN
)
self.play(
Transform(jewel_type, jewel_type_copy),
*[
ApplyMethod(thief.look_at, jewel_type_copy)
for thief in thieves
]
)
self.play(*it.chain(*[
[thief.change_mode, "happy", thief.look_at, necklace]
for thief in thieves
]))
self.wait()
self.play(*[
jewel_type.restore
for jewel_type in jewel_types
])
self.play(*it.chain(*[
[thief.change_mode, "confused", thief.look_at, necklace]
for thief in thieves
]))
def divvy_with_n_cuts(
self,
with_thieves = True,
highlight_groups = True,
show_matching_after_divvying = True,
):
necklace = self.necklace
jewel_types = self.jewel_types
jewels = sorted(
necklace.jewels,
lambda m1, m2 : cmp(m1.get_center()[0], m2.get_center()[0])
)
slice_indices, binary_choices = self.find_slice_indices(jewels, jewel_types)
subgroups = [
VGroup(*jewels[i1:i2])
for i1, i2 in zip(slice_indices, slice_indices[1:])
]
buff = (jewels[1].get_left()[0]-jewels[0].get_right()[0])/2
v_lines = VGroup(*[
DashedLine(UP, DOWN).next_to(group, RIGHT, buff = buff)
for group in subgroups[:-1]
])
strand_groups = [VGroup(), VGroup()]
for group, choice in zip(subgroups, binary_choices):
strand = Line(
group[0].get_center(), group[-1].get_center(),
color = necklace.chain.get_color()
)
strand.add(*group)
strand_groups[choice].add(strand)
self.add(strand)
self.play(ShowCreation(v_lines))
self.play(
FadeOut(necklace.chain),
*it.chain(*[
list(map(Animation, group))
for group in strand_groups
])
)
for group in strand_groups:
group.save_state()
self.play(
strand_groups[0].shift, UP/2.,
strand_groups[1].shift, DOWN/2.,
)
if with_thieves:
self.play(*it.chain(*[
[thief.change_mode, "happy", thief.look_at, self.necklace]
for thief in self.thieves
]))
self.play(Blink(self.thieves[1]))
else:
self.wait()
if highlight_groups:
for group in strand_groups:
box = Rectangle(
width = group.get_width()+2*SMALL_BUFF,
height = group.get_height()+2*SMALL_BUFF,
stroke_width = 0,
fill_color = YELLOW,
fill_opacity = 0.3,
)
box.move_to(group)
self.play(FadeIn(box))
self.wait()
self.play(FadeOut(box))
self.wait()
if show_matching_after_divvying:
for jewel_type in jewel_types:
self.play(
*[
ApplyMethod(jewel.scale_in_place, 1.5)
for jewel in jewel_type
],
rate_func = there_and_back,
run_time = 2
)
self.wait()
self.play(
FadeOut(v_lines),
FadeIn(necklace.chain),
*[
group.restore for group in strand_groups
]
)
self.remove(*strand_groups)
self.add(necklace)
########
def get_necklace(self, **kwargs):
colors = reduce(op.add, [
num*[color]
for num, color in zip(self.num_per_jewel, self.jewel_colors)
])
self.necklace = Necklace(*colors, **kwargs)
self.necklace.shift(self.necklace_center)
return self.necklace
def get_jewels_organized_by_type(self, jewels):
return [
VGroup(*[m for m in jewels if m.get_color() == color])
for color in map(Color, self.jewel_colors)
]
def shuffle_jewels(self, jewels, run_time = 2, path_arc = np.pi/2, **kwargs):
shuffled_indices = list(range(len(jewels)))
random.shuffle(shuffled_indices)
target_group = VGroup(*[
jewel.copy().move_to(jewels[shuffled_indices[i]])
for i, jewel in enumerate(jewels)
])
self.play(Transform(
jewels, target_group,
run_time = run_time,
path_arc = path_arc,
**kwargs
))
def find_slice_indices(self, jewels, jewel_types):
def jewel_to_type_number(jewel):
for i, jewel_type in enumerate(jewel_types):
if jewel in jewel_type:
return i
raise Exception("Not in any jewel_types")
type_numbers = list(map(jewel_to_type_number, jewels))
n_types = len(jewel_types)
for slice_indices in it.combinations(list(range(1, len(jewels))), n_types):
slice_indices = [0] + list(slice_indices) + [len(jewels)]
if self.forced_binary_choices is not None:
all_binary_choices = [self.forced_binary_choices]
else:
all_binary_choices = it.product(*[list(range(2))]*(n_types+1))
for binary_choices in all_binary_choices:
subsets = [
type_numbers[i1:i2]
for i1, i2 in zip(slice_indices, slice_indices[1:])
]
left_sets, right_sets = [
[
subset
for subset, index in zip(subsets, binary_choices)
if index == target_index
]
for target_index in range(2)
]
flat_left_set = np.array(list(it.chain(*left_sets)))
flat_right_set = np.array(list(it.chain(*right_sets)))
match_array = [
sum(flat_left_set == n) == sum(flat_right_set == n)
for n in range(n_types)
]
if np.all(match_array):
return slice_indices, binary_choices
raise Exception("No fair division found")
class ThingToProve(PiCreatureScene):
def construct(self):
arrow = Arrow(UP, DOWN)
top_words = TextMobject("$n$ jewel types")
top_words.next_to(arrow, UP)
bottom_words = TextMobject("""
Fair division possible
with $n$ cuts
""")
bottom_words.next_to(arrow, DOWN)
self.play(
Write(top_words, run_time = 2),
self.pi_creature.change_mode, "raise_right_hand"
)
self.play(ShowCreation(arrow))
self.play(
Write(bottom_words, run_time = 2),
self.pi_creature.change_mode, "pondering"
)
self.wait(3)
class FiveJewelCase(IntroduceStolenNecklaceProblem):
CONFIG = {
"jewel_colors" : [BLUE, GREEN, WHITE, RED, YELLOW],
"num_per_jewel" : [6, 4, 4, 2, 8],
"forced_binary_choices" : (0, 1, 0, 1, 0, 1),
}
def construct(self):
random.seed(self.random_seed)
self.add(self.get_necklace())
jewels = self.necklace.jewels
self.shuffle_jewels(jewels, run_time = 0)
self.jewel_types = self.get_jewels_organized_by_type(jewels)
self.add_title()
self.add_thieves()
for thief in self.thieves:
ApplyMethod(thief.change_mode, "pondering").update(1)
thief.look_at(self.necklace)
self.divvy_with_n_cuts()
def add_title(self):
n_cuts = len(self.jewel_colors)
title = TextMobject(
"%d jewel types, %d cuts"%(n_cuts, n_cuts)
)
title.to_edge(UP)
self.add(title)
class SixJewelCase(FiveJewelCase):
CONFIG = {
"jewel_colors" : [BLUE, GREEN, WHITE, RED, YELLOW, MAROON_B],
"num_per_jewel" : [6, 4, 4, 2, 2, 6],
"forced_binary_choices" : (0, 1, 0, 1, 0, 1, 0),
}
class DiscussApplicability(TeacherStudentsScene):
def construct(self):
self.teacher_says("""
Minimize sharding,
allocate resources evenly
""")
self.change_student_modes(*["pondering"]*3)
self.wait(2)
class ThreeJewelCase(FiveJewelCase):
CONFIG = {
"jewel_colors" : [BLUE, GREEN, WHITE],
"num_per_jewel" : [6, 4, 8],
"forced_binary_choices" : (0, 1, 0, 1),
}
class RepeatedShuffling(IntroduceStolenNecklaceProblem):
CONFIG = {
"num_shuffles" : 5,
"random_seed" : 3,
"show_matching_after_divvying" : False,
}
def construct(self):
random.seed(self.random_seed)
self.add(self.get_necklace())
jewels = self.necklace.jewels
self.jewel_types = self.get_jewels_organized_by_type(jewels)
self.add_thieves()
for thief in self.thieves:
ApplyMethod(thief.change_mode, "pondering").update(1)
thief.look_at(self.necklace)
for x in range(self.num_shuffles):
self.shuffle_jewels(jewels)
self.divvy_with_n_cuts(
show_matching_after_divvying = False
)
class NowForTheTopology(TeacherStudentsScene):
def construct(self):
self.teacher_says("Now for the \\\\ topology")
self.change_student_modes(*["hooray"]*3)
self.wait(3)
class ExternallyAnimatedScene(Scene):
def construct(self):
raise Exception("Don't actually run this class.")
class SphereOntoPlaneIn3D(ExternallyAnimatedScene):
pass
class DiscontinuousSphereOntoPlaneIn3D(ExternallyAnimatedScene):
pass
class WriteWords(Scene):
CONFIG = {
"words" : "",
"color" : WHITE,
}
def construct(self):
words = TextMobject(self.words)
words.set_color(self.color)
words.set_width(FRAME_WIDTH-1)
words.to_edge(DOWN)
self.play(Write(words))
self.wait(2)
class WriteNotAllowed(WriteWords):
CONFIG = {
"words" : "Not allowed",
"color" : RED,
}
class NonAntipodalCollisionIn3D(ExternallyAnimatedScene):
pass
class AntipodalCollisionIn3D(ExternallyAnimatedScene):
pass
class WriteBorsukUlam(WriteWords):
CONFIG = {
"words" : "Borsuk-Ulam Theorem",
}
class WriteAntipodal(WriteWords):
CONFIG = {
"words" : "``Antipodal''",
"color" : MAROON_B,
}
class ProjectOntoEquatorIn3D(ExternallyAnimatedScene):
pass
class ProjectOntoEquatorWithPolesIn3D(ExternallyAnimatedScene):
pass
class ProjectAntipodalNonCollisionIn3D(ExternallyAnimatedScene):
pass
class ShearThenProjectnOntoEquatorPolesMissIn3D(ExternallyAnimatedScene):
pass
class ShearThenProjectnOntoEquatorAntipodalCollisionIn3D(ExternallyAnimatedScene):
pass
class ClassicExample(TeacherStudentsScene):
def construct(self):
self.teacher_says("The classic example...")
self.change_student_modes(*["happy"]*3)
self.wait(2)
class AntipodalEarthPoints(ExternallyAnimatedScene):
pass
class RotatingEarth(ExternallyAnimatedScene):
pass
class TemperaturePressurePlane(GraphScene):
CONFIG = {
"x_labeled_nums" : [],
"y_labeled_nums" : [],
"x_axis_label" : "Temperature",
"y_axis_label" : "Pressure",
"graph_origin" : 2.5*DOWN + 2*LEFT,
"corner_square_width" : 4,
"example_point_coords" : (2, 5),
}
def construct(self):
self.setup_axes()
self.draw_arrow()
self.add_example_coordinates()
self.wander_continuously()
def draw_arrow(self):
square = Square(
side_length = self.corner_square_width,
stroke_color = WHITE,
stroke_width = 0,
)
square.to_corner(UP+LEFT, buff = 0)
arrow = Arrow(
square.get_right(),
self.coords_to_point(*self.example_point_coords)
)
self.play(ShowCreation(arrow))
def add_example_coordinates(self):
dot = Dot(self.coords_to_point(*self.example_point_coords))
dot.set_color(YELLOW)
tex = TexMobject("(25^\\circ\\text{C}, 101 \\text{ kPa})")
tex.next_to(dot, UP+RIGHT, buff = SMALL_BUFF)
self.play(ShowCreation(dot))
self.play(Write(tex))
self.wait()
self.play(FadeOut(tex))
def wander_continuously(self):
path = VMobject().set_points_smoothly([
ORIGIN, 2*UP+RIGHT, 2*DOWN+RIGHT,
5*RIGHT, 4*RIGHT+UP, 3*RIGHT+2*DOWN,
DOWN+LEFT, 2*RIGHT
])
point = self.coords_to_point(*self.example_point_coords)
path.shift(point)
path.set_color(GREEN)
self.play(ShowCreation(path, run_time = 10, rate_func=linear))
self.wait()
class AlternateSphereSquishing(ExternallyAnimatedScene):
pass
class AlternateAntipodalCollision(ExternallyAnimatedScene):
pass
class AskWhy(TeacherStudentsScene):
def construct(self):
self.student_says("But...why?")
self.change_student_modes("pondering", None, "thinking")
self.play(self.get_teacher().change_mode, "happy")
self.wait(3)
class PointOutVSauce(CheckOutMathologer):
CONFIG = {
"channel_name" : "",
"logo_file" : "Vsauce_logo",
"logo_height" : 1,
"logo_color" : GREY,
}
def get_logo(self):
logo = SVGMobject(file_name = self.logo_file)
logo.set_height(self.logo_height)
logo.to_corner(UP+LEFT)
logo.set_stroke(width = 0)
logo.set_fill(GREEN)
logo.sort()
return logo
def get_logo_intro_animation(self, logo):
return DrawBorderThenFill(
logo,
run_time = 2,
)
class WalkEquatorPostTransform(GraphScene):
CONFIG = {
"x_labeled_nums" : [],
"y_labeled_nums" : [],
"graph_origin" : 2.5*DOWN + 2*LEFT,
"curved_arrow_color" : WHITE,
"curved_arrow_radius" : 3,
"num_great_arcs" : 10,
}
def construct(self):
self.setup_axes()
self.add_curved_arrow()
self.great_arc_images = self.get_great_arc_images()
self.walk_equator()
self.walk_tilted_equator()
self.draw_transverse_curve()
self.walk_transverse_curve()
def add_curved_arrow(self):
arc = Arc(
start_angle = 2*np.pi/3, angle = -np.pi/2,
radius = self.curved_arrow_radius,
color = self.curved_arrow_color
)
arc.add_tip()
arc.move_to(self.coords_to_point(0, 7))
self.add(arc)
def walk_equator(self):
equator = self.great_arc_images[0]
dots = VGroup(Dot(), Dot())
dots.set_color(MAROON_B)
dot_movement = self.get_arc_walk_dot_movement(equator, dots)
dot_movement.update(0)
self.play(ShowCreation(equator, run_time = 3))
self.play(FadeIn(dots[0]))
dots[1].set_fill(opacity = 0)
self.play(dot_movement)
self.play(dots[1].set_fill, None, 1)
self.play(dot_movement)
self.play(dot_movement)
proportion = equator.collision_point_proportion
self.play(self.get_arc_walk_dot_movement(
equator, dots,
rate_func = lambda t : 2*proportion*smooth(t)
))
v_line = DashedLine(FRAME_Y_RADIUS*UP, FRAME_Y_RADIUS*DOWN)
v_line.shift(dots.get_center()[0]*RIGHT)
self.play(ShowCreation(v_line))
self.wait()
self.play(FadeOut(v_line))
dots.save_state()
equator.save_state()
self.play(
equator.fade,
dots.fade
)
self.first_dots = dots
def walk_tilted_equator(self):
equator = self.great_arc_images[0]
tilted_eq = self.great_arc_images[1]
dots = VGroup(Dot(), Dot())
dots.set_color(MAROON_B)
dot_movement = self.get_arc_walk_dot_movement(tilted_eq, dots)
dot_movement.update(0)
self.play(ReplacementTransform(equator.copy(), tilted_eq))
self.wait()
self.play(FadeIn(dots))
self.play(dot_movement)
proportion = tilted_eq.collision_point_proportion
self.play(self.get_arc_walk_dot_movement(
tilted_eq, dots,
rate_func = lambda t : 2*proportion*smooth(t)
))
v_line = DashedLine(FRAME_Y_RADIUS*UP, FRAME_Y_RADIUS*DOWN)
v_line.shift(dots.get_center()[0]*RIGHT)
self.play(ShowCreation(v_line))
self.wait()
self.play(FadeOut(v_line))
self.play(*list(map(FadeOut, [tilted_eq, dots])))
def draw_transverse_curve(self):
transverse_curve = self.get_transverse_curve(self.great_arc_images)
dots = self.first_dots
equator = self.great_arc_images[0]
self.play(dots.restore)
equator.restore()
self.great_arc_images.fade()
target_arcs = list(self.great_arc_images[1:])
target_dots = []
for arc in target_arcs:
new_dots = dots.copy()
for dot, point in zip(new_dots, arc.x_collision_points):
dot.move_to(point)
target_dots.append(new_dots)
alt_eq = equator.copy()
alt_eq.points = np.array(list(reversed(alt_eq.points)))
alt_dots = dots.copy()
alt_dots.submobjects.reverse()
target_arcs += [alt_eq, alt_eq.copy()]
target_dots += [alt_dots, alt_dots.copy()]
equator_transform = Succession(*[
Transform(equator, arc, rate_func=linear)
for arc in target_arcs
])
dots_transform = Succession(*[
Transform(dots, target, rate_func=linear)
for target in target_dots
])
self.play(
ShowCreation(transverse_curve, lag_ratio = 0),
equator_transform,
dots_transform,
run_time = 10,
rate_func=linear,
)
self.wait(2)
def walk_transverse_curve(self):
transverse_curve = self.get_transverse_curve(self.great_arc_images)
dots = self.first_dots
def dot_update(dots, alpha):
for dot, curve in zip(dots, transverse_curve):
dot.move_to(curve.point_from_proportion(alpha))
return dots
for x in range(2):
self.play(
UpdateFromAlphaFunc(dots, dot_update),
run_time = 4
)
self.play(
UpdateFromAlphaFunc(dots, dot_update),
run_time = 4,
rate_func = lambda t : 0.455*smooth(t)
)
self.play(
dots.set_color, YELLOW,
dots.scale_in_place, 1.2,
rate_func = there_and_back
)
self.wait()
#######
def get_arc_walk_dot_movement(self, arc, dots, **kwargs):
def dot_update(dots, alpha):
dots[0].move_to(arc.point_from_proportion(0.5*alpha))
dots[1].move_to(arc.point_from_proportion(0.5+0.5*alpha))
return dots
if "run_time" not in kwargs:
kwargs["run_time"] = 5
return UpdateFromAlphaFunc(dots, dot_update, **kwargs)
def sphere_to_plane(self, point):
x, y, z = point
return np.array([
x - 2*x*z + y + 1,
y+0.5*y*np.cos(z*np.pi),
0
])
def sphere_point(self, portion_around_equator, equator_tilt = 0):
theta = portion_around_equator*2*np.pi
point = np.cos(theta)*RIGHT + np.sin(theta)*UP
phi = equator_tilt*np.pi
return rotate_vector(point, phi, RIGHT)
def get_great_arc_images(self):
curves = VGroup(*[
ParametricFunction(
lambda t : self.sphere_point(t, s)
).apply_function(self.sphere_to_plane)
for s in np.arange(0, 1, 1./self.num_great_arcs)
# for s in [0]
])
curves.set_color(YELLOW)
curves[0].set_color(RED)
for curve in curves:
antipodal_x_diff = lambda x : \
curve.point_from_proportion(x+0.5)[0]-\
curve.point_from_proportion(x)[0]
last_x = 0
last_sign = np.sign(antipodal_x_diff(last_x))
for x in np.linspace(0, 0.5, 100):
sign = np.sign(antipodal_x_diff(x))
if sign != last_sign:
mean = np.mean([last_x, x])
curve.x_collision_points = [
curve.point_from_proportion(mean),
curve.point_from_proportion(mean+0.5),
]
curve.collision_point_proportion = mean
break
last_x = x
last_sign = sign
return curves
def get_transverse_curve(self, gerat_arc_images):
points = list(it.chain(*[
[
curve.x_collision_points[i]
for curve in gerat_arc_images
]
for i in (0, 1)
]))
full_curve = VMobject(close_new_points = True)
full_curve.set_points_smoothly(points + [points[0]])
full_curve.set_color(MAROON_B)
first_half = full_curve.copy().pointwise_become_partial(
full_curve, 0, 0.5
)
second_half = first_half.copy().rotate_in_place(np.pi, RIGHT)
broken_curve = VGroup(first_half, second_half)
return broken_curve
class WalkAroundEquatorPreimage(ExternallyAnimatedScene):
pass
class WalkTiltedEquatorPreimage(ExternallyAnimatedScene):
pass
class FormLoopTransverseToEquator(ExternallyAnimatedScene):
pass
class AntipodalWalkAroundTransverseLoop(ExternallyAnimatedScene):
pass
class MentionGenerality(TeacherStudentsScene, ThreeDScene):
def construct(self):
necklace = Necklace(width = FRAME_X_RADIUS)
necklace.shift(2*UP)
necklace.to_edge(RIGHT)
arrow = TexMobject("\\Leftrightarrow")
arrow.scale(2)
arrow.next_to(necklace, LEFT)
q_marks = TexMobject("???")
q_marks.next_to(arrow, UP)
arrow.add(q_marks)
formula = TexMobject("f(\\textbf{x}) = f(-\\textbf{x})")
formula.next_to(self.get_students(), UP, buff = LARGE_BUFF)
formula.to_edge(LEFT, buff = LARGE_BUFF)
self.play(
self.teacher.change_mode, "raise_right_hand",
self.teacher.look_at, arrow
)
self.play(
FadeIn(necklace, run_time = 2, lag_ratio = 0.5),
Write(arrow),
*[
ApplyMethod(pi.look_at, arrow)
for pi in self.get_pi_creatures()
]