Do you know cmath module?

0350fb935be160186cd72472c9e5543b?s=47 HayaoSuzuki
February 29, 2020

Do you know cmath module?

0350fb935be160186cd72472c9e5543b?s=128

HayaoSuzuki

February 29, 2020
Tweet

Transcript

  1. ܅͸ cmath Λ஌͍ͬͯΔ͔ Hayao Suzuki PyCon mini Shizuoka 2020 February

    29, 2020
  2. Contents 1 ࣗݾ঺հ 2 cmath ͱ͸Կऀ͔ 3 ෳૉ਺ͱ͸Կ͔ 4 ෳૉ਺ͷۃ࠲ඪදه

    5 ෳૉࢦ਺വ਺ 6 ཭ࢄ Fourier ม׵ 7 1 ͷ n ৐ࠜ 8 ·ͱΊ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 2 / 33
  3. ࣗݾ঺հ ͓લ୭Α ໊લ Hayao Suzukiʢླ໦ɹॣʣ Twitter @CardinalXaro ϒϩά https://xaro.hatenablog.jp/ ઐ໳

    ਺ֶ (૊߹ͤ࿦ɾάϥϑཧ࿦) ֶҐ म࢜ʢ޻ֶʣ ɺిؾ௨৴େֶ ࢓ࣄ גࣜձࣾΞΠϦοδ › εϚʔτϑΥϯΞϓϦͷόοΫΤϯυαʔόʔͷ։ൃ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 3 / 33
  4. ࣗݾ঺հ ٕज़ॻͷࠪಡ › ʰEffective Pythonʱ ʢΦϥΠϦʔδϟύϯʣ › ʰΤϨΨϯτͳ SciPyʱ ʢΦϥΠϦʔδϟύϯʣ

    › ʰσʔλαΠΤϯεઃܭϚχϡΞϧʱ ʢΦϥΠϦʔδϟύϯʣͳͲ › https://xaro.hatenablog.jp/ ʹҰཡ͋Γ·͢ɻ ͍ΖΜͳൃද › ʮSymPy ʹΑΔ਺ࣜॲཧʯ ʢPyCon JP 2018ʣ › ʮPython Ͱָ͠Ήॳ౳੔਺࿦ʯ ʢPyCon mini Hiroshima 2019ʣ ͳͲ › https://xaro.hatenablog.jp/ ʹҰཡ͋Γ·͢ɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 4 / 33
  5. ࠓ೔ͷൃද όοςϦʔಉࠝ఩ֶʢPEP 206 ΑΓʣ Python σΟετϦϏϡʔγϣϯࣗ਎͕ɺผ్μ΢ϯϩʔυ͢Δ͜ͱͳ͘ ͙͢ʹར༻Ͱ͖Δ๛෋Ͱ൚༻ੑͷߴ͍ඪ४ϥΠϒϥϦΛ࣋ͭ͜ͱɻ Python νϡʔτϦΞϧͰ঺հ͞Ε͍ͯΔྫ ›

    xmlrpc.client XML-RPC ΫϥΠΞϯτ › xmlrpc.server XML-RPC αʔόʔ › email ిࢠϝʔϧͱ MIME ॲཧͷͨΊͷύοέʔδ › json JSON Τϯίʔμ͓Αͼσίʔμ › sqlite3 SQLite σʔλϕʔεʹର͢Δ DB-API 2.0 Πϯλ ϑΣʔε Hayao (Shizuoka 2020) All about cmath module February 29, 2020 5 / 33
  6. ࠓ೔ͷൃද cmath ͱ͸Կऀ͔ 1 C ݴޠͰ࣮૷͞Εͨߴ଎ͳ math ϥΠϒϥϦ 2 Ωϡ΢Ϧ

    (Cucumber) ͷը૾ࣝผͷͨΊͷ਺ֶϥΠϒϥϦ 3 ෳૉ਺ (Complex Number) ͷܭࢉϥΠϒϥϦ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 6 / 33
  7. ࠓ೔ͷൃද cmath Ϟδϡʔϧ › ෳૉ਺ͷͨΊͷ਺ֶؔ਺ › 9V ి஑΍ΒχΧυి஑ͷΑ͏ͳଘࡏʹࠓɺεϙοτΛ౰ͯΔɻ ࠓճ࢖͏΋ͷ ›

    Python 3.7.xʢPython 3.8.x Ͱ΋ָ͠Ί·͢ʂʣ › MatplotlibʢάϥϑඳըϥΠϒϥϦʣ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 7 / 33
  8. ࠓ೔ͷൃද ܅͸ cmath Λ஌͍ͬͯΔ͔ › cmath ͱ͸Կऀ͔ › ෳૉ਺ͱ͸Կ͔ ›

    ෳૉ਺ͷۃ࠲ඪදه › ෳૉࢦ਺വ਺ › ཭ࢄ Fourier ม׵ › 1 ͷ n ৐ࠜ › ·ͱΊ ࢿྉ͸ઃܭਤڞ༗αΠτʹ͋Δʂ ࢿྉ͸͢΂ͯ https://github.com/HayaoSuzuki/PyCon-mini-Shizuoka-2020/ ʹ ͋Γ·͢ɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 8 / 33
  9. ෳૉ਺ͱ͸ ෳૉ਺ɺ஌ͬͯ·͔͢ʁ Hayao (Shizuoka 2020) All about cmath module February

    29, 2020 9 / 33
  10. ෳૉ਺ͷఆٛ ఆٛ (ෳૉ਺) i2 = `1 Ͱ͋ΔΑ͏ͳجఈ 1; i Λ࣮࣋ͭ਺ମ

    R ্ͷ 2 ࣍ݩϕΫτϧۭ ؒͷݩΛෳૉ਺ͱݺͿɻ·ͨɺi Λڏ਺୯ҐͱݺͿɻ Python Ͱෳૉ਺Λఆٛ͢Δ >>> 3 + 5j # Python Ͱ͸ڏ਺୯ҐΛ j ·ͨ͸ J ͱ͢Δ (3+5j) >>> 1J**2 # ڏ਺୯Ґͷࣗ৐͸-1 ͱͳΔɻ (-1+0j) >>> 4 + 5j == (5j + 4) # ࣮෦ͱڏ෦͕ͦΕͧΕ౳͍͠ True Hayao (Shizuoka 2020) All about cmath module February 29, 2020 10 / 33
  11. ෳૉ਺ͱମ ମ (Field) == ࢛ଇԋࢉ͕Ͱ͖Δू߹ ෳૉ਺͸ෳૉ਺ମ C Λͳ͢ɻ Python ʹ͓͚Δෳૉ਺ͷ࢛ଇԋࢉ

    >>> 8 - 5j + -5 + 1j # Ճ๏ (3-4j) >>> (1 + 2j) * (1 - 2J) # ৐๏ (5+0j) >>> (97 + 0j) / (4 + 9j) # আ๏ (3.9999999999999996-9j) 97 ͸ૉ਺Ͱ͋Δ͕ɺ97 = (4 + 9i)(4 ` 9i) ͱͳΔɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 11 / 33
  12. ෳૉ਺ͱॱং ෳૉ਺ମ͸ॱংମͰ͸ͳ͍ ࣮਺ͷΑ͏ͳશॱংؔ܎ΛఆٛͰ͖ͳ͍ʂ Python ΋ෳૉ਺ମ͸ॱংମͰ͸ͳ͍͜ͱΛ஌͍ͬͯΔ >>> -100 - 100j <

    65536 + 256j # ӈล͕େ͖ͦ͏ʹࢥ͑Δ͕... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'complex' and 'complex' ॱংମʹ͓͚Δฏํݩ͸ඇෛͰ͋Δɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 12 / 33
  13. ෳૉ਺ͷڞ໾ ෳૉ਺ͷڞ໾ ෳૉ਺ z = x + iy ʹରͯ͠ —

    z = x ` iy Λ z ͷڞ໾ͱݺͿɻ Python ʹ͓͚Δෳૉ਺ͷڞ໾ >>> z = 5 - 3j >>> z.conjugate() # complex ܕͷϝιουͱͯ͠ (5+3j) >>> z * (z.conjugate() / abs(z)**2) # ٯݩΛߏ੒͢Δ (1+5.551115123125783e-17j) ڞ໾ͷઆ໌͸Ͳ͜ʹ͋Δʁ ૊ΈࠐΈܕ΍ cmath Ͱ͸ͳ͘ numbers ϞδϡʔϧͰઆ໌͞Ε͍ͯΔɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 13 / 33
  14. ෳૉ਺ͷۃ࠲ඪදه ෳૉ਺ฏ໘ ෳૉ਺ z = x + iy Λ 2

    ࣍ݩ࣮਺ฏ໘ R2 ্ͷ఺ (x; y) ͱΈͳ͢͜ͱ ͕Ͱ͖Δɻ͜ΕΛෳૉ਺ฏ໘ͱ͍͏ɻ ෳૉ਺ͷۃ࠲ඪܗࣜ ෳૉ਺ฏ໘্ͷ఺ z = x + iy(x; y 2 R) Λ࣮෦ x ͱڏ෦ y ͷ૊ (x; y) Ͱ͸ͳ͘ݪ఺͔Βͷڑ཭ r ͱภ֯ „ ͷ૊ (r; „) Ͱ΋ఆٛͰ͖Δɻ ͜ΕΛෳૉ਺ͷۃ࠲ඪܗࣜͱ͍͏ɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 14 / 33
  15. ෳૉ਺ͷۃ࠲ඪදه ඦฉ͸ҰݟʹવΓ Figure: ෳૉ਺ฏ໘ʢWikipedia ͔ΒҾ༻ʣ Hayao (Shizuoka 2020) All about

    cmath module February 29, 2020 15 / 33
  16. Python ʹ͓͚Δෳૉ਺ͷۃ࠲ඪදه Python ʹ͓͚Δෳૉ਺ͷ࢛ଇԋࢉ >>> import cmath # ਅଧొ৔ >>>

    z = 1 + 2j # ௚ަ࠲ඪ͔Βۃ࠲ඪʹม׵͢Δ >>> r, phi = cmath.polar(z) >>> r, phi # r = abs(z), phi = cmath.phase(z) (2.23606797749979, 1.1071487177940904) >>> w = cmath.rect(r, phi) # ۃ࠲ඪ͔Β௚ަ࠲ඪʹม׵͢Δ >>> w (1.0000000000000002+2j) >>> cmath.isclose(z, w) # == Ͱ͸ͳ͘ isclose Λ࢖͏ True Hayao (Shizuoka 2020) All about cmath module February 29, 2020 16 / 33
  17. ࢦ਺വ਺ ࢦ਺വ਺ cmath.exp(x)ʢެࣜυΩϡϝϯτΑΓʣ e Λࣗવର਺ͷఈͱͯ͠ɺe ͷ x ৐Λฦ͠·͢ɻ ࣗવର਺ͷఈͷෳૉ਺৐ͬͯԿʁʁʁ ›

    ࣗવ਺৐ ! Θ͔Δ › ੔਺৐ ! Θ͔Δ › ༗ཧ਺৐ ! ·ͩΘ͔Δ › ࣮਺৐ ! ·ͩ͜ΕͳΒ... › ෳૉ਺৐ ! ͜Ε΋͏Θ͔ΜͶ͐ͳ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 17 / 33
  18. ࢦ਺വ਺Λ८Δ๯ݥ ۃݶͰఆٛ͢Ε͹͍͍Μͩʂ ez := lim n!1 1 + z n

    !n : ແݶڃ਺Ͱఆٛ͢Ε͹͍͍Μͩʂ ez := 1 X n=0 zn n! : ࣮വ਺Ͱఆٛ͢Ε͹͍͍Μͩʂ ez := ex(cos y + i sin y) where z = x + iy. Hayao (Shizuoka 2020) All about cmath module February 29, 2020 18 / 33
  19. ࢦ਺വ਺Λ८Δ๯ݥ ۃݶ஋ʹΑΔఆٛ def exp_by_limit(z: complex, n: int = 10) ->

    complex: N = 10 ** n return pow(1 + z / N, N) ແݶڃ਺ʹΑΔఆٛ def exp_by_series(z: complex, n: int = 30) -> complex: return sum(pow(z, i) / factorial(i) for i in range(n)) ࣮വ਺ʹΑΔఆٛ def exp_by_real_func(z: complex) -> complex: x, y, i = z.real, z.imag, (0 + 1j) return math.exp(x) * (math.cos(y) + math.sin(y) * i) Hayao (Shizuoka 2020) All about cmath module February 29, 2020 19 / 33
  20. ݟͤͯ΋Β͓͏͔ɺcmath.exp ͷҖྗͱ΍ΒΛ ࣗ࡞ͷؔ਺Ͱ Euler ͷ౳ࣜ eiı = `1 Λܭࢉͯ͠ΈΔ >>>

    z = cmath.pi * 1j >>> exp_by_limit(z) (-1.0004936019770099+1.0340526558763341e-07j) >>> exp_by_series(z) (-1.0000000000000002+3.461777852236587e-16j) >>> exp_by_real_func(z) (-1+1.2246467991473532e-16j) cmath.exp Ͱ Euler ͷ౳ࣜ eiı = `1 Λܭࢉͯ͠ΈΔ >>> cmath.exp(z) (-1+1.2246467991473532e-16j) Hayao (Shizuoka 2020) All about cmath module February 29, 2020 20 / 33
  21. ཭ࢄ Fourier ม׵ ཭ࢄ Fourier ม׵ ෳૉവ਺ f(x) ͷ཭ࢄ Fourier

    ม׵ F (t) ͸ F (t) = N`1 X n=0 f(n)e`i 2ınt N Ͱ༩͑ΒΕΔɻ ཭ࢄ Fourier ม׵ͷԠ༻෼໺͸ 2 ͭ › ৴߸ղੳ › σʔλѹॖ › ͦͷଞॾʑ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 21 / 33
  22. ཭ࢄ Fourier ม׵ ྫɿ৴߸ղੳ Figure: ಾͷ৴߸ Hayao (Shizuoka 2020) All

    about cmath module February 29, 2020 22 / 33
  23. ཭ࢄ Fourier ม׵ ྫɿ৴߸ͷαϯϓϦϯά Figure: ಾͷ৴߸ΛαϯϓϦϯά͢Δ Hayao (Shizuoka 2020) All

    about cmath module February 29, 2020 23 / 33
  24. ཭ࢄ Fourier ม׵ ཭ࢄ Fourier ม׵ʢߴ଎ Fourier ม׵Ͱ͸ͳ͍ʂʣ Fs =

    [ sum( sampling_fs[n] * cmath.exp(-2j * cmath.pi * k * n / sampling_freq) for n in range(sampling_freq) ) for k in range(sampling_freq) ] Hayao (Shizuoka 2020) All about cmath module February 29, 2020 24 / 33
  25. ཭ࢄ Fourier ม׵ ৴߸ͷप೾਺εϖΫτϧ plt.stem( range(-sampling_freq // 2, sampling_freq //

    2), list(map(abs, Fs)), use_line_collection=True, ) Hayao (Shizuoka 2020) All about cmath module February 29, 2020 25 / 33
  26. ཭ࢄ Fourier ม׵ ྫɿ৴߸ͷप೾਺εϖΫτϧ Figure: ಾͷ৴߸ͷप೾਺εϖΫτϧ Hayao (Shizuoka 2020) All

    about cmath module February 29, 2020 26 / 33
  27. ཭ࢄ Fourier ม׵ ཭ࢄ Fourier ม׵ ٯ཭ࢄ Fourier ม׵͸ f(n)

    = 1 N N`1 X n=0 F (t)ei 2ınt N Ͱ༩͑ΒΕΔɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 27 / 33
  28. ཭ࢄ Fourier ม׵ ٯ཭ࢄ Fourier ม׵ # ٯ཭ࢄ Fourier ม׵

    inverse_Fs = [ sum( (1 / sampling_freq) * Fs[n] * cmath.exp(2j * cmath.pi * k * n / sampling_freq) for n in range(sampling_freq) ) for k in range(sampling_freq) ] # ࣮෦͚ͩऔΓग़͢ real_attr = operator.attrgetter("real") inverse_Fs_real = list(map(real_attr, inverse_Fs)) Hayao (Shizuoka 2020) All about cmath module February 29, 2020 28 / 33
  29. ཭ࢄ Fourier ม׵ ྫɿ৴߸Λ෮ݩ͢Δ Figure: ٯ཭ࢄ Fourier ม׵Ͱݩͷ৴߸Λ෮ݩ͢Δ Hayao (Shizuoka

    2020) All about cmath module February 29, 2020 29 / 33
  30. 1 ͷ n ৐ࠜ 1 ͷ n ৐ࠜ 1 ͷ

    n ৐ࠜ͸ ei 2ık n (k = 0; : : : ; n ` 1) Ͱ༩͑ΒΕΔɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 30 / 33
  31. 1 ͷ n ৐ࠜ 1 ͷ n ৐ࠜͰਖ਼ n ֯ܗΛ࡞ਤ͢Δ

    N = 7 roots_of_one = [ cmath.exp(((2 * cmath.pi * k) / N) * 1j) for k in range(N + 1) ] angles = list(map(cmath.phase, roots_of_one)) length = list(map(abs, roots_of_one)) plt.polar(angles, length) Hayao (Shizuoka 2020) All about cmath module February 29, 2020 31 / 33
  32. 1 ͷ n ৐ࠜ 1 ͷ 7 ৐ࠜʹΑΔਖ਼ࣣ֯ܗͷ࡞ਤ Figure: ਖ਼ࣣ֯ܗ

    ਖ਼ࣣ֯ܗ͸ఆنͱίϯύεʹΑͬͯ࡞ਤ͕Ͱ͖ͳ͍ɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 32 / 33
  33. ·ͱΊ ·ͱΊ › cmath ͸ෳૉ਺ͷͨΊͷ਺ֶϥΠϒϥϦͰ͋Δɻ › ඪ४ϥΠϒϥϦͷൣғͰ΋཭ࢄ Fourier ม׵͸Ͱ͖Δɻ ›

    ܅͚ͩͷ࠷ڧͷ cmath ΞϓϦέʔγϣϯΛ࣮૷͠Α͏ʂ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 33 / 33