Share
Embed
Do you know cmath module?
Pycon mini Shizuoka 2020 (online)
https://github.com/HayaoSuzuki/PyCon-mini-Shizuoka-2020
HayaoSuzuki
February 29, 2020
Transcript
33 slides
-
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 -
ࣗݾհ ͓લ୭Α ໊લ Hayao Suzukiʢླɹॣʣ Twitter @CardinalXaro ϒϩά https://xaro.hatenablog.jp/ ઐ
ֶ (߹ͤɾάϥϑཧ) ֶҐ म࢜ʢֶʣ ɺిؾ௨৴େֶ ࣄ גࣜձࣾΞΠϦοδ › εϚʔτϑΥϯΞϓϦͷόοΫΤϯυαʔόʔͷ։ൃ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 3 / 33 -
ࣗݾհ ٕज़ॻͷࠪಡ › ʰ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 -
ࠓͷൃද όοςϦʔಉֶࠝʢ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 -
ࠓͷൃද cmath ͱԿऀ͔ 1 C ݴޠͰ࣮͞Εͨߴͳ math ϥΠϒϥϦ 2 ΩϡϦ
(Cucumber) ͷը૾ࣝผͷͨΊͷֶϥΠϒϥϦ 3 ෳૉ (Complex Number) ͷܭࢉϥΠϒϥϦ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 6 / 33 -
ࠓͷൃද cmath Ϟδϡʔϧ › ෳૉͷͨΊͷֶؔ › 9V ిΒχΧυిͷΑ͏ͳଘࡏʹࠓɺεϙοτΛͯΔɻ ࠓճ͏ͷ ›
Python 3.7.xʢPython 3.8.x Ͱָ͠Ί·͢ʂʣ › MatplotlibʢάϥϑඳըϥΠϒϥϦʣ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 7 / 33 -
ࠓͷൃද ܅ 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 -
ෳૉͷఆٛ ఆٛ (ෳૉ) 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 -
ෳૉͱମ ମ (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 -
ෳૉͱॱং ෳૉମॱংମͰͳ͍ ࣮ͷΑ͏ͳશॱংؔΛఆٛͰ͖ͳ͍ʂ 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 -
ෳૉͷڞ ෳૉͷڞ ෳૉ 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 -
ෳૉͷۃ࠲ඪදه ෳૉฏ໘ ෳૉ 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 -
ෳૉͷۃ࠲ඪදه ඦฉҰݟʹવΓ Figure: ෳૉฏ໘ʢWikipedia ͔ΒҾ༻ʣ Hayao (Shizuoka 2020) All about
cmath module February 29, 2020 15 / 33 -
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 -
ࢦവ ࢦവ cmath.exp(x)ʢެࣜυΩϡϝϯτΑΓʣ e Λࣗવରͷఈͱͯ͠ɺe ͷ x Λฦ͠·͢ɻ ࣗવରͷఈͷෳૉͬͯԿʁʁʁ ›
ࣗવ ! Θ͔Δ › ! Θ͔Δ › ༗ཧ ! ·ͩΘ͔Δ › ࣮ ! ·ͩ͜ΕͳΒ... › ෳૉ ! ͜Ε͏Θ͔ΜͶ͐ͳ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 17 / 33 -
ࢦവΛ८Δݥ ۃݶͰఆٛ͢Ε͍͍Μͩʂ 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 -
ࢦവΛ८Δݥ ۃݶʹΑΔఆٛ 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 -
ݟͤͯΒ͓͏͔ɺ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 -
ࢄ 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 -
ࢄ Fourier ม ྫɿ৴߸ղੳ Figure: ಾͷ৴߸ Hayao (Shizuoka 2020) All
about cmath module February 29, 2020 22 / 33 -
ࢄ Fourier ม ྫɿ৴߸ͷαϯϓϦϯά Figure: ಾͷ৴߸ΛαϯϓϦϯά͢Δ Hayao (Shizuoka 2020) All
about cmath module February 29, 2020 23 / 33 -
ࢄ 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 -
ࢄ 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 -
ࢄ Fourier ม ྫɿ৴߸ͷपεϖΫτϧ Figure: ಾͷ৴߸ͷपεϖΫτϧ Hayao (Shizuoka 2020) All
about cmath module February 29, 2020 26 / 33 -
ࢄ 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 -
ࢄ 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 -
ࢄ Fourier ม ྫɿ৴߸Λ෮ݩ͢Δ Figure: ٯࢄ Fourier มͰݩͷ৴߸Λ෮ݩ͢Δ Hayao (Shizuoka
2020) All about cmath module February 29, 2020 29 / 33 -
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 -
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 -
1 ͷ n ࠜ 1 ͷ 7 ࠜʹΑΔਖ਼ࣣ֯ܗͷ࡞ਤ Figure: ਖ਼ࣣ֯ܗ
ਖ਼ࣣ֯ܗఆنͱίϯύεʹΑͬͯ࡞ਤ͕Ͱ͖ͳ͍ɻ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 32 / 33 -
·ͱΊ ·ͱΊ › cmath ෳૉͷͨΊͷֶϥΠϒϥϦͰ͋Δɻ › ඪ४ϥΠϒϥϦͷൣғͰࢄ Fourier มͰ͖Δɻ ›
܅͚ͩͷ࠷ڧͷ cmath ΞϓϦέʔγϣϯΛ࣮͠Α͏ʂ Hayao (Shizuoka 2020) All about cmath module February 29, 2020 33 / 33