Created
October 9, 2024 01:08
-
-
Save Elteoremadebeethoven/37f95e40dd8c48cd20533af16f93c339 to your computer and use it in GitHub Desktop.
Code from my Short "Sqrt(2) is irrational".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from manim import * | |
config.disable_caching = True | |
# Change default font size | |
DEFAULT_FONT_SIZE = 28 | |
MathTex.set_default(font_size=DEFAULT_FONT_SIZE) | |
Tex.set_default(font_size=DEFAULT_FONT_SIZE) | |
SCALE_FACTOR = 1 | |
tmp_pixel_height = config.pixel_height | |
config.pixel_height = config.pixel_width | |
config.pixel_width = tmp_pixel_height | |
config.frame_height = config.frame_height / SCALE_FACTOR | |
config.frame_width = config.frame_height * 9 / 16 | |
class MainScene(Scene): | |
def align_tex_with_symbol(self, t1, t2): | |
""" | |
Align t2 with one symbol of t1 | |
""" | |
tex1, index1 = t1 | |
tex2, index2 = t2 | |
ref1 = tex1[index1].get_center() | |
ref2 = tex2[index2].get_center() | |
vector = ref1 - ref2 | |
tex2_y = tex2.get_y() | |
tex2.shift(vector).set_y(tex2_y) | |
def construct(self): | |
title = Tex("Suppose ", | |
"$\\displaystyle\\sqrt{2} = \\frac{p}{q}$", | |
"is an irreducible", "fraction") | |
title[-2:].arrange(DOWN, aligned_edge=LEFT, buff=0.1) | |
title[-2:].next_to(title[1], RIGHT, buff=0.2) | |
title.set_x(0).to_edge(UP) | |
equations = [ | |
# 0 1 2 3 | |
MathTex("\\sqrt{2}","q","=","p"), | |
# 0 1 2 3 4 5 6 | |
MathTex("\\Rightarrow","(","\\sqrt{2}q",")^2","=","p","^2"), | |
# 0 1 2 3 4 5 6 | |
MathTex("\\Rightarrow","2","q","^2","=","p","^2"), | |
] | |
eq_grp1 = VGroup(*equations)\ | |
.arrange(DOWN, aligned_edge=LEFT, buff=0.1)\ | |
.next_to(title, DOWN) | |
conc1 = Tex("If $p^2$ is even then $p$ is even")\ | |
.next_to(eq_grp1, DOWN) | |
_because = Tex("Because:").next_to(conc1, DOWN) | |
odd_proof = VGroup( | |
# 0 1 2 3 4 5 6 7 8, 9 10 | |
MathTex("(2k-1)^2","=","4","k","^","2","-","4","k","+1"), | |
# 0 1 2 3 4 5 6 7 8 | |
MathTex("=","4","k","(","k","-","1",")","+1"), | |
).arrange(DOWN, aligned_edge=LEFT).next_to(_because, DOWN) | |
odd_proof[1].align_to(odd_proof[0][1], LEFT) | |
generic_odd_brace = Brace(odd_proof[0][0], DOWN, buff=0.05) | |
generic_odd_tex = generic_odd_brace.get_text(r"generic\\odd\\number", buff=0.05) | |
generic_odd_tex.scale(0.7, about_point=generic_odd_tex.get_top()) | |
_4kp1_brace = Brace(odd_proof[1][1:8], DOWN, buff=0)\ | |
.scale([1.1,1,1]) | |
_4kp1_tex = _4kp1_brace.get_text("even", buff=0.05) | |
# _4kp1_tex.scale(0.8, about_point=_4kp1_tex.get_top()) | |
_1 = odd_proof[1][-1].copy().next_to(_4kp1_tex, RIGHT) | |
_1.align_to(odd_proof[1][-1], LEFT) | |
_1.align_to(_4kp1_tex, DOWN) | |
odd_brace = Brace(VGroup(_4kp1_tex, _1), DOWN, buff=0)\ | |
.scale([1.1,1,1]) | |
odd_tex = odd_brace.get_text("odd", buff=0.05) | |
logic_proof = VGroup( | |
# 0 1 2 3 | |
MathTex("{\\rm odd}(n)","\\to","{\\rm odd}(n^2)"), | |
# 0 1 2 3 | |
MathTex("\\Leftrightarrow","\\neg","{\\rm odd}(n^2)","\\to","\\neg","{\\rm odd}(n)"), | |
# 0 1 2 3 | |
MathTex("\\Leftrightarrow","{\\rm even}(n^2)","\\to","{\\rm even}(n)"), | |
Tex("$\\therefore p$ is even"), | |
).arrange(DOWN, aligned_edge=LEFT, buff=0.1)\ | |
.next_to(odd_proof, DOWN, buff=1.2) | |
q_contradiction = VGroup( | |
# 0 1 2 3 4 5 6 7 8 | |
MathTex("\\Rightarrow", "2", "q^2", "=", "(", "2", "k" ,")", "^2"), | |
MathTex("\\Rightarrow", "2", "q^2", "=", "4", "k", "^2"), | |
# 0 1 2 3 4 5 | |
MathTex("\\Rightarrow","q^2", "=", "2", "k", "^2"), | |
MathTex("\\therefore\\,\\text{even}(q^2)\\to\\text{even}(q)"), | |
MathTex("\\Rightarrow \\sqrt{2} = \\frac{2k}{2m}"), | |
).arrange(DOWN, aligned_edge=LEFT, buff=0.1)\ | |
.next_to(logic_proof, DOWN, buff=0.1) | |
adm_sign = MathTex("!", stroke_width=3, stroke_opacity=1).scale(2)\ | |
.next_to(q_contradiction[-1], RIGHT)\ | |
.set_color(RED) | |
# --------- Aligns | |
_because.align_to(title, LEFT) | |
self.align_tex_with_symbol((eq_grp1[0],2), (eq_grp1[1], 4)) | |
self.align_tex_with_symbol((eq_grp1[0],2), (eq_grp1[2], 4)) | |
eq_grp1[2][0].align_to(eq_grp1[1][0], LEFT) | |
self.align_tex_with_symbol((logic_proof[0], 2), (logic_proof[1], 3)) | |
self.align_tex_with_symbol((logic_proof[0], 2), (logic_proof[2], 2)) | |
logic_proof[2][0].align_to(logic_proof[1][0], LEFT) | |
logic_proof.set_x(0) | |
self.align_tex_with_symbol((q_contradiction[0], 3), (q_contradiction[2], 2)) | |
q_contradiction[2][0].align_to(q_contradiction[1][0], LEFT) | |
_therefore = MathTex("\\therefore")\ | |
.next_to(logic_proof[0], LEFT, buff=0.1) | |
_logic_proof_1 = VGroup(_therefore, logic_proof[0]).set_x(0) | |
logic_proof[-1].align_to(_logic_proof_1, LEFT) | |
fade_group = VGroup( | |
_because, | |
odd_proof, | |
logic_proof[:-1], | |
generic_odd_brace, | |
generic_odd_tex, | |
_1, odd_brace, odd_tex, | |
_4kp1_tex, _4kp1_brace, | |
_therefore | |
) | |
shift_grp = VGroup( | |
# logic_proof, | |
q_contradiction, | |
adm_sign | |
) | |
shift_grp.shift(UP * fade_group.height) | |
# ------- Animations | |
self.play(Write(title)) | |
self.wait() | |
self.play( | |
TransformFromCopy(title[1][:3], eq_grp1[0][0][:]), # sqrt(2) | |
TransformFromCopy(title[1][-1], eq_grp1[0][1]), # q | |
TransformFromCopy(title[1][3], eq_grp1[0][2]), # = | |
TransformFromCopy(title[1][4], eq_grp1[0][3]), # p | |
) | |
self.play( | |
TransformMatchingShapes(eq_grp1[0].copy(), eq_grp1[1]), | |
) | |
self.play( | |
TransformMatchingShapes(eq_grp1[1].copy(), eq_grp1[2]), | |
) | |
self.play(Write(conc1)) | |
self.wait() | |
self.play(Write(_because)) | |
self.play(Write(odd_proof[0][0])) | |
self.play( | |
GrowFromCenter(generic_odd_brace), | |
FadeIn(generic_odd_tex), | |
) | |
self.wait() | |
self.play(Write(odd_proof[0][1:])) | |
self.play( | |
TransformMatchingShapes(odd_proof[0][1:-1].copy(), odd_proof[1][:-1]), | |
TransformFromCopy(odd_proof[0][-1], odd_proof[1][-1]) | |
) | |
self.play( | |
GrowFromCenter(_4kp1_brace), | |
FadeIn(_4kp1_tex), | |
) | |
self.play( | |
TransformFromCopy(odd_proof[1][-1], _1) | |
) | |
self.play( | |
GrowFromCenter(odd_brace), | |
FadeIn(odd_tex), | |
) | |
self.play(Write(_logic_proof_1)) | |
self.wait() | |
self.play( | |
TransformMatchingTex(logic_proof[0].copy(), logic_proof[1]), | |
) | |
self.play( | |
*[ | |
ReplacementTransform(logic_proof[1][i].copy(), logic_proof[2][j]) | |
for i,j in [ | |
(0, 0), | |
(2, 1), | |
(3, 2), | |
(5, 3), | |
] | |
], | |
run_time=3 | |
) | |
self.play( | |
Indicate(conc1), | |
Indicate(logic_proof[-2]), | |
run_time=2.5 | |
) | |
self.play(Write(logic_proof[-1])) | |
self.wait() | |
# --- shift group | |
self.play( | |
FadeOut(fade_group), | |
logic_proof[-1].animate.shift(UP * fade_group.height) | |
) | |
self.wait() | |
self.play(Write(q_contradiction[0])) | |
self.play( | |
TransformMatchingTex(q_contradiction[0].copy(), q_contradiction[1]), | |
) | |
self.play( | |
TransformFromCopy(q_contradiction[1][0], q_contradiction[2][0]), | |
TransformFromCopy(q_contradiction[1][2:], q_contradiction[2][1:]), | |
) | |
self.play(Write(q_contradiction[3])) | |
self.wait() | |
self.play(Write(q_contradiction[4])) | |
self.wait() | |
self.play(FadeIn(adm_sign, scale=9)) | |
self.play( | |
Indicate(title[-2:], color=RED), | |
Indicate(q_contradiction[4], color=RED), | |
run_time=2 | |
) | |
self.wait() | |
self.play(FadeOut(*self.mobjects)) | |
self.play( | |
Write(Tex("$\\therefore\\,\\sqrt{2}$ is irrational")), | |
run_time=1 | |
) | |
self.wait(2) | |
# self.add( | |
# title, *eq_grp1, conc1, _because, odd_proof, | |
# generic_odd_brace, | |
# generic_odd_tex, | |
# _4kp1_brace, _4kp1_tex, _1, | |
# odd_brace, odd_tex, | |
# *logic_proof, | |
# *q_contradiction, | |
# adm_sign | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment