Pythonã«é¢ããå°ãã®èæ¯
Pythonã¯çŽ æŽãããèšèªã§ãã ãã®åã«ããã€ãã®èšèªãè©ŠããŸãããåŠæ ¡ã®ãã¹ã«ã«ã Cãã¯ã©ã¹ä»ãCãC ++-倧åŠã§ã æåŸã®2ã€ïŒ3ã€ïŒã¯ããã°ã©ãã³ã°ã«å¯Ÿãã匷ãå«æªæãæ€ãä»ããŸãããåé¡ã解決ãã代ããã«ãå²ãåœãŠãšãã¹ãã©ã¯ã¿ïŒéå»ã®æãèšèïŒãå°ç¡ãã«ããŠãäœã¬ãã«ã®ããªããã£ãã®èŠ³ç¹ããèããŸãã ç§ã®æèŠã§ã¯ãCã¯æè²ãç§åŠã®åé¡ïŒãããã«ãããæ°åŠã®åéïŒã®è§£æ±ºã«ã¯é©ããŠããŸããã 圌ãã¯ç§ã«å察ããã ãããšç¢ºä¿¡ããŠããŸãããç§ã¯èª°ãã«äœããæŒãä»ããããšã¯ããŠããŸãããåã«æèŠãè¡šããŠããŸãã
Pythonã¯ãã€ãŠå瀺ã§ããã ç§ã®äººçã§åããŠãç§ã¯Cã§æ £ç¿çãªãã®ãããé«ãæœè±¡åã®ããã€ãã®ã¬ãã«ãæžããŸããã ã¿ã¹ã¯ãšã³ãŒãã®éã®è·é¢ã¯ããã€ãŠãªãã»ã©ççž®ãããŸããã
çªç¶NISTçµ±èšãã¹ããå®è£ ããå¿ èŠããªããã°ãPythonã§ãããŸã§ãã£ãšããããã£ãŠããã ããã ã¿ã¹ã¯ã¯éåžžã«åçŽãªããã«æãããŸããé·ããæ°ã¡ã¬ãã€ãïŒ> = 10ïŒã®é åãããããã®é åã«é©çšããå¿ èŠãããäžé£ã®ãã¹ãããããŸãã
numpyã¯äœã«é©ããŠããŸããïŒ
pythonã«ã¯ãé åãæäœããããã®åªããnumpyããã±ãŒãžããããŸããããã¯ãæ¬è³ªçã«LAPACKã®ãããªé«éã©ã€ãã©ãªçšã®é«ã¬ãã«ã€ã³ã¿ãŒãã§ã€ã¹ã§ãã ç§åŠèšç®çšã®çŽ³å£«çšã»ããå šäœïŒSympyãNumpyãScipyãMatplotlibïŒãå©çšå¯èœã§ããããã«æãããŸãããããã«äœãå¿ èŠã§ããããïŒ
numpyãæ±ã£ããã¹ãŠã®äººã¯ã圌ãè¯ãããšãç¥ã£ãŠããŸããããã¹ãŠã§ã¯ãããŸããã æäœããã¯ãã«åãããããã«ããå¿ èŠããããŸãïŒRã®ããã«ïŒã ããæå³ã§ã¯ãã¢ã¬ã€å šäœã§åäžã§ãã äœããã®çç±ã§åé¡ãçªç¶ãã®ãã©ãã€ã ã«åãŸããªãå Žåãåé¡ãçºçããŠããŸãã
ã©ã®ãããªéãã¯ãã«åã¿ã¹ã¯ã«ã€ããŠè©±ããŠããã®ã§ããïŒ ã¯ããå°ãªããšãåãNISTã䜿çšããŸããBerlekamp-Messiã¢ã«ãŽãªãºã ã䜿çšããŠç·åœ¢ã·ããã¬ãžã¹ã¿ã®é·ããèšç®ããŸãã é£ç¶ãããŠãããã®æé·ãµãã·ãŒã±ã³ã¹ã®é·ããèšç®ããŸãã ç§ã¯ãåé¡ããã¯ãã«åããããã®ã«æžããäœããã®çš®é¡ã®ç¬åµçãªè§£æ±ºçãããå¯èœæ§ãæé€ããŸããã
ãããïŒ
åãNISTã®äŸãšããŠããåãæ¿ããã·ãŒã±ã³ã¹ã®æ°ãèšç®ããå¿
èŠããããŸããããåãæ¿ãããšã¯ãé£ç¶ããåäœïŒ... 1111 ...ïŒãé£ç¶ãããŒãïŒ... 000 ... ïŒãããã³ãã®éã ãããè¡ãã«ã¯ãæåŸã®èŠçŽ ïŒx [ïŒ-1]ïŒã®ãªãå
ã®ã·ãŒã±ã³ã¹ãååŸãããããã1ã·ããããã·ãŒã±ã³ã¹ïŒx [2ïŒ]ïŒãæžç®ããçµæã®æ°ããã·ãŒã±ã³ã¹ã®éãŒãèŠçŽ ã®æ°ãèšç®ããŸãã äžç·ã«ãã¹ãŠã®ããã«ãªããŸãïŒ
ããã¯å¿ã«ãšã£ãŠé¢çœããã¬ãŒãã³ã°ã®ããã«èŠãããããããŸããããããã§ã¯æ¬è³ªçã«äœãäžèªç¶ãªããšãèµ·ãã£ãŠããŸããããã¯ãã°ãããããšèªè ã«ã¯æããã§ã¯ãªãããªãã¯ã§ãã ããã¯ãŸã é ãããšã¯èšããŸã§ããããŸãã-誰ãã¡ã¢ãªå²ãåœãŠããã£ã³ã»ã«ããŠããŸããã
np.count_nonzero(x[:-1] - x[1:])
ããã¯å¿ã«ãšã£ãŠé¢çœããã¬ãŒãã³ã°ã®ããã«èŠãããããããŸããããããã§ã¯æ¬è³ªçã«äœãäžèªç¶ãªããšãèµ·ãã£ãŠããŸããããã¯ãã°ãããããšèªè ã«ã¯æããã§ã¯ãªãããªãã¯ã§ãã ããã¯ãŸã é ãããšã¯èšããŸã§ããããŸãã-誰ãã¡ã¢ãªå²ãåœãŠããã£ã³ã»ã«ããŠããŸããã
Pythonã§ã®ãã¯ãã«åãããŠããªãæäœã¯é·ãæéã§ãã numpyãååã§ãªãå Žåãã©ã察åŠããã®ã§ããïŒ éåžžã圌ãã¯ããã€ãã®ãœãªã¥ãŒã·ã§ã³ãæäŸããŸãïŒ
- ãã³ãJITã 圌女ããã³ãã®ã¿ã€ãã«ããŒãžã§èª¬æãããŠããããã«åããŠããããã¹ããŒãªãŒãçµäºãã䟡å€ããããšæããŸãã ç§ã¯å°ãåã«åœŒå¥³ãééã£ãŠããããšãå¿ããŠããŸããã äžçªäžã®è¡ã¯ãå éãæ²ããããªãç§ãæåŸ ããã»ã©å°è±¡çã§ã¯ãªãã£ããšããããšã§ãã
- ã·ãã³ã ããŠãcythonã¯Pythonã®æå³ãšç²Ÿç¥ãç Žå£ããªããæ¬åœã«çŸãããšã¬ã¬ã³ããªãœãªã¥ãŒã·ã§ã³ã§ãããšä¿¡ããŠãã人ãæãæããŠãã ããã ããã¯æããŸããã Cythonã«çããããèªåãã ãŸãã®ããããŠãC ++ãCã®ãããªããŸãæŽç·ŽãããŠããªããã®ã«åãæ¿ããããšãã§ããŸãã
- Cã®ããã«ããã¯ãæžãçŽããæããPythonããããããã€ã³ã¯ããŸãã ããããŸããããããã°ã©ã å šäœãæã£ãŠããå Žåã¯ã©ãã§ãããããèšç®ãšããã«ããã¯ããã¹ãŠã§ãã Xiã¯æäŸããŠããŸããïŒ ãã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ã2ã€ä»¥äžã®èšèªïŒPythonãšCïŒãç¥ã£ãŠããå¿ èŠããããšããäºå®ã«ã€ããŠã¯è¿°ã¹ãŠããŸããã
ãžã¥ãªã¢ãç»å ŽïŒ
æ¢åã®ãœãªã¥ãŒã·ã§ã³ãæ¡çšããè¯ããã®ãèŠã€ããããªãã£ãïŒããã°ã©ã ã§ããªãã£ãïŒããããããé«éãªããã®ã«æžãæããããšã«ããŸããã å®éã21äžçŽã«é åã®éåžžã®ãµããŒãã䜿çšããŠãæ°å€ã®è±ç©ããæžããå Žåããã¯ãã«åãããæäœã¯ããã®ãŸãŸããªã©ã§äœ¿çšã§ããŸãã ãªã©ãããªãã®éžæã¯éåžžã«å¯ã§ã¯ãããŸããïŒ
- Fortran ãããŠãç§ãã¡ã®ãã¡ããæ°ã«å ¥ãã®èšèªããBLAS / LAPACKãåŒãåºããªãã£ãã®ã¯èª°ã§ããïŒ Fortranã¯ãç§åŠèšç®ã®ããã®æ¬åœã«åªããïŒããè¯ãSIïŒïŒèšèªã§ãã Fortranã®æ代ããå€ãã®ããšãçºæãããããã°ã©ãã³ã°èšèªã«è¿œå ããããšããçç±ã§ãç§ã¯ãããåãå ¥ããŸããã§ããã ãã£ãšçŸä»£çãªãã®ãæãã§ããŸããã
- Rã«ã¯PythonïŒãã¯ãã«åïŒãšåãåé¡ããããŸãã
- Matlab-ããããã¯ããæ®å¿µãªãããã§ãã¯ãããéããããŸããã
- ãžã¥ãªã¢ -銬ã¯æããé¢éžããé¢éžããŸããïŒå®å®ããŒãžã§ã³1.0ãŸã§ã¯èªç¶ã§ããïŒ
ãžã¥ãªã¢ã®æãããªå©ç¹
- å°ãªããšãåããé«ã¬ãã«ããªPythonã®ããã«èŠããŸãããå¿ èŠã«å¿ããŠãæ°å€ãå°ãã ãäžããããšãã§ããŸãã
- ã¡ã¢ãªã®å²ãåœãŠãªã©ã§å€§éšãããŸããã
- 匷åãªåã·ã¹ãã ã ã¿ã€ãã¯ãªãã·ã§ã³ã§åŠæ¹ãããéåžžã«æäžãããŸãã åäžã®ã¿ã€ããæå®ããã«ããã°ã©ã ãäœæã§ããŸããããã䜿çšå¯èœã«ãããšãããã°ã©ã ã¯é«éã«ãªããŸãã ãããã埮åŠãªéãããããŸãã
- çµã¿èŸŒã¿åãšåããããé«éãªã«ã¹ã¿ã åãæžãã®ã¯ç°¡åã§ãã
- è€æ°ã®ãã£ã¹ãããã ããã«ã€ããŠäœæéã話ãããšãã§ããŸãããç§ã®æèŠã§ã¯ãããã¯ãžã¥ãªã¢ãæã£ãŠããæé«ã®ãã®ã§ããããã¹ãŠã®ããã°ã©ã ã®èšèšã®åºç€ã§ãããäžè¬ã«èšèªã®å²åŠã®åºç€ã§ãã
è€æ°ã®ãã£ã¹ãããã®ãããã§ãå€ãã®ããšãéåžžã«åäžã«æžãããŠããŸãã
è€æ°ã®ãã£ã¹ããããããäŸrand() # 0 1 rand(10) # 10 0 1 rand(3,5) # 3 5 .... using Distributions d = Normal() # 0, 1 rand(d) # rand(d, 10) # 10 ...
ã€ãŸããæåã®åŒæ°ã¯ãDistributionsããã®ä»»æã®ïŒ1次å ïŒååžã«ããããšãã§ããŸãããé¢æ°åŒã³åºãã¯æåéãåããŸãŸã§ãã ãããŠãé åžã ãã§ãªãïŒRNGèªäœãMersenneTwisterãªããžã§ã¯ããªã©ãšããŠè»¢éããããšãã§ããŸãïŒã å¥ã®ïŒç§ã®æèŠã§ã¯ãå®äŸãšãªãïŒäŸã¯ãloc / ilocãªãã®DataFramesã§ã®ããã²ãŒã·ã§ã³ã§ãã
- 6.é
åã¯ãã€ãã£ãã®çµã¿èŸŒã¿ã§ãã ããã«äœ¿ãããã¯ãã«åã
ã©ã¡ããç¯çœªã«ãªããã«ã€ããŠé»ã£ãŠããããšã®çæïŒ
- æ°ããèšèªã äžæ¹ã§ããã¡ãããã€ãã¹ã äœããããããæªçã§ãã äžæ¹ã圌ã¯éå»ã®å€ãã®èšèªã®ããããèæ ®ããã巚人ã®è©ã®äžã«ãç«ã¡ãå€ãã®èå³æ·±ãæ°ãããã®ãåžåããŸããã
- ããã«é«éãªããã°ã©ã ãæ©èœããå¯èœæ§ã¯äœãã§ãã 察åŠãéåžžã«ç°¡åãªéèªæãªãã®ãããã€ããããŸããããªãã¯çæãèžããã³ãã¥ããã£ã«å©ããæ±ãããããäžåºŠèžã...ãªã© ãããã¯äž»ã«ãåã®äžå®å®æ§ãåã®äžå®å®æ§ãããã³ã°ããŒãã«å€æ°ã§ãã äžè¬ã«ãç§ãèªåã§ãããéãããžã¥ãªã¢ã§ãã°ããæžãããããã°ã©ããŒã¯ããã€ãã®æ®µéãçµãŸãïŒaïŒPythonã§æžããŸãã ããã¯çŽ æŽãããããšã§ãããããã§ãããããã©ãŒãã³ã¹ã®åé¡ãçºçããããšããããŸãã bïŒCã®ãããªèšè¿°ïŒå¯èœãªå Žåã¯åžžã«åãèŠå®ããŸãã cïŒã¿ã€ããåŠæ¹ããå¿ èŠãããïŒéåžžã«èšéãããïŒå Žæãããã³å¹²æžããå ŽæãåŸã ã«ç解ããŸãã
- çæ ç³» äžéšã®ããã±ãŒãžã¯ãã©ããã§åžžã«äœããèœã¡ããšããæå³ã§çã§ãã äžéšã¯æçããŠããŸãããäºãã«ççŸããŠããŸãïŒããšãã°ãä»ã®ã¿ã€ããžã®å€æãå¿ èŠã§ãïŒã äžæ¹ã§ãããã¯æããã«æªãããšã§ãã äžæ¹ããžã¥ãªã¢ã«ã¯ãç§ãã¡ã¯å·šäººã®è©ã®äžã«ç«ã€ããšããçç±ã ãã§ãå€ãã®èå³æ·±ã倧èãªã¢ã€ãã¢ããããŸã-ç§ãã¡ã¯çæãèžãããšãšãããããã£ãŠã¯ãããªãããšããçŽ æŽãããçµéšãèç©ããŠãããããã¯ïŒéšåçã«ïŒããã±ãŒãžéçºè ã«ãã£ãŠèæ ®ãããŠããŸã
- é åã«1ããçªå·ãä»ããŸããåè«ã§ãããããã¯ãã¡ãããã©ã¹ã§ãã ãããããŸããçå£ã«ãåé¡ã¯äœã§ãããçªå·ä»ãã¯ã©ã®ã€ã³ããã¯ã¹ã§å§ãŸããŸããïŒ 5åã§æ £ããŸãã æŽæ°ãæŽæ°ã§ã¯ãªãæŽæ°ãšåŒã°ããããšã«æå¥ãèšã人ã¯ããŸããã ãããã¯ãã¹ãŠå³ã®åé¡ã§ãã ãããŠãã¯ããã¢ã«ãŽãªãºã ã«åŸã£ãŠå°ãªããšãåãã³ãŒã¡ã³ãåã-ãã¹ãŠãããããçªå·ãä»ããããŠãããéã«Pythonã§ãããããçŽãããšã¯æã äžäŸ¿ã§ãã
ããŠãé床ã¯ã©ãã§ããïŒ
ãã¹ãŠãå§ãŸã£ãçç±ãæãåºãæãæ¥ãŸããã
泚ïŒPythonãå¿ããŠããŸã£ãã®ã§ãã³ã¡ã³ãã«æ¹åç¹ãæžããŠãã ãããã©ãããããã§å®è¡ããŠã©ã³ã¿ã€ã ã確èªããŸãã
ãããã£ãŠããã€ã¯ããã³ãããŒã¯ã䜿çšãã2ã€ã®å°ããªäŸã§ãã
ãã¯ãã«åããããã®
ã¿ã¹ã¯ïŒ0ãš1ã®ãã¯ãã«Xãé¢æ°ã®å
¥åã«äŸçµŠããã1ãšïŒ-1ïŒïŒ1-> 1ã0-> -1ïŒã®ãã¯ãã«ã«å€æãããã®ãã¯ãã«ã®ããŒãªãšå€æããã®ä¿æ°ã®æ°ãèšç®ããå¿
èŠããããŸãã絶察å€ãå¢çã®å¢çãè¶
ããŠããŸãã
ããã§ã¯é©ãã¹ãããšã¯äœã衚瀺ãããŸãããåãããã«ã圌ãèªèº«ã¯ãããèæ ®ãããæé©åãããFortranããã°ã©ã ã«æž¡ããŸãã
def process_fft(x, boundary): return np.count_nonzero(np.abs(np.fft.fft(2*x-1)) > boundary) %timeit process_fft(x, 2000) 84.1 ms ± 335 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
function process_fft(x, boundary) count(t -> t > boundary, abs.(fft(2*x.-1))) end @benchmark process_fft(x, 2000) BenchmarkTools.Trial: memory estimate: 106.81 MiB allocs estimate: 61 -------------- minimum time: 80.233 ms (4.75% GC) median time: 80.746 ms (4.70% GC) mean time: 85.000 ms (8.67% GC) maximum time: 205.831 ms (52.27% GC) -------------- samples: 59 evals/sample: 1
ããã§ã¯é©ãã¹ãããšã¯äœã衚瀺ãããŸãããåãããã«ã圌ãèªèº«ã¯ãããèæ ®ãããæé©åãããFortranããã°ã©ã ã«æž¡ããŸãã
ãã¯ãã«åã§ããªããã®
0ãš1ã®é
åãå
¥åãããé£ç¶ãããŠãããã®æãé·ããµãã·ãŒã±ã³ã¹ã®é·ããèŠã€ããŸãã
éãã¯èçŒã§æããã§ãã numpyããŒãžã§ã³ããä»äžããããã³/ãŸãã¯ãã¯ãã«åããããã®ãã³ãã¯å€§æè¿ã§ãã ãŸãããããã®ããã°ã©ã ã¯ã»ãšãã©åãã§ãã ããšãã°ããžã¥ãªã¢ã«åäžã®ã¿ã€ããç»é²ããŸããã§ããïŒåã®ã¿ã€ããšæ¯èŒããŠãã ããïŒ-ãã¹ãŠåãã§ããã¹ãŠãè¿ éã«æ©èœããŸãã
def longest(x): maxLen = 0 currLen = 0 # This will count the number of ones in the block for bit in x: if bit == 1: currLen += 1 maxLen = max(maxLen, currLen) else: maxLen = max(maxLen, currLen) currLen = 0 return max(maxLen, currLen) %timeit longest(x) 599 ms ± 639 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
function longest(x) maxLen = 0 currLen = 0 # This will count the number of ones in the block for bit in x if bit == 1 currLen += 1 maxLen = max(maxLen, currLen) else maxLen = max(maxLen, currLen) currLen = 0 end end return max(maxLen, currLen) end @benchmark longest(x) BenchmarkTools.Trial: memory estimate: 0 bytes allocs estimate: 0 -------------- minimum time: 9.094 ms (0.00% GC) median time: 9.248 ms (0.00% GC) mean time: 9.319 ms (0.00% GC) maximum time: 12.177 ms (0.00% GC) -------------- samples: 536 evals/sample: 1
éãã¯èçŒã§æããã§ãã numpyããŒãžã§ã³ããä»äžããããã³/ãŸãã¯ãã¯ãã«åããããã®ãã³ãã¯å€§æè¿ã§ãã ãŸãããããã®ããã°ã©ã ã¯ã»ãšãã©åãã§ãã ããšãã°ããžã¥ãªã¢ã«åäžã®ã¿ã€ããç»é²ããŸããã§ããïŒåã®ã¿ã€ããšæ¯èŒããŠãã ããïŒ-ãã¹ãŠåãã§ããã¹ãŠãè¿ éã«æ©èœããŸãã
ãŸããæ瀺ãããããŒãžã§ã³ã¯æçµããã°ã©ã ã«ã¯å«ãŸããŠããŸããããããã«æé©åãããŠããããšã«æ³šæããŠãã ããã ããã§ã¯ããããã¯äŸãšããŠäžããããŠãããäžå¿ èŠãªè€éãã¯ãããŸããïŒJuliaã§è¿œå ã¡ã¢ãªã転éããŠrfftãã€ã³ãã¬ãŒã¹ã§å®è¡ãããªã©ïŒã
æåŸã«äœãåºãŸãããïŒ
PythonãšJuliaã®æçµçãªã³ãŒãã¯è¡šç€ºããŸãããããã¯ç§å¯ã§ãïŒå°ãªããšãä»ã®ãšããïŒã PythonããŒãžã§ã³ãçµäºããããã«åæ¢ããæç¹ã§ã16ã¡ã¬ãã€ãã®ãã€ããªæåã®é åã«å¯Ÿãããã¹ãŠã®NISTãã¹ããçŽ50ç§ã§å®äºããŸããã çŸæç¹ã®ãžã¥ãªã¢ã§ã¯ããã¹ãŠã®ãã¹ããåãããªã¥ãŒã ã§å®è¡ãããŸãïŒæ倧20ç§ïŒã ç§ã¯æãè ã§ãããæé©åãªã©ã®äœå°ããã£ãã®ãããããŸããã ãªã© ããããããã¯ç§ãšåãããã«ããã¹ãŠã®é·æãšçæããããç§ã®æèŠã§ã¯ãããã°ã©ãã³ã°èšèªã®ç空äžã®ããã°ã©ã ã®çé¢é床ã§ã¯ãªããç§ãå人çã«ã©ã®ããã«ããã°ã©ã ããããšãã§ããããèæ ®ããå¿ èŠããããŸãïŒæ¬åœã«é倧ãªãšã©ãŒãªãã«ïŒã
ãªãããããã¹ãŠæžããã®ã§ããïŒ
ããã®äººã ã¯èå³ãæã¡ãŸããã æéãæ¥ããæžãããšã«ããŸããã å°æ¥çã«ã¯ãããã€ãã®å žåçãªåé¡ã解決ããäŸãæããŠããžã¥ãªã¢ããã培åºçã«æ€èšããŠãããããšæããŸãã ãªãã§ïŒ ããå€ãã®èšèªãè¯ããã®ãç°ãªããã®ããããŠèªåã«åã£ããã®ãèŠã€ããã人ã¯èª°ã§ãèãããŠãã ããã