Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from manimlib.imports import *
- import os
- from scipy import special
- SVG_IMAGE_DIR = "E://Projekty//Kodowe//manim"
- class Shapes(Scene):
- def construct(self):
- circle = Circle(color=PURPLE_A)
- square = Square(fill_color=GOLD_B, fill_opacity=1, color=GOLD_A)
- square.move_to(UP + LEFT)
- circle.surround(square)
- rectangle = Rectangle(height=2, width=3)
- ellipse = Ellipse(width=3, height=1, color=RED)
- ellipse.shift(2 * DOWN + 2 * RIGHT)
- pointer = CurvedArrow(2 * RIGHT, 5 * RIGHT, color=MAROON_C)
- arrow = Arrow(LEFT, UP)
- arrow.next_to(circle, DOWN + LEFT)
- rectangle.next_to(arrow, DOWN + LEFT)
- ring = Annulus(inner_radius=.5, outer_radius=1, color=BLUE)
- ring.next_to(ellipse, RIGHT)
- self.add(pointer)
- self.play(FadeIn(square))
- self.play(Rotating(square), FadeIn(circle))
- self.play(GrowArrow(arrow))
- self.play(GrowFromCenter(rectangle), GrowFromCenter(ellipse), GrowFromCenter(ring))
- class Key(SVGMobject):
- CONFIG = {
- "color": GOLD,
- "file_name_prefix": "key2",
- "stroke_color": WHITE,
- "fill_opacity": 1.0,
- "height": .5,
- }
- def __init__(self, mode="plain", **kwargs):
- digest_config(self, kwargs)
- self.mode = mode
- self.parts_named = False
- try:
- svg_file = os.path.join(
- SVG_IMAGE_DIR,
- "%s_%s.svg" % (self.file_name_prefix, mode)
- )
- SVGMobject.__init__(self, file_name=svg_file, **kwargs)
- except:
- warnings.warn("No %s design with mode %s" %
- (self.file_name_prefix, mode))
- svg_file = os.path.join(
- SVG_IMAGE_DIR,
- "key2.svg",
- )
- SVGMobject.__init__(self, mode="plain", file_name=svg_file, **kwargs)
- class Letter(SVGMobject):
- CONFIG = {
- # "color": WHITE,
- "file_name_prefix": "envelope",
- "stroke_color": WHITE,
- "fill_opacity": 1.0,
- "height": 1,
- }
- def __init__(self, mode="plain", **kwargs):
- digest_config(self, kwargs)
- self.mode = mode
- self.parts_named = False
- try:
- svg_file = os.path.join(
- SVG_IMAGE_DIR,
- "%s_%s.svg" % (self.file_name_prefix, mode)
- )
- SVGMobject.__init__(self, file_name=svg_file, **kwargs)
- except:
- warnings.warn("No %s design with mode %s" %
- (self.file_name_prefix, mode))
- svg_file = os.path.join(
- SVG_IMAGE_DIR,
- "envelope.svg",
- )
- SVGMobject.__init__(self, mode="plain", file_name=svg_file, **kwargs)
- class Crypt(Scene):
- def construct(self):
- letter1 = Letter()
- letter2 = Letter()
- letter1.shift(4 * LEFT)
- letter2.shift(4 * RIGHT)
- ellipse = Ellipse(width=1, height=.5, color=WHITE)
- ellipse.shift(4 * LEFT)
- alice = TextMobject("Alice")
- alice.next_to(letter1, LEFT)
- bob = TextMobject("Bob")
- bob.next_to(letter2, RIGHT)
- key = Key()
- key2 = Key()
- key.scale(2)
- key.shift(3 * UP)
- # key.shift(3.5 * LEFT + 1 * UP)
- # key2.shift(3.5 * RIGHT + 1 * UP)
- key2.scale(2)
- key2.shift(3 * UP)
- # start of anim
- self.add(letter1, alice, bob)
- self.wait()
- # show key, and divide to both
- self.play(FadeIn(key))
- self.play(FadeIn(key2))
- self.play(ApplyMethod(key.move_to, 3.5 * LEFT + 1 * UP), ApplyMethod(key2.move_to, 3.5 * RIGHT + 1 * UP))
- self.play(FadeOut(key2))
- key2.scale(.5)
- self.play(ApplyMethod(key.scale, .5))#, ApplyMethod(key2.scale, .5))
- # incrypt
- self.wait()
- self.play(ApplyMethod(key.shift, 1 * DOWN))
- self.play(FadeOut(key))
- self.play(Transform(letter1, ellipse))
- # send
- self.wait()
- self.play(ApplyMethod(letter1.move_to, 4 * RIGHT))
- # decrypt
- self.play(FadeIn(key2))
- self.play(ApplyMethod(key2.shift, 1 * DOWN))
- self.play(FadeOut(key2))
- self.play(Transform(letter1, letter2))
- self.wait()
- class Generating(Scene):
- def construct(self):
- arrow1 = Arrow(4 * LEFT, LEFT)
- arrow2 = Arrow(RIGHT, 3 * RIGHT)
- arrow3 = Arrow(2 * UP, .5 * UP)
- funct = TextMobject("AES")
- funct.scale(2)
- # seed = TextMobject("seed")
- # seed.shift(4*LEFT)
- # types of input
- input1 = TextMobject("Ala ma kota")
- input1.next_to(arrow1, LEFT)
- input3 = TextMobject("tajnehaslo123456")
- input3.next_to(arrow3, UP)
- input2 = TextMobject("Z6kwuEWmquIx")
- input2.next_to(arrow2, RIGHT)
- input21 = TextMobject("7kmn3fUKqw==")
- input21.next_to(input2, DOWN)
- key = Key()
- key.next_to(input3, UP)
- key.scale(.7)
- ellipse = Ellipse(width=1, height=.5, color=WHITE)
- ellipse.next_to(input2, UP)
- letter = Letter()
- letter.scale(.7)
- letter.next_to(input1, UP)
- # arrows go wild
- self.add(funct)
- self.play(Write(input1), Write(input3), GrowFromCenter(key), GrowFromCenter(letter))
- self.play(GrowArrow(arrow1))
- self.play(GrowArrow(arrow3))
- self.wait()
- self.play(GrowArrow(arrow2))
- self.play(Write(input2), Write(input21), GrowFromCenter(ellipse))
- self.wait()
- self.wait()
- # next part
- # remove all obj
- self.play(FadeOut(input1), FadeOut(input3), FadeOut(input2), FadeOut(letter), FadeOut(ellipse),
- FadeOut(arrow1), FadeOut(arrow2), FadeOut(arrow3), FadeOut(funct), FadeOut(input21))
- self.play(ApplyMethod(key.scale, 2))
- self.wait()
- # part about random gens
- text_main = TextMobject("pseudo-random number generator")
- text_main.shift(3*RIGHT)
- text_left = TextMobject("cryptographically secure")
- text_left.next_to(text_main, LEFT)
- text_left.set_color(BLUE)
- text_shrt_main = TextMobject("PRNG")
- text_shrt_main.shift(.5 * RIGHT)
- text_shrt_left = TextMobject("CS")
- text_shrt_main.set_color(WHITE)
- text_shrt_left.set_color(BLUE)
- text_shrt_left.next_to(text_shrt_main, LEFT)
- text_time = TextMobject("aktualny czas")
- text_time.shift(2*DOWN)
- # play anim
- self.play(Write(text_main))
- self.wait()
- self.play(Write(text_time))
- self.wait()
- self.play(FadeOut(text_time))
- self.wait()
- self.play(Write(text_left))
- self.wait()
- self.play(Transform(text_main, text_shrt_main), Transform(text_left, text_shrt_left))
- self.wait()
- self.play(FadeOut(text_main), FadeOut(text_left), FadeOut(key))
- self.wait()
- class Conditions(Scene):
- def construct(self):
- text_cond1 = TextMobject("-Correlated")
- text_cond1.shift(2 * UP + 4 * LEFT)
- text_expl1 = TextMobject("lorem ipsum")
- text_expl1.scale(.7)
- text_expl1.set_color(RED)
- text_expl1.next_to(text_cond1.get_corner(DOWN + RIGHT), DOWN)
- text_expl1.align_to(text_cond1, LEFT)
- text_cond2 = TextMobject("-Przewidywalnosc")
- text_cond2.align_to(text_cond1, LEFT)
- text_expl21 = TextMobject("przy znajomosci n-1 bitow, prawdopodobienstwo zgadniecia")
- text_expl22 = TextMobject("poprawnego bitu musi wynosic 0.5")
- text_expl21.scale(.7)
- text_expl22.scale(.7)
- text_expl21.set_color(RED)
- text_expl22.set_color(RED)
- text_expl21.next_to(text_cond2.get_corner(DOWN+RIGHT), DOWN)
- text_expl22.next_to(text_expl21.get_corner(DOWN+LEFT), DOWN)
- text_expl21.align_to(text_cond2, LEFT)
- text_expl22.align_to(text_cond2, LEFT)
- text_cond3 = TextMobject("-Balans")
- text_cond3.shift(2 * DOWN)
- text_cond3.align_to(text_cond1, LEFT)
- text_expl3 = TextMobject("na kazdy bit zrodla powinien przypadac jeden wygenerowany")
- text_expl3.scale(.7)
- text_expl3.set_color(RED)
- text_expl3.next_to(text_cond3.get_corner(DOWN + RIGHT), DOWN)
- text_expl3.align_to(text_cond3, LEFT)
- self.play(Write(text_cond1))
- self.wait(2)
- self.play(Write(text_expl1))
- self.play(Write(text_cond2))
- self.wait(2)
- self.play(Write(text_expl21), Write(text_expl22))
- self.play(Write(text_cond3))
- self.wait(2)
- self.play(Write(text_expl3))
- self.wait()
- class Test1(Scene):
- def construct(self):
- # create data
- n = 100
- seq = "1111101010110111111100001011010001111000111011100100100011100010011100011001000000010000100011101100"
- seq_post = ''
- for i in seq:
- if i == '1':
- seq_post += '+1'
- else:
- seq_post += '-1'
- seq_splited = [seq_post[i:i + 2] for i in range(0, len(seq_post), 2)]
- s = 0
- for i in seq_splited:
- if i[0] == '+':
- s += 1
- else:
- s -= 1
- # code definition
- s_code = "\\usepackage{pythonhighlight}\n" \
- "\\begin{python}\n"\
- "s = \'\'\n" \
- "for x in range(10):\n" \
- " for _ in range(100):\n" \
- " s += str(random.randint(0, 1))\n" \
- " print(s)\n" \
- "\\end{python}"
- # t_code = TextMobject(s_code)
- # hipothesis definition
- t_hip0 = TextMobject("$H_{0}:$ Sekwencja binarna jest losowa")
- t_hipa = TextMobject("$H_{a}:$ Sekwencja binarna nie jest losowa")
- t_alpha = TextMobject("$\\alpha = 0.01$")
- # text created
- text_seq = TextMobject(seq)
- text_seq.scale(.5)
- text_seq_post = TextMobject(seq_post)
- text_seq_post.scale(.5)
- text_s = TextMobject(str(s) + ' = ')
- # text_s.scale(.5)
- text_s.next_to(text_seq_post, LEFT)
- # animation
- # self.play(Write(t_code))
- # self.wait()
- # self.play(FadeOut(t_code))
- # TODO add intro to hipothesis
- self.play(Write(text_seq))
- self.wait()
- self.play(Transform(text_seq, text_seq_post))
- self.wait()
- self.play(Write(text_s))
- self.wait()
- self.play(FadeOut(text_seq), FadeOut(text_s))
- self.wait()
- # hipothesis test
- t1 = TextMobject("$ S_{obs} = \\frac{| S_{n} |}{\\sqrt{n}} $")
- s1 = abs(s)/pow(n, 1/2)
- t2 = TextMobject("$ %f = \\frac{| %d |}{\\sqrt{%d}} $" % (s1, s, n))
- p_value = special.erfc(s1/pow(2, 1/2))
- t3 = TextMobject("$ P-value = \\textbf{erfc}\\left(\\frac{S_{obs}}{\\sqrt{2}}\\right) $")
- t4 = TextMobject("$ %f = \\textbf{erfc}\\left(\\frac{ %f }{\\sqrt{2}}\\right) $" % (p_value, s1))
- t_alpha_result = TextMobject("$\\alpha = 0.01 \\implies P-value > 0.01$ ")
- t_alpha_result.shift(DOWN)
- t_alpha_result2 = TextMobject("$\\alpha = 0.01 \\implies %f > 0.01$ " % (p_value))
- t_alpha_result2.shift(DOWN)
- test1_title = TextMobject("\"Frequency (Monobit)\" Test:")
- test1_title.scale(0.5)
- test1_title.to_edge(UP+LEFT)
- test1_title.set_color(ORANGE)
- # ...
- self.play(Write(test1_title))
- self.play(Write(t1))
- self.wait(3)
- self.play(Transform(t1, t2))
- self.wait()
- self.play(ApplyMethod(t1.move_to, 2 * UP))
- self.play(Write(t3))
- self.wait(2)
- self.play(Transform(t3, t4))
- self.wait(2)
- self.play(ApplyMethod(t3.move_to, UP))
- self.play(Write(t_alpha_result))
- self.wait(2)
- self.play(Transform(t_alpha_result, t_alpha_result2))
- self.wait(2)
- self.play(ApplyMethod(t_alpha_result.move_to, UP + DOWN))
- self.wait(2)
- # conclusion
- t_hip0.move_to(DOWN)
- t_hipa.next_to(t_hip0, DOWN)
- self.play(Write(t_hip0))
- self.play(Write(t_hipa))
- self.wait()
- self.play(ApplyMethod(t_hip0.set_color, GREEN), ApplyMethod(t_hipa.set_color, RED))
- self.wait(2)
- F_ans_test2 = 0.25
- class Test2(Scene):
- @staticmethod
- def propotion(block: str) -> float:
- return block.count('1')/len(block)
- def construct(self):
- n = 100
- M = 10
- seq = "0100100100000011001011110101010011000001101101000000011111101001101111011001011101111110000111000010"
- seq_block = [seq[i:i + 10] for i in range(0, len(seq), 10)]
- t_name = TextMobject("Frequency Test within a Block:")
- t_name.scale(0.5)
- t_name.to_edge(UP + LEFT)
- t_name.set_color(ORANGE)
- t_n = TextMobject("$n = %d$" % n)
- t_n.scale(0.7)
- t_n.shift(5 * LEFT + 2 * UP)
- t_M = TextMobject("$M = %d$" % M)
- t_M.scale(0.7)
- t_M.next_to(t_n, DOWN)
- t_seq = TextMobject(seq)
- t_seq.scale(0.5)
- t_seq_block = TextMobject(str(seq_block))
- t_seq_block.scale(0.7)
- t_epsilon = TextMobject("$\\varepsilon_{i} = $")
- t_epsilon.next_to(t_seq_block, LEFT)
- # animation
- self.play(Write(t_name))
- self.wait()
- self.play(Write(t_seq), Write(t_n))
- self.wait(2)
- self.play(Transform(t_seq, t_seq_block), Write(t_epsilon), Write(t_M))
- self.wait(2)
- # todo fade out
- # calculations
- t_pi = TextMobject("$\\pi_{i} = \\frac{\\displaystyle\\sum_{j=1}^{M} "
- "\\varepsilon_{(i-1)M+j}}{M}$")
- t_pi.shift(2 * UP)
- f_seq = []
- for i in seq_block:
- f_seq.append(self.propotion(i))
- t_pi_seq = TextMobject(str(f_seq))
- t_pi_i = TextMobject("$\\pi_{i} = $")
- t_pi_i.next_to(t_pi_seq, LEFT)
- # animation calculations
- self.play(Write(t_pi))
- self.wait()
- self.play(Transform(t_seq, t_pi_seq), Transform(t_epsilon, t_pi_i))
- self.wait(2)
- # count chi^2
- t_chi_formula = TextMobject("$\\chi^{2}(obs) = 4 M \\displaystyle"
- "\\sum_{i=1}^{N}(\\pi_{i}-^1/_2)^{2}$")
- t_chi_formula.shift(2 * DOWN)
- N = 10
- t_chi_text_chi_only = TextMobject("$\\chi^{2}(obs) = $")
- s_chi_text = "$ 4 \\times %d (" % N
- f_real_ans = 0
- for index, i in enumerate(f_seq):
- f_real_ans += pow(i - .5, 2)
- s_chi_text += "(%f - 0.5)^{2}" % i
- if index < N:
- s_chi_text += "+"
- s_chi_text += ")$"
- global F_ans_test2
- F_ans_test2 = f_real_ans
- t_chi_ans = TextMobject("$ = %f $" % f_real_ans)
- t_chi_text = TextMobject(s_chi_text)
- t_chi_text.shift(2 * DOWN)
- t_chi_text.scale(.5)
- t_chi_text_chi_only.next_to(t_chi_text, LEFT)
- t_chi_ans.next_to(t_chi_text, RIGHT)
- self.play(Write(t_chi_formula))
- self.wait()
- self.play(Transform(t_chi_formula, t_chi_text), Write(t_chi_text_chi_only),
- Write(t_chi_ans))
- self.wait(2)
- self.play(FadeOut(t_chi_formula), FadeOut(t_chi_text_chi_only),
- FadeOut(t_chi_ans), FadeOut(t_seq), FadeOut(t_epsilon),
- FadeOut(t_n), FadeOut(t_M), FadeOut(t_pi))
- self.wait()
- class Test2_part2(Scene):
- def construct(self):
- t_pvalue_formula = TextMobject("$ P-value = \\textbf{igamc}( ^N/_2, \\frac{\\chi^{2}(obs)}{2})$")
- global F_ans_test2
- N = 10
- s_pvalue = "2.29\\times10^{-07}"
- print(special.gammainc(N/2, F_ans_test2/2))
- # STATIC VALUE BASED ON CALC!
- t_pvalue_ans = TextMobject("$ %s \\approx \\textbf{igamc}( %f , %f ) $" % (s_pvalue, N/2, F_ans_test2/2))
- t_name = TextMobject("Frequency Test within a Block:")
- t_name.scale(0.5)
- t_name.to_edge(UP + LEFT)
- t_name.set_color(ORANGE)
- self.add(t_name)
- self.play(Write(t_pvalue_formula))
- self.wait(2)
- self.play(Transform(t_pvalue_formula, t_pvalue_ans))
- self.wait(4)
- self.play(ApplyMethod(t_pvalue_formula.shift, UP))
- # hipothesis
- t_alpha_result = TextMobject("$\\alpha = 0.01 \\implies P-value < 0.01$ ")
- # t_alpha_result.shift(DOWN)
- t_alpha_result2 = TextMobject("$\\alpha = 0.01 \\implies %s < 0.01$ " % (s_pvalue))
- # t_alpha_result2.shift(DOWN)
- t_hip0 = TextMobject("$H_{0}:$ Sekwencja binarna jest losowa")
- t_hipa = TextMobject("$H_{a}:$ Sekwencja binarna nie jest losowa")
- t_hip0.move_to(DOWN)
- t_hipa.next_to(t_hip0, DOWN)
- self.play(Write(t_alpha_result))
- self.wait(2)
- self.play(Transform(t_alpha_result, t_alpha_result2))
- self.wait(2)
- self.play(ApplyMethod(t_alpha_result.move_to, UP + DOWN))
- self.wait()
- self.play(Write(t_hip0))
- self.play(Write(t_hipa))
- self.wait()
- self.play(ApplyMethod(t_hip0.set_color, RED), ApplyMethod(t_hipa.set_color, GREEN))
- self.wait(2)
- class Test_my_code(Scene):
- def construct(self):
- s1 = 2.123123
- s = -2
- n = 100
- t1 = TextMobject("$ S_{obs} = \\frac{| S_{n} |}{\\sqrt{n}} $")
- t2 = TextMobject("$ %f = \\frac{| %d |}{\\sqrt{%d}} $" % (s1, s, n))
- t3 = TextMobject("$ P-value = \\textbf{erfc}\\left(\\frac{S_{obs}}{\\sqrt{2}}\\right) $")
- t_chi_formula = TextMobject("$\\chi^{2}(obs) = 4 M \\displaystyle"
- "\\sum_{i=1}^{N}(\\pi_{i}- ^1/_2)^{2}$")
- t_pi_seq = [0.3, 0.2, 0.4, 0.1]
- for index, i in enumerate(t_pi_seq):
- print(type(i))
- self.add(t_chi_formula)
- self.wait(4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement