Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.57 KB | None | 0 0
  1. from manimlib.imports import *
  2. import os
  3. from scipy import special
  4.  
  5. SVG_IMAGE_DIR = "E://Projekty//Kodowe//manim"
  6.  
  7.  
  8. class Shapes(Scene):
  9. def construct(self):
  10. circle = Circle(color=PURPLE_A)
  11. square = Square(fill_color=GOLD_B, fill_opacity=1, color=GOLD_A)
  12. square.move_to(UP + LEFT)
  13. circle.surround(square)
  14. rectangle = Rectangle(height=2, width=3)
  15. ellipse = Ellipse(width=3, height=1, color=RED)
  16. ellipse.shift(2 * DOWN + 2 * RIGHT)
  17. pointer = CurvedArrow(2 * RIGHT, 5 * RIGHT, color=MAROON_C)
  18. arrow = Arrow(LEFT, UP)
  19. arrow.next_to(circle, DOWN + LEFT)
  20. rectangle.next_to(arrow, DOWN + LEFT)
  21. ring = Annulus(inner_radius=.5, outer_radius=1, color=BLUE)
  22. ring.next_to(ellipse, RIGHT)
  23.  
  24. self.add(pointer)
  25. self.play(FadeIn(square))
  26. self.play(Rotating(square), FadeIn(circle))
  27. self.play(GrowArrow(arrow))
  28. self.play(GrowFromCenter(rectangle), GrowFromCenter(ellipse), GrowFromCenter(ring))
  29.  
  30.  
  31. class Key(SVGMobject):
  32. CONFIG = {
  33. "color": GOLD,
  34. "file_name_prefix": "key2",
  35. "stroke_color": WHITE,
  36. "fill_opacity": 1.0,
  37. "height": .5,
  38. }
  39.  
  40. def __init__(self, mode="plain", **kwargs):
  41. digest_config(self, kwargs)
  42. self.mode = mode
  43. self.parts_named = False
  44. try:
  45. svg_file = os.path.join(
  46. SVG_IMAGE_DIR,
  47. "%s_%s.svg" % (self.file_name_prefix, mode)
  48. )
  49. SVGMobject.__init__(self, file_name=svg_file, **kwargs)
  50. except:
  51. warnings.warn("No %s design with mode %s" %
  52. (self.file_name_prefix, mode))
  53. svg_file = os.path.join(
  54. SVG_IMAGE_DIR,
  55. "key2.svg",
  56. )
  57. SVGMobject.__init__(self, mode="plain", file_name=svg_file, **kwargs)
  58.  
  59.  
  60. class Letter(SVGMobject):
  61. CONFIG = {
  62. # "color": WHITE,
  63. "file_name_prefix": "envelope",
  64. "stroke_color": WHITE,
  65. "fill_opacity": 1.0,
  66. "height": 1,
  67. }
  68.  
  69. def __init__(self, mode="plain", **kwargs):
  70. digest_config(self, kwargs)
  71. self.mode = mode
  72. self.parts_named = False
  73. try:
  74. svg_file = os.path.join(
  75. SVG_IMAGE_DIR,
  76. "%s_%s.svg" % (self.file_name_prefix, mode)
  77. )
  78. SVGMobject.__init__(self, file_name=svg_file, **kwargs)
  79. except:
  80. warnings.warn("No %s design with mode %s" %
  81. (self.file_name_prefix, mode))
  82. svg_file = os.path.join(
  83. SVG_IMAGE_DIR,
  84. "envelope.svg",
  85. )
  86. SVGMobject.__init__(self, mode="plain", file_name=svg_file, **kwargs)
  87.  
  88.  
  89. class Crypt(Scene):
  90. def construct(self):
  91. letter1 = Letter()
  92. letter2 = Letter()
  93. letter1.shift(4 * LEFT)
  94. letter2.shift(4 * RIGHT)
  95. ellipse = Ellipse(width=1, height=.5, color=WHITE)
  96. ellipse.shift(4 * LEFT)
  97.  
  98. alice = TextMobject("Alice")
  99. alice.next_to(letter1, LEFT)
  100. bob = TextMobject("Bob")
  101. bob.next_to(letter2, RIGHT)
  102.  
  103. key = Key()
  104. key2 = Key()
  105. key.scale(2)
  106. key.shift(3 * UP)
  107. # key.shift(3.5 * LEFT + 1 * UP)
  108. # key2.shift(3.5 * RIGHT + 1 * UP)
  109. key2.scale(2)
  110. key2.shift(3 * UP)
  111.  
  112.  
  113. # start of anim
  114. self.add(letter1, alice, bob)
  115. self.wait()
  116.  
  117. # show key, and divide to both
  118. self.play(FadeIn(key))
  119. self.play(FadeIn(key2))
  120. self.play(ApplyMethod(key.move_to, 3.5 * LEFT + 1 * UP), ApplyMethod(key2.move_to, 3.5 * RIGHT + 1 * UP))
  121. self.play(FadeOut(key2))
  122. key2.scale(.5)
  123. self.play(ApplyMethod(key.scale, .5))#, ApplyMethod(key2.scale, .5))
  124.  
  125. # incrypt
  126. self.wait()
  127. self.play(ApplyMethod(key.shift, 1 * DOWN))
  128. self.play(FadeOut(key))
  129. self.play(Transform(letter1, ellipse))
  130.  
  131. # send
  132. self.wait()
  133. self.play(ApplyMethod(letter1.move_to, 4 * RIGHT))
  134.  
  135. # decrypt
  136. self.play(FadeIn(key2))
  137. self.play(ApplyMethod(key2.shift, 1 * DOWN))
  138. self.play(FadeOut(key2))
  139. self.play(Transform(letter1, letter2))
  140. self.wait()
  141.  
  142.  
  143. class Generating(Scene):
  144. def construct(self):
  145.  
  146. arrow1 = Arrow(4 * LEFT, LEFT)
  147. arrow2 = Arrow(RIGHT, 3 * RIGHT)
  148. arrow3 = Arrow(2 * UP, .5 * UP)
  149.  
  150. funct = TextMobject("AES")
  151. funct.scale(2)
  152.  
  153. # seed = TextMobject("seed")
  154. # seed.shift(4*LEFT)
  155.  
  156. # types of input
  157. input1 = TextMobject("Ala ma kota")
  158. input1.next_to(arrow1, LEFT)
  159.  
  160. input3 = TextMobject("tajnehaslo123456")
  161. input3.next_to(arrow3, UP)
  162.  
  163. input2 = TextMobject("Z6kwuEWmquIx")
  164. input2.next_to(arrow2, RIGHT)
  165. input21 = TextMobject("7kmn3fUKqw==")
  166. input21.next_to(input2, DOWN)
  167.  
  168. key = Key()
  169. key.next_to(input3, UP)
  170. key.scale(.7)
  171.  
  172. ellipse = Ellipse(width=1, height=.5, color=WHITE)
  173. ellipse.next_to(input2, UP)
  174.  
  175. letter = Letter()
  176. letter.scale(.7)
  177. letter.next_to(input1, UP)
  178.  
  179. # arrows go wild
  180. self.add(funct)
  181. self.play(Write(input1), Write(input3), GrowFromCenter(key), GrowFromCenter(letter))
  182. self.play(GrowArrow(arrow1))
  183. self.play(GrowArrow(arrow3))
  184. self.wait()
  185. self.play(GrowArrow(arrow2))
  186. self.play(Write(input2), Write(input21), GrowFromCenter(ellipse))
  187. self.wait()
  188. self.wait()
  189.  
  190. # next part
  191.  
  192. # remove all obj
  193. self.play(FadeOut(input1), FadeOut(input3), FadeOut(input2), FadeOut(letter), FadeOut(ellipse),
  194. FadeOut(arrow1), FadeOut(arrow2), FadeOut(arrow3), FadeOut(funct), FadeOut(input21))
  195. self.play(ApplyMethod(key.scale, 2))
  196. self.wait()
  197. # part about random gens
  198.  
  199. text_main = TextMobject("pseudo-random number generator")
  200. text_main.shift(3*RIGHT)
  201. text_left = TextMobject("cryptographically secure")
  202. text_left.next_to(text_main, LEFT)
  203. text_left.set_color(BLUE)
  204.  
  205. text_shrt_main = TextMobject("PRNG")
  206. text_shrt_main.shift(.5 * RIGHT)
  207. text_shrt_left = TextMobject("CS")
  208. text_shrt_main.set_color(WHITE)
  209. text_shrt_left.set_color(BLUE)
  210. text_shrt_left.next_to(text_shrt_main, LEFT)
  211.  
  212. text_time = TextMobject("aktualny czas")
  213. text_time.shift(2*DOWN)
  214.  
  215. # play anim
  216.  
  217. self.play(Write(text_main))
  218. self.wait()
  219. self.play(Write(text_time))
  220. self.wait()
  221. self.play(FadeOut(text_time))
  222. self.wait()
  223. self.play(Write(text_left))
  224. self.wait()
  225. self.play(Transform(text_main, text_shrt_main), Transform(text_left, text_shrt_left))
  226. self.wait()
  227. self.play(FadeOut(text_main), FadeOut(text_left), FadeOut(key))
  228. self.wait()
  229.  
  230.  
  231. class Conditions(Scene):
  232. def construct(self):
  233. text_cond1 = TextMobject("-Correlated")
  234. text_cond1.shift(2 * UP + 4 * LEFT)
  235. text_expl1 = TextMobject("lorem ipsum")
  236. text_expl1.scale(.7)
  237. text_expl1.set_color(RED)
  238. text_expl1.next_to(text_cond1.get_corner(DOWN + RIGHT), DOWN)
  239. text_expl1.align_to(text_cond1, LEFT)
  240.  
  241. text_cond2 = TextMobject("-Przewidywalnosc")
  242. text_cond2.align_to(text_cond1, LEFT)
  243. text_expl21 = TextMobject("przy znajomosci n-1 bitow, prawdopodobienstwo zgadniecia")
  244. text_expl22 = TextMobject("poprawnego bitu musi wynosic 0.5")
  245. text_expl21.scale(.7)
  246. text_expl22.scale(.7)
  247. text_expl21.set_color(RED)
  248. text_expl22.set_color(RED)
  249. text_expl21.next_to(text_cond2.get_corner(DOWN+RIGHT), DOWN)
  250. text_expl22.next_to(text_expl21.get_corner(DOWN+LEFT), DOWN)
  251. text_expl21.align_to(text_cond2, LEFT)
  252. text_expl22.align_to(text_cond2, LEFT)
  253.  
  254. text_cond3 = TextMobject("-Balans")
  255. text_cond3.shift(2 * DOWN)
  256. text_cond3.align_to(text_cond1, LEFT)
  257. text_expl3 = TextMobject("na kazdy bit zrodla powinien przypadac jeden wygenerowany")
  258. text_expl3.scale(.7)
  259. text_expl3.set_color(RED)
  260. text_expl3.next_to(text_cond3.get_corner(DOWN + RIGHT), DOWN)
  261. text_expl3.align_to(text_cond3, LEFT)
  262.  
  263. self.play(Write(text_cond1))
  264. self.wait(2)
  265. self.play(Write(text_expl1))
  266.  
  267. self.play(Write(text_cond2))
  268. self.wait(2)
  269. self.play(Write(text_expl21), Write(text_expl22))
  270.  
  271. self.play(Write(text_cond3))
  272. self.wait(2)
  273. self.play(Write(text_expl3))
  274. self.wait()
  275.  
  276.  
  277. class Test1(Scene):
  278. def construct(self):
  279. # create data
  280. n = 100
  281. seq = "1111101010110111111100001011010001111000111011100100100011100010011100011001000000010000100011101100"
  282. seq_post = ''
  283. for i in seq:
  284. if i == '1':
  285. seq_post += '+1'
  286. else:
  287. seq_post += '-1'
  288. seq_splited = [seq_post[i:i + 2] for i in range(0, len(seq_post), 2)]
  289. s = 0
  290. for i in seq_splited:
  291. if i[0] == '+':
  292. s += 1
  293. else:
  294. s -= 1
  295.  
  296. # code definition
  297. s_code = "\\usepackage{pythonhighlight}\n" \
  298. "\\begin{python}\n"\
  299. "s = \'\'\n" \
  300. "for x in range(10):\n" \
  301. " for _ in range(100):\n" \
  302. " s += str(random.randint(0, 1))\n" \
  303. " print(s)\n" \
  304. "\\end{python}"
  305. # t_code = TextMobject(s_code)
  306.  
  307. # hipothesis definition
  308.  
  309. t_hip0 = TextMobject("$H_{0}:$ Sekwencja binarna jest losowa")
  310. t_hipa = TextMobject("$H_{a}:$ Sekwencja binarna nie jest losowa")
  311. t_alpha = TextMobject("$\\alpha = 0.01$")
  312.  
  313. # text created
  314. text_seq = TextMobject(seq)
  315. text_seq.scale(.5)
  316. text_seq_post = TextMobject(seq_post)
  317. text_seq_post.scale(.5)
  318. text_s = TextMobject(str(s) + ' = ')
  319. # text_s.scale(.5)
  320. text_s.next_to(text_seq_post, LEFT)
  321.  
  322. # animation
  323. # self.play(Write(t_code))
  324. # self.wait()
  325. # self.play(FadeOut(t_code))
  326.  
  327. # TODO add intro to hipothesis
  328. self.play(Write(text_seq))
  329. self.wait()
  330. self.play(Transform(text_seq, text_seq_post))
  331. self.wait()
  332. self.play(Write(text_s))
  333. self.wait()
  334. self.play(FadeOut(text_seq), FadeOut(text_s))
  335. self.wait()
  336.  
  337. # hipothesis test
  338. t1 = TextMobject("$ S_{obs} = \\frac{| S_{n} |}{\\sqrt{n}} $")
  339. s1 = abs(s)/pow(n, 1/2)
  340. t2 = TextMobject("$ %f = \\frac{| %d |}{\\sqrt{%d}} $" % (s1, s, n))
  341. p_value = special.erfc(s1/pow(2, 1/2))
  342.  
  343. t3 = TextMobject("$ P-value = \\textbf{erfc}\\left(\\frac{S_{obs}}{\\sqrt{2}}\\right) $")
  344. t4 = TextMobject("$ %f = \\textbf{erfc}\\left(\\frac{ %f }{\\sqrt{2}}\\right) $" % (p_value, s1))
  345.  
  346. t_alpha_result = TextMobject("$\\alpha = 0.01 \\implies P-value > 0.01$ ")
  347. t_alpha_result.shift(DOWN)
  348. t_alpha_result2 = TextMobject("$\\alpha = 0.01 \\implies %f > 0.01$ " % (p_value))
  349. t_alpha_result2.shift(DOWN)
  350.  
  351. test1_title = TextMobject("\"Frequency (Monobit)\" Test:")
  352. test1_title.scale(0.5)
  353. test1_title.to_edge(UP+LEFT)
  354. test1_title.set_color(ORANGE)
  355.  
  356. # ...
  357. self.play(Write(test1_title))
  358.  
  359. self.play(Write(t1))
  360. self.wait(3)
  361. self.play(Transform(t1, t2))
  362. self.wait()
  363. self.play(ApplyMethod(t1.move_to, 2 * UP))
  364.  
  365. self.play(Write(t3))
  366. self.wait(2)
  367. self.play(Transform(t3, t4))
  368. self.wait(2)
  369. self.play(ApplyMethod(t3.move_to, UP))
  370.  
  371. self.play(Write(t_alpha_result))
  372. self.wait(2)
  373. self.play(Transform(t_alpha_result, t_alpha_result2))
  374. self.wait(2)
  375. self.play(ApplyMethod(t_alpha_result.move_to, UP + DOWN))
  376. self.wait(2)
  377. # conclusion
  378.  
  379. t_hip0.move_to(DOWN)
  380. t_hipa.next_to(t_hip0, DOWN)
  381.  
  382. self.play(Write(t_hip0))
  383. self.play(Write(t_hipa))
  384. self.wait()
  385. self.play(ApplyMethod(t_hip0.set_color, GREEN), ApplyMethod(t_hipa.set_color, RED))
  386. self.wait(2)
  387.  
  388.  
  389. F_ans_test2 = 0.25
  390.  
  391.  
  392. class Test2(Scene):
  393. @staticmethod
  394. def propotion(block: str) -> float:
  395. return block.count('1')/len(block)
  396.  
  397. def construct(self):
  398. n = 100
  399. M = 10
  400. seq = "0100100100000011001011110101010011000001101101000000011111101001101111011001011101111110000111000010"
  401. seq_block = [seq[i:i + 10] for i in range(0, len(seq), 10)]
  402.  
  403. t_name = TextMobject("Frequency Test within a Block:")
  404. t_name.scale(0.5)
  405. t_name.to_edge(UP + LEFT)
  406. t_name.set_color(ORANGE)
  407.  
  408. t_n = TextMobject("$n = %d$" % n)
  409. t_n.scale(0.7)
  410. t_n.shift(5 * LEFT + 2 * UP)
  411. t_M = TextMobject("$M = %d$" % M)
  412. t_M.scale(0.7)
  413. t_M.next_to(t_n, DOWN)
  414.  
  415. t_seq = TextMobject(seq)
  416. t_seq.scale(0.5)
  417. t_seq_block = TextMobject(str(seq_block))
  418. t_seq_block.scale(0.7)
  419. t_epsilon = TextMobject("$\\varepsilon_{i} = $")
  420. t_epsilon.next_to(t_seq_block, LEFT)
  421.  
  422. # animation
  423.  
  424. self.play(Write(t_name))
  425. self.wait()
  426. self.play(Write(t_seq), Write(t_n))
  427. self.wait(2)
  428. self.play(Transform(t_seq, t_seq_block), Write(t_epsilon), Write(t_M))
  429. self.wait(2)
  430. # todo fade out
  431.  
  432. # calculations
  433. t_pi = TextMobject("$\\pi_{i} = \\frac{\\displaystyle\\sum_{j=1}^{M} "
  434. "\\varepsilon_{(i-1)M+j}}{M}$")
  435. t_pi.shift(2 * UP)
  436.  
  437. f_seq = []
  438. for i in seq_block:
  439. f_seq.append(self.propotion(i))
  440.  
  441. t_pi_seq = TextMobject(str(f_seq))
  442. t_pi_i = TextMobject("$\\pi_{i} = $")
  443. t_pi_i.next_to(t_pi_seq, LEFT)
  444.  
  445. # animation calculations
  446.  
  447. self.play(Write(t_pi))
  448. self.wait()
  449. self.play(Transform(t_seq, t_pi_seq), Transform(t_epsilon, t_pi_i))
  450. self.wait(2)
  451.  
  452. # count chi^2
  453.  
  454. t_chi_formula = TextMobject("$\\chi^{2}(obs) = 4 M \\displaystyle"
  455. "\\sum_{i=1}^{N}(\\pi_{i}-^1/_2)^{2}$")
  456. t_chi_formula.shift(2 * DOWN)
  457. N = 10
  458.  
  459. t_chi_text_chi_only = TextMobject("$\\chi^{2}(obs) = $")
  460. s_chi_text = "$ 4 \\times %d (" % N
  461. f_real_ans = 0
  462. for index, i in enumerate(f_seq):
  463. f_real_ans += pow(i - .5, 2)
  464. s_chi_text += "(%f - 0.5)^{2}" % i
  465. if index < N:
  466. s_chi_text += "+"
  467. s_chi_text += ")$"
  468. global F_ans_test2
  469. F_ans_test2 = f_real_ans
  470. t_chi_ans = TextMobject("$ = %f $" % f_real_ans)
  471. t_chi_text = TextMobject(s_chi_text)
  472. t_chi_text.shift(2 * DOWN)
  473. t_chi_text.scale(.5)
  474. t_chi_text_chi_only.next_to(t_chi_text, LEFT)
  475. t_chi_ans.next_to(t_chi_text, RIGHT)
  476.  
  477. self.play(Write(t_chi_formula))
  478. self.wait()
  479. self.play(Transform(t_chi_formula, t_chi_text), Write(t_chi_text_chi_only),
  480. Write(t_chi_ans))
  481. self.wait(2)
  482. self.play(FadeOut(t_chi_formula), FadeOut(t_chi_text_chi_only),
  483. FadeOut(t_chi_ans), FadeOut(t_seq), FadeOut(t_epsilon),
  484. FadeOut(t_n), FadeOut(t_M), FadeOut(t_pi))
  485. self.wait()
  486.  
  487.  
  488. class Test2_part2(Scene):
  489. def construct(self):
  490. t_pvalue_formula = TextMobject("$ P-value = \\textbf{igamc}( ^N/_2, \\frac{\\chi^{2}(obs)}{2})$")
  491.  
  492. global F_ans_test2
  493. N = 10
  494.  
  495. s_pvalue = "2.29\\times10^{-07}"
  496.  
  497. print(special.gammainc(N/2, F_ans_test2/2))
  498. # STATIC VALUE BASED ON CALC!
  499. t_pvalue_ans = TextMobject("$ %s \\approx \\textbf{igamc}( %f , %f ) $" % (s_pvalue, N/2, F_ans_test2/2))
  500.  
  501. t_name = TextMobject("Frequency Test within a Block:")
  502. t_name.scale(0.5)
  503. t_name.to_edge(UP + LEFT)
  504. t_name.set_color(ORANGE)
  505.  
  506. self.add(t_name)
  507. self.play(Write(t_pvalue_formula))
  508. self.wait(2)
  509. self.play(Transform(t_pvalue_formula, t_pvalue_ans))
  510. self.wait(4)
  511. self.play(ApplyMethod(t_pvalue_formula.shift, UP))
  512.  
  513. # hipothesis
  514.  
  515. t_alpha_result = TextMobject("$\\alpha = 0.01 \\implies P-value < 0.01$ ")
  516. # t_alpha_result.shift(DOWN)
  517. t_alpha_result2 = TextMobject("$\\alpha = 0.01 \\implies %s < 0.01$ " % (s_pvalue))
  518. # t_alpha_result2.shift(DOWN)
  519. t_hip0 = TextMobject("$H_{0}:$ Sekwencja binarna jest losowa")
  520. t_hipa = TextMobject("$H_{a}:$ Sekwencja binarna nie jest losowa")
  521. t_hip0.move_to(DOWN)
  522. t_hipa.next_to(t_hip0, DOWN)
  523.  
  524. self.play(Write(t_alpha_result))
  525. self.wait(2)
  526. self.play(Transform(t_alpha_result, t_alpha_result2))
  527. self.wait(2)
  528. self.play(ApplyMethod(t_alpha_result.move_to, UP + DOWN))
  529. self.wait()
  530.  
  531. self.play(Write(t_hip0))
  532. self.play(Write(t_hipa))
  533. self.wait()
  534. self.play(ApplyMethod(t_hip0.set_color, RED), ApplyMethod(t_hipa.set_color, GREEN))
  535. self.wait(2)
  536.  
  537.  
  538. class Test_my_code(Scene):
  539. def construct(self):
  540. s1 = 2.123123
  541. s = -2
  542. n = 100
  543. t1 = TextMobject("$ S_{obs} = \\frac{| S_{n} |}{\\sqrt{n}} $")
  544. t2 = TextMobject("$ %f = \\frac{| %d |}{\\sqrt{%d}} $" % (s1, s, n))
  545.  
  546. t3 = TextMobject("$ P-value = \\textbf{erfc}\\left(\\frac{S_{obs}}{\\sqrt{2}}\\right) $")
  547. t_chi_formula = TextMobject("$\\chi^{2}(obs) = 4 M \\displaystyle"
  548. "\\sum_{i=1}^{N}(\\pi_{i}- ^1/_2)^{2}$")
  549. t_pi_seq = [0.3, 0.2, 0.4, 0.1]
  550. for index, i in enumerate(t_pi_seq):
  551. print(type(i))
  552.  
  553. self.add(t_chi_formula)
  554. self.wait(4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement