Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

ࣗݾ঺հ ͓લ୭Α ໊લ Hayao Suzukiʢླ໦ɹॣʣ Twitter @CardinalXaro ϒϩά https://xaro.hatenablog.jp/ ઐ໳ ਺ֶ (૊߹ͤ࿦ɾάϥϑཧ࿦) ֶҐ म࢜ʢ޻ֶʣ ɺిؾ௨৴େֶ ࢓ࣄ גࣜձࣾΞΠϦοδ › εϚʔτϑΥϯΞϓϦͷόοΫΤϯυαʔόʔͷ։ൃ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 3 / 33

Slide 4

Slide 4 text

ࣗݾ঺հ ٕज़ॻͷࠪಡ › ʰ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

Slide 5

Slide 5 text

ࠓ೔ͷൃද όοςϦʔಉࠝ఩ֶʢ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

Slide 6

Slide 6 text

ࠓ೔ͷൃද cmath ͱ͸Կऀ͔ 1 C ݴޠͰ࣮૷͞Εͨߴ଎ͳ math ϥΠϒϥϦ 2 Ωϡ΢Ϧ (Cucumber) ͷը૾ࣝผͷͨΊͷ਺ֶϥΠϒϥϦ 3 ෳૉ਺ (Complex Number) ͷܭࢉϥΠϒϥϦ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 6 / 33

Slide 7

Slide 7 text

ࠓ೔ͷൃද cmath Ϟδϡʔϧ › ෳૉ਺ͷͨΊͷ਺ֶؔ਺ › 9V ి஑΍ΒχΧυి஑ͷΑ͏ͳଘࡏʹࠓɺεϙοτΛ౰ͯΔɻ ࠓճ࢖͏΋ͷ › Python 3.7.xʢPython 3.8.x Ͱ΋ָ͠Ί·͢ʂʣ › MatplotlibʢάϥϑඳըϥΠϒϥϦʣ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 7 / 33

Slide 8

Slide 8 text

ࠓ೔ͷൃද ܅͸ 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

Slide 9

Slide 9 text

ෳૉ਺ͱ͸ ෳૉ਺ɺ஌ͬͯ·͔͢ʁ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 9 / 33

Slide 10

Slide 10 text

ෳૉ਺ͷఆٛ ఆٛ (ෳૉ਺) 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

Slide 11

Slide 11 text

ෳૉ਺ͱମ ମ (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

Slide 12

Slide 12 text

ෳૉ਺ͱॱং ෳૉ਺ମ͸ॱংମͰ͸ͳ͍ ࣮਺ͷΑ͏ͳશॱংؔ܎ΛఆٛͰ͖ͳ͍ʂ Python ΋ෳૉ਺ମ͸ॱংମͰ͸ͳ͍͜ͱΛ஌͍ͬͯΔ >>> -100 - 100j < 65536 + 256j # ӈล͕େ͖ͦ͏ʹࢥ͑Δ͕... Traceback (most recent call last): File "", line 1, in TypeError: '<' not supported between instances of 'complex' and 'complex' ॱংମʹ͓͚Δฏํݩ͸ඇෛͰ͋Δɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 12 / 33

Slide 13

Slide 13 text

ෳૉ਺ͷڞ໾ ෳૉ਺ͷڞ໾ ෳૉ਺ 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

Slide 14

Slide 14 text

ෳૉ਺ͷۃ࠲ඪදه ෳૉ਺ฏ໘ ෳૉ਺ 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

Slide 15

Slide 15 text

ෳૉ਺ͷۃ࠲ඪදه ඦฉ͸ҰݟʹવΓ Figure: ෳૉ਺ฏ໘ʢWikipedia ͔ΒҾ༻ʣ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 15 / 33

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

ࢦ਺വ਺ ࢦ਺വ਺ cmath.exp(x)ʢެࣜυΩϡϝϯτΑΓʣ e Λࣗવର਺ͷఈͱͯ͠ɺe ͷ x ৐Λฦ͠·͢ɻ ࣗવର਺ͷఈͷෳૉ਺৐ͬͯԿʁʁʁ › ࣗવ਺৐ ! Θ͔Δ › ੔਺৐ ! Θ͔Δ › ༗ཧ਺৐ ! ·ͩΘ͔Δ › ࣮਺৐ ! ·ͩ͜ΕͳΒ... › ෳૉ਺৐ ! ͜Ε΋͏Θ͔ΜͶ͐ͳ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 17 / 33

Slide 18

Slide 18 text

ࢦ਺വ਺Λ८Δ๯ݥ ۃݶͰఆٛ͢Ε͹͍͍Μͩʂ 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

Slide 19

Slide 19 text

ࢦ਺വ਺Λ८Δ๯ݥ ۃݶ஋ʹΑΔఆٛ 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

Slide 20

Slide 20 text

ݟͤͯ΋Β͓͏͔ɺ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

Slide 21

Slide 21 text

཭ࢄ 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

Slide 22

Slide 22 text

཭ࢄ Fourier ม׵ ྫɿ৴߸ղੳ Figure: ಾͷ৴߸ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 22 / 33

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

཭ࢄ 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

Slide 25

Slide 25 text

཭ࢄ 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

Slide 26

Slide 26 text

཭ࢄ Fourier ม׵ ྫɿ৴߸ͷप೾਺εϖΫτϧ Figure: ಾͷ৴߸ͷप೾਺εϖΫτϧ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 26 / 33

Slide 27

Slide 27 text

཭ࢄ 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

Slide 28

Slide 28 text

཭ࢄ 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

Slide 29

Slide 29 text

཭ࢄ Fourier ม׵ ྫɿ৴߸Λ෮ݩ͢Δ Figure: ٯ཭ࢄ Fourier ม׵Ͱݩͷ৴߸Λ෮ݩ͢Δ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 29 / 33

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

1 ͷ n ৐ࠜ 1 ͷ 7 ৐ࠜʹΑΔਖ਼ࣣ֯ܗͷ࡞ਤ Figure: ਖ਼ࣣ֯ܗ ਖ਼ࣣ֯ܗ͸ఆنͱίϯύεʹΑͬͯ࡞ਤ͕Ͱ͖ͳ͍ɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 32 / 33

Slide 33

Slide 33 text

·ͱΊ ·ͱΊ › cmath ͸ෳૉ਺ͷͨΊͷ਺ֶϥΠϒϥϦͰ͋Δɻ › ඪ४ϥΠϒϥϦͷൣғͰ΋཭ࢄ Fourier ม׵͸Ͱ͖Δɻ › ܅͚ͩͷ࠷ڧͷ cmath ΞϓϦέʔγϣϯΛ࣮૷͠Α͏ʂ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 33 / 33