æ°ããããã°ãä½ãã¾ããã®ã§ãç¥ãããã¾ã
æ°ããããã°ã¯https://www.bicycle1885.org/ã§ãã ãã¡ãã¯ä»å¾æ´æ°ããªãäºå®ã§ãã ããã£ããæåã®è¨äºãã覧ãã ããã www.bicycle1885.org
Juliaã®foræã¯ãªãéã?
1年以ä¸ã¶ãã®ããã°æ´æ°ã§ããã
atsuoishimotoããã®ããã°è¨äºã§ï¼ç§ã®ããã°è¨äºãè¨åããã¦ãã¾ããã atsuoishimoto.hatenablog.com
æ¸ãã¦ããå 容ã¯ã¾ãã«ãã®éãã§ï¼foræãåããã¨èªä½ãåãç«ã¦ã¦é ãããã§ã¯ãªãï¼ãã®ä¸ã§ä½ããã®å¦çãããéã«åçåä»ãè¨èªã§ããPythonã®æ§è³ªä¸é¿ãã¦éããªãå¦çãããï¼ãã®å¦çãforæã®ä¸ã§ä½åº¦ãç¹°ãè¿ããã¨ã§çµæã¨ãã¦foræå ¨ä½ã®å¦çãé ããªãã¨ããå 容ã§ããã
ããããã¨ï¼ã²ã¨ã¤ã®çåã¨ãã¦ï¼ãªãåãåçåä»ãè¨èªã§ããã¯ãã®Juliaè¨èªã§ã¯åæ§ã®å¦çãé«éãªã®ããæ°ã«ãªãã¨ããã§ãããããã®è¨äºã§ã¯ãã®ããããå°ã解説ãããã¨æãã¾ãã
Juliaã®åæ¨è«
åé¡ã¨ãªã£ã¦ããã³ã¼ãã¯ï¼ä»¥ä¸ã®ããã«ï¼ã¤ã®ãã¯ãã«ã®å ç©ãè¨ç®ããã³ã¼ãã§ãã
function dot(a, b) s = zero(eltype(a)) for i in 1:endof(a) s += a[i] * b[i] end return s end
ãã®ã³ã¼ãã¯ï¼ç¹ã«åã®æå®ãªã©ããªãã«ãé¢ãããï¼Pythonã§ä¼¼ãã³ã¼ããæ¸ããå ´åã¨æ¯ã¹ã¦é常ã«é«éã«åä½ãã¾ãã
Julia:
julia> a = ones(100000); b = ones(100000); julia> @benchmark dot(a, b) BenchmarkTools.Trial: memory estimate: 16 bytes allocs estimate: 1 -------------- minimum time: 121.605 μs (0.00% GC) median time: 121.666 μs (0.00% GC) mean time: 132.996 μs (0.00% GC) maximum time: 1.456 ms (0.00% GC) -------------- samples: 10000 evals/sample: 1
In [2]: %timeit dot(a, b) 31.4 ms ± 680 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
ããã§Pythonãããã»ã©é
ããªãçç±ã¯ï¼å
ã®ããã°è¨äºã§è§£èª¬ããã¦ããã¨ããï¼foræã®ä¸ã§è¡ããã¦ããä¸ã¤ä¸ã¤ã®è¨ç®ã®è£ã§æ§ã
ãªä»éããå¦çãè¡ããã¦ããããã§ããä¾ãã°ï¼ä¹ç®ãè¡ã*
æ¼ç®åã§ã¯ï¼ãããã被æ¼ç®åã®åãä¹ç®ããµãã¼ããã¦ããããªã©ã®ç¢ºèªãå¿
è¦ã§ããåçè¨èªã§ã¯å¤æ°ã®åãåçã«æ±ºå®ãããããï¼ããããå¦çãä»éããã®ã¯é¿ãã¦éããªãããã«ãæãã¾ããï¼Juliaã¯ããããå¦çãçç¥ãããã¨ã§é«éåãéæãã¦ãã¾ãã
Juliaã¯ãªãããããå¦çãçç¥ã§ããã®ã§ãããã? ãã®çãã¯Juliaã®åæ¨è«ã«ããã¾ããå®ã¯ï¼Juliaã®å¦çç³»ã¯é¢æ°ãå®è¡ããåã«åæ¨è«ãè¡ãã¾ããJuliaã®åæ¨è«å¨ã¯ï¼é¢æ°ã®ã³ã¼ããè¦ã¦åã£ã¦ï¼åã決å®ã§ããã¨ããã«èªåçã«å注éãä»ãã¦åãã¾ããå
ã»ã©ã®ã³ã¼ããä¾ã«å°ã説æãã¦ã¿ã¾ãããããã®ã³ã¼ãä¾ã§ã¯ï¼dot(a, b)
ã¨ããå¼ã³åºããèµ·ããéã«å¼æ°ã§ããa
ã¨b
ã®åã決å®ãã¾ããä¸ã®ä¾ã§ã¯ä¸¡æ¹ã¨ãVector{Float64}
(ããã¯Array{Float64,1}
ã®å¥å) ã¨ããFloat64
å (å精度ã®æµ®åå°æ°ç¹æ°ã®å) ãè¦ç´ ã¨ãã1次å
é
åã§ããããããã¨å½ç¶foræã®ä¸ã«ããa[i]
ãb[i]
ã¯Float64
åã®å¤ãåãåºããã¨ãåããã¾ããããã¨ï¼a[i] * b[i]
ã¨ããé
åã®i
çªç®ã®è¦ç´ ãåãåºãã¦ç©ãè¨ç®ããå¦çã¯ï¼Float64
å士ã®ç©ã ã¨åããã¾ãã®ã§ï¼è¢«æ¼ç®åãä¹ç®ããµãã¼ããã¦ãããã©ããããã¡ãã¡foræã®å
é¨ã§ç¢ºèªããå¿
è¦ã¯ããã¾ãããããããå¦çãåæã«è¡ã£ã¦ãããã®ãåæ¨è«ã§ãã
åæ¨è«ã®çµæãè¦ã¦ã¿ã¾ãããã@code_typed
ã¨ãããã¯ãã使ãã¨ï¼é¢æ°ã®åæ¨è«ã®çµæã表示ãããã¨ãã§ãã¾ãã
julia> @code_typed dot(a, b) CodeInfo(:(begin s = (Base.sitofp)(Float64, 0)::Float64 # line 3: $(Expr(:inbounds, false)) # çç¥ SSAValue(4) = (Base.arraysize)(a, 1)::Int64 # çç¥ $(Expr(:inbounds, :pop)) SSAValue(5) = (Base.select_value)((Base.slt_int)(SSAValue(4), 0)::Bool, 0, SSAValue(4))::Int64 SSAValue(6) = (Base.select_value)((Base.sle_int)(1, SSAValue(5))::Bool, SSAValue(5), (Base.sub_int)(1, 1)::Int64)::Int64 #temp# = 1 17: unless (Base.not_int)((#temp# === (Base.add_int)(SSAValue(6), 1)::Int64)::Bool)::Bool goto 27 SSAValue(7) = #temp# SSAValue(8) = (Base.add_int)(#temp#, 1)::Int64 i = SSAValue(7) #temp# = SSAValue(8) # line 4: s = (Base.add_float)(s, (Base.mul_float)((Base.arrayref)(a, i)::Float64, (Base.arrayref)(b, i)::Float64)::Float64)::Float64 25: goto 17 27: # line 6: return s end))=>Float64
ä¸ã®æ¹ãè¦ã¦ã¿ãã¨ï¼(Base.mul_float)((Base.arrayref)(a, i)::Float64, (Base.arrayref)(b, i)::Float64)::Float64
ã¨æ¸ãããé¨åãããã®ããããã¨æãã¾ãããã®é¨åãã¾ãã«a[i] * b[i]
ã®è¨ç®ã«ã«å¯¾å¿ããé¨åã§ï¼ããããã®è¢«æ¼ç®åã¨æ¼ç®çµæãFloat64
ã«ãªããã¨ãã¡ããã¨æ¨è«ã§ãã¦ãã¾ããã¾ãï¼ä¸çªæå¾ã«=>Float64
ã¨æ¸ããã¦ããããã«ï¼dot(a, b)
ã®è¨ç®çµæãFloat64
åã§ãããã¨ãæ¨è«ããã¦ãã¾ããè¨ç®çµæã®åãæ¨è«ã§ãããã¨ã§ï¼ä»ã®é¢æ°ããdot
é¢æ°ãå¼ã³åºããã¨ãããã®è¨ç®çµæãæ¨è«ã§ãï¼åã®æ
å ±ãã©ãã©ãä¼æãã¦ããã¾ãã
åæ¨è«ã¯ï¼ã½ã¼ã¹ã³ã¼ãã®ä¸ã§ã¤ã³ã¿ã¼ããªã¿ãå®è¡ããããã«è¡ãã¾ãããã®ã¤ã³ã¿ã¼ããªã¿ã¯å¼æ°ã®å¤ãªã©ãæ±ãã¤ã³ã¿ã¼ããªã¿ã§ã¯ãªãï¼åãæ±ãã¤ã³ã¿ã¼ããªã¿ã§ããåæ¨è«ã®è©³ããã¢ã«ã´ãªãºã ã¯å®è£ ãèªããï¼Inference Convergence Algorithm in Julia㨠Inference Convergence Algorithm in Julia - Revisitedãªã©ã®è§£èª¬ãåç §ãã¦ä¸ããã
ã³ã¼ãã®çæ
ã½ã¼ã¹ã³ã¼ãã«ç¾ããå¤æ°ãé¢æ°ã®è¿ãå¤ã®åãåæ¨è«ã§å¤æããã¨ï¼ããã¤ãã®ç¢ºèªå¦çãçãã ãã§ãªãï¼Juliaã¯ãã®åã«ç¹åããå½ä»¤ãçæãããã¨ãã§ãã¾ããé¢æ°ã®å®è¡æã«ã¯ã³ã¼ãçæãè¡ããã¨ã§å¦çé度ãä¸ãã¦ãã¾ãããã®ä¸éç¶æ
ãè¦ã@code_llvm dot(a, b)
ã¨ãããã¯ããããã¾ãã®ã§ï¼çµæãè¦ã¦ã¿ã¾ãããã
julia> @code_llvm dot(a, b) define double @julia_dot_62600(i8** dereferenceable(40), i8** dereferenceable(40)) #0 !dbg !5 { # çç¥ idxend2: ; preds = %idxend %18 = add i64 %"#temp#.08", 1 %19 = getelementptr double, double* %10, i64 %13 %20 = load double, double* %19, align 8 %21 = getelementptr double, double* %12, i64 %13 %22 = load double, double* %21, align 8 %23 = fmul double %20, %22 %24 = fadd double %s.09, %23 %25 = icmp eq i64 %"#temp#.08", %4 br i1 %25, label %L27.loopexit, label %if }
ãã®çµæã¯LLVM IRã¨ããè¨èªã§æ¸ããã¦ãã¾ããLLVM IRã¯æ©æ¢°èªã«è¿ãä½ã¬ãã«ãªå½ä»¤åã§ï¼Cè¨èªãªã©ã®ã³ã³ãã¤ã©ã§ããclangãRustã®ããã¯ã¨ã³ãã§ã使ããã¦ãããã®ã§ãã
ãã®çµæãè¦ãã¨%20 = load double, double* %19, align 8
ã¨ããå½ä»¤ã§ã¡ã¢ãªã®ããé¨åããdouble
ã®å¤ãèªã¿åºãã¦ããã®ããããã¾ããããã¯ã¡ããã©a[i]
(ãããã¯b[i]
)ã«ç¸å½ããå¦çã§ããã¾ãï¼èªã¿åºããå¤ã®ç©ãè¨ç®ãã%23 = fmul double %20, %22
ã¨ããå½ä»¤ãããã®ãåããã¾ããfmul
ã¨ããå½ä»¤ã¯æµ®åå°æ°ç¹æ°ã®ç©ãè¨ç®ããã®ã«ç¹åããå½ä»¤ã§ã ('fmul' Instruction)ãã§ã¯a
ãb
ã®è¦ç´ ãFloat64
ã§ãªãæ´æ°åã®Int64
ã ã£ããã©ããªãã§ãããã? ããããå ´åã§ãJuliaã¯åæ¨è«ãè¡ã£ã¦Int64
åã«ç¹åããç©ã®æ¼ç®å½ä»¤ãçæãã¾ãããã®ããã«åæ¨è«ã®çµæã使ã£ã¦ããå¦çã«ç¹åããå½ä»¤ã使ããã¨ã§ï¼é常ã«é«å¹çãªã³ã¼ããçæãã¾ããã«ã¼ã1åãããã§ã¯å¤§ããéãã¯ããã¾ãããï¼é·ãåãã«ã¼ãã ã¨ç©ããç©ãã£ã¦å¤§ããªå·®ã«ãªãã¾ãããããã¦çæãããé¢æ°ã®ã³ã¼ãã¯ï¼æ¬¡å以éã«åãåã®å¼æ°ãããã¨åå©ç¨ããã¾ãã
å¯ä½ç¨
Juliaã¯é¢æ°ã®å®è¡æã«åæ¨è«ã¨ã³ã¼ãçæãè¡ããã¨ã説æãã¾ãããï¼ããã«ã¯å¯ä½ç¨ãããã¾ããåãã¦é¢æ°ãå¼ã³åºãéã«åæ¨è«ã¨ã³ã¼ãçæã»æé©åã«ãããã³ã¹ããä¸ä¹ãããã¾ãããã®ã³ã¹ãããã¤ããã»ã©ã®ãã®ãã©ããã確èªãã¹ããªã®ã§ããããï¼ç¾å¨ã®Julia (v0.6.2)ã¯ç¹æ®ãªãã¨ãããªãéãå¿ ãåæ¨è«ã¨ã³ã¼ãçæãè¡ãã¾ããã§ãã®ã§ï¼ååã®é¢æ°å¼ã³åºãã§ã¯ã³ãã³ãé ãããï¼ããã巨大ãªããã±ã¼ã¸ã«ãªãã¨ç©ããç©ãã£ã¦æ°ç§ããæ°åç§ã®é ãã«ãªã£ãããã¾ããã¨ã¦ãè¯ãä¾ããTwitterã§è¦ã¤ããã®ã§å¼ç¨ãããã¨æãã¾ãã
ããã¯äººéã¨è»ãä¸ã¡ã¼ãã«ç«¶èµ°ã§ã©ã£ã¡ãæ©ãããè¦ã¦ãã®ã¨åããã¨ã ã¨ãããã¾ãï¼
— ãã¾ãµãããã (@MathSorcerer) 2018å¹´1æ7æ¥
å°æ¥çã«ãã£ã¨Juliaã®å¦çç³»ãè³¢ããªãã°ï¼èªåãä»1ã¡ã¼ãã«èµ°ããããã¨ãã¦ãã®ããã«ãã©ã½ã³ããããã¨ãã¦ãã®ãåããããã«ãªãï¼ããããå¯ä½ç¨ã®å¹æã軽æ¸ãããããã«ãªãã¨æãã¾ãã
Pythonã§åããã¨ãã§ããªãã®ã
Numbaã¨ããããã¸ã§ã¯ãã¯ï¼Juliaã«ä¼¼ããããªåæ¨è«ã¨ã³ã¼ãçæãè¡ã£ã¦ããããã§ãã
使ãæ¹ã¯ç°¡åã§ï¼@jit
ã¨ããã¢ããã¼ã·ã§ã³ãé¢æ°ã«ã¤ããã ãã§ãã
import numba @numba.jit def dot(a, b): s = 0.0 for i in range(len(a)): s += a[i] * b[i] return s
å®éã«ããã試ãã¦ã¿ãã¨ï¼Juliaã¨åãã¬ãã«ã®é«éãªã³ã¼ãã«ãªãã¾ããã
In [2]: %timeit dot(a, b) 133 µs ± 1.43 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
ã©ã®ç¨åº¦è¤éãªã³ã¼ãã¾ã§ä¸æãè¡ãã®ãã¯åããã¾ãã(ä¾ãã°ï¼ã¦ã¼ã¶å®ç¾©åã§ãã¡ããã¨æé©åã§ããã®ã)ãï¼é常ã«é¢ç½ãããã¸ã§ã¯ãã ã¨æãã¾ããä»å¾ï¼RãRubyã§ãä¼¼ããããªã¢ããã¼ããåºã¦ããããããã¾ãããã
EzXML.jlãä½ã£ã話
å æã§ãããEzXML.jlã¨ããXMLãHTMLãæ±ãããã®ããã±ã¼ã¸ããªãªã¼ã¹ãã¾ããã EzXML.jlã¯libxml2ã¨ããCè¨èªã§æ¸ãããã¡ã¸ã£ã¼ãªã©ã¤ãã©ãªã®ã©ããã¼ãªã®ã§ããããããJuliaã§ã©ããããéç¨ã§è²ã ã¨å·¥å¤«ãããã¾ããã®ã§ããã®è¾ºãå ±æãããã¨æãã¾ããã¾ããããã±ã¼ã¸ã®éçºã«ã¤ãã¦ãç§ã®æãã¨ãããè²ã ã¨ç´¹ä»ãã¦ããããã¨ãå± ã¾ãã®ã§ãããããJuliaã®ããã±ã¼ã¸ãä½ã£ã¦ã¿ããã¨æãæ¹ã«ã¯åèã«ãªããã¨æãã¾ãã
ä½æ ä½ã£ãã
ãããããJuliaã«ã¯LightXML.jlã¨ããããã±ã¼ã¸ãããããã¡ããlibxml2ãã©ããããããã±ã¼ã¸ã§EzXML.jlã¨éè¤ããé¨åãããªãããã¾ããLightXML.jlã¯Juliaã®XMLãæ±ãããã±ã¼ã¸ã®ä¸ã§ã¯æããã使ããã¦ãããã®ã§ãå¤ãã®ä¾åããã±ã¼ã¸ãããã¾ããããããªãããããã¤ãåé¡ããããç§ã¯ãã®ããã±ã¼ã¸ã«ããããéãã§ããªãæ°ããããã±ã¼ã¸ãä½ããã¨ã«ãã¾ããã
LightXML.jlã§åé¡ã ã¨æããããã®ãåæããã¨ä¸»ã«ä»¥ä¸ã®æ§ãªãã®ãæãããã¾ãã * APIãJuliaã£ã½ããªã * åã®è¨è¨ããã¾ããã¾ããªã * æ©è½ãå°ãªã * Cå´ã§ç¢ºä¿ãããã¡ã¢ãªã¼ãèªåçã«è§£æ¾ãã¦ãããªã
ã¾ããAPIã«é¢ãã¦ã§ããå¦ãªååã®é¢æ°ãæä¾ããã¦ãã¾ããä¾ãã°ãXMLãã¡ã¤ã«ããã¼ã¹ãã¦èªã¿è¾¼ãé¢æ°ã¯parse_file
ã¨ããä½ãããã®ãåãããªãååã§ããJuliaã§ã¯ããã£ã¡ã
ãusing
ã使ã£ã¦ããã±ã¼ã¸ããæä¾ããã¦ããé¢æ°ãããã±ã¼ã¸åãä»ããã«å©ç¨ãã¾ããããä¾ãã°ã¡ãã£ã¨å¤§ããã®ããã°ã©ã ã§parse_file
ã¨ããé¢æ°ãå¼ã°ãã¦ãã¦ãããããXMLãèªã¿è¾¼ãé¢æ°ã ã¨ã¯ãªããªãæãã¾ãããã¾ããå¹¾ã¤ãã®é¢æ°åã«ã¯child_nodes
ãis_elementnode
ãªã©ã¢ã³ãã¼ã¹ã³ã¢ã使ããã¦ãã¦ããããJuliaã£ã½ãé¢æ°ã®å½åæ³ã§ã¯ãªãã§ããä¾ãã°ãJuliaã®æ¨æºé¢æ°ãè¦ã¦ã¿ãã¨eachline
ãhaskey
ãªã©ã¢ã³ãã¼ã¹ã³ã¢ã使ããªãé¢æ°åãä¸è¬çã§ãã¹ã¿ã¤ã«ã¬ã¤ãã«ããã®ããã«è¨è¿°ããã¦ãã¾ãã
åã«é¢ãã¦ããã¡ãã£ã¨è¨è¨ããããªãæ°ããã¾ããXMLã®DOMã¢ãã«ã§ã¯ãXMLã®ææ¸ä¸ãæ§æããè¦ç´ ããã¼ãã¨å¼ã³ã<element/>
ã®ãããªè¦ç´ ãã¼ãã<!-- comment -->
ã®ãããªã³ã¡ã³ããã¼ããªã©æ§ã
ãªç¨®é¡ã®ãã¼ããããã®ã§ãããLightXML.jlã§ã¯ãã®ãããªãã¼ããå¥ã
ã®Juliaã®åã§ã©ãããã¦ãã¾ããã§ãã®ã§ã以ä¸ã®ããã«ãã¼ããè¦ã¤ããã¨ãã®ç¨®é¡ãå¤å¥ãã¦ç®çã®åã®ãã¼ãã®ãã£ã¹ãããå¿
è¦ãããã¾ãã
for c in child_nodes(xroot) if is_elementnode(c) e = XMLElement(c) # ... end end
ããã¯ããã«ãé¢åãããã§ãããä¸åº¦çæãããªãã¸ã§ã¯ããå¥ã®ãªãã¸ã§ã¯ãã¸ãã£ã¹ããããã¨ã§ä½è¨ãªãªãã¸ã§ã¯ãã®çæãèµ·ãã¾ããããããã©ã¼ãã³ã¹ä¸ããã¾ããããããªãã¨æãã¾ãã
ã¾ããlibxml2ã¯åå空éãXPathãªã©æ§ã ãªæ©è½ãå®è£ ãã¦ããã®ã§ãããLightXML.jlããæä¾ããã¦ããã®ã¯ãã®ããä¸é¨ã§XMLã®èªã¿æ¸ãã¨DOMæä½ã ãã§ããããã«ãlibxml2ãéãã¦ç¢ºä¿ããã¡ã¢ãªã¼é åã¯ãJuliaå´ããã®åç §ããªããªã£ã¦ãèªåçã«ã¯è§£æ¾ãã¦ãããªãã®ã§ãèªåã§è§£æ¾ããªãã¨ã¡ã¢ãªã¼ãªã¼ã¯ãèµ·ããã¾ãã
ãããã®åé¡ã解決ãã¦å¿«é©ã«XMLãæ±ããããã«ãããããEzXML.jlã¨ããæ°ããããã±ã¼ã¸ãéçºãããã¨ã«æ±ºãã¾ããã
EzXML.jlã®å£²ã
EzXML.jlã®å£²ãã¯ããã®ååã®éãç°¡åã«(easy)ã«XMLãæ±ããããã«ãããã¨ã§ãããã®ãããAPIãåã®è¨è¨ãããä¸åº¦èãç´ãããã¤libxml2ãæä¾ãã便å©ãªæ©è½ã使ããããã«ãã¦ãã¾ãã次ã®ã³ã¼ãã¯ãEzXML.jlã使ã£ã¦XMLã®ããªã¼ã辿ã£ãããXPathã使ããã¢ã§ãã
using EzXML # Parse an XML string # (use `readxml(<filename>)` to read a document from a file). doc = parsexml(""" <primates> <genus name="Homo"> <species name="sapiens">Human</species> </genus> <genus name="Pan"> <species name="paniscus">Bonobo</species> <species name="troglodytes">Chimpanzee</species> </genus> </primates> """) # Get the root element from `doc`. primates = root(doc) # Iterate over child elements. for genus in eachelement(primates) # Get an attribute value by name. genus_name = genus["name"] println("- ", genus_name) for species in eachelement(genus) # Get the content within an element. species_name = content(species) println(" â ", species["name"], " (", species_name, ")") end end println() # Find texts using XPath query. for species_name in content.(find(primates, "//species/text()")) println("- ", species_name) end
EzXML.jlãXMLãã¡ã¤ã«ãèªã¿è¾¼ãã¨ãã®é¢æ°åã¯readxml
ã§ããããã¯ãæ¨æºã©ã¤ãã©ãªã«ãããreadcsv
ã«æ²¿ã£ã¦å½åãããã®ã§ããread
ã¨ããé¢æ°åã¯Juliaã®æ¨æºã©ã¤ãã©ãªã§ã¯ãã¡ã¤ã«ããèªã¿è¾¼ãéã«ä½¿ãããä¸è¬çãªåè©ã§ãã®ã§ããã®ç¹ã§ãä¸è²«æ§ãããã¾ããæååãããã¼ã¿ãèªã¿è¾¼ãå ´åã«ã¯parsexml
ã¨ããé¢æ°åã«ãªã£ã¦ãã¦ããããJuliaã§ã¯parse
ãæååã«å¯¾ãã¦é »ç¹ã«ä½¿ããããã¨ãåæ ããã¦ãã¾ããããè¦ç´ ã®å±æ§ãåãã«ã¯è¾æ¸(Dict
)ã¨åãããã«element[name]
ã¨ããæ§æã使ãã¾ããå¤ãã»ããããã«ã¯ãã¡ããelement[name] = value
ã¨æ¸ãã¾ããä»ã®é¢æ°ã«é¢ãã¦ããä¸è²«æ§ããã£ã¦ä¸åº¦ç解ããã°ãã¾ãã¬ãã¡ã¬ã³ã¹ãå¼ããªãã¦ãããã°ã©ãã³ã°ã§ããããã«ãªã£ã¦ãã¾ãã
åã®è¨è¨ã«é¢ãã¦ããªãã¹ãã·ã³ãã«ã«ãªãããã«ãã¦ãã¾ããEzXML.jlãæä¾ãã¦ããåã¯ä¸»ã«EzXML.Document
ã¨EzXML.Node
ã®2種é¡ã ãã§ããEzXML.Document
ã¯XMLææ¸å
¨ä½ã«å¯¾å¿ããåã§ãEzXML.Node
ã¯ãã®æ§æè¦ç´ ã§ãããã¼ãã表ãåã§ããEzXML.Node
ã¯XMLä¸ã®ãã¼ãã£ã½ããã®ãã¹ã¦ãã©ãããã¾ãã®ã§ãè¦ç´ ããããã¹ãããã³ã¡ã³ãããå±æ§ããã¹ã¦ãEzXML.Node
ã§ãããã®ãã¼ããã©ã®ãããªç¨®é¡ã®ãã¼ããç¥ãã«ã¯nodetype
é¢æ°ãå¼ã³ã¾ãããããã®åã®ååã¯ã³ã¼ãä¸ã§ä½¿ããããã¨ã¯ãã¾ããªãã§ããããããã±ã¼ã¸ã¨ãã¦ã¯exportãã¦ãã¾ãããããããå¿
è¦ã§ããã°ä¸ã®ããã«ã¢ã¸ã¥ã¼ã«åãä»ãã¦ä½¿ããã¨ãã§ãã¾ãã
æ©è½ãLightXML.jlããè±å¯ã§ããåå空éãæ±ããã¨ãã§ãã¾ãããXPathã使ã£ã¦ç®çã®è¦ç´ ãæ¤ç´¢ãããã¨ãã§ãã¾ããXPathã¯find
ã¨ããé¢æ°ããªã¼ãã¼ãã¼ããã¦ãã¦ãfind(node, "//foo")
ã¨ããã°node
ãã¼ã以ä¸ã®foo
è¦ç´ ããã¹ã¦åå¾ãããã¨ãã§ãã¾ããã¾ããã¡ã¢ãªã«ä¹ãåããªã大è¦æ¨¡ãªXMLãã¡ã¤ã«ãé«éã«ãã¼ã¹ããããã«ãstreaming readerãæä¾ãã¦ãã¾ãã
æå¾ã«ãEzXML.jlã¯ã¡ã¢ãªãªã½ã¼ã¹ãèªåçã«è§£æ¾ãã¦ããã¾ããæ®éã«Juliaã使ã£ã¦ãã人ã¯ã¡ã¢ãªãªã½ã¼ã¹ã®è§£æ¾ãªãã¦æ°ã«ããããªãã§ããããããèªåçã«ãã£ã¦ãããã°å¤§å¤æ¥½ã§ãããªã®ã§ãEzXML.jlã§ã¯ãã®è¾ºã®é¢åãè¦ã¦ããããã¨ã«ãã¦ãã¾ãããã®æ¬¡ã§ã¯ããã®å®è£ ãã©ã®ããã«ãªã£ã¦ããã®ãã説æãã¦ããã¾ãã
å é¨å®è£
ã¾ãã¯Juliaã§Cã®ã©ã¤ãã©ãªã¼ãã©ããããéã®åºæ¬çãªãã¨ã確èªãã¦ããã¾ããããJuliaã®ãªãã¸ã§ã¯ãã¨Cè¨èªã®æ§é ä½ã§ã¯ã¡ã¢ãªã¬ã¤ã¢ã¦ãã«äºææ§ãããã¾ããä¾ãã°ãstruct point_s { int kind; double x; double y; }
ã¨ããæ§é ä½ãããã°ãJuliaå´ããã¯immutable Point; kind::Cint; x::Float64; y::Float64; end
ã®ãããªåã®ãªãã¸ã§ã¯ããå®ç¾©ãã¦ããã°ããã®ã¡ã¢ãªã¬ã¤ã¢ã¦ãããã®ã¾ã¾Juliaå´ã«åããã¨ãã§ãã¾ããCã®é¢æ°ããpoint_s*
ã®ãããªãã¤ã³ã¿å¤ãè¿ãããå ´åã¯ãJuliaå´ã§unsafe_load(ptr::Ptr{Point})
ã使ã£ã¦Juliaã®Point
ãªãã¸ã§ã¯ãã«å¤æã§ãã¾ããã¾ããCè¨èªã®é¢æ°ã®å¼ã³åºãã¯ccall
ã§å®ç¾ã§ãã¾ãã
EzXML.jlã§ã¯ãlibxml2ã®ãã¼ããæ±ãæã¯ä»¥ä¸ã®_Node
åã«åãã¦ãã¾ããããã¯ãlibxml2ã®ã©ããªç¨®é¡ã®ãã¼ããå
±éãã¦æã£ã¦ãããã£ã¼ã«ãã§ããå¾ã§è¿°ã¹ã¾ãããæåã®_private
ãã£ã¼ã«ããéè¦ã§ãã
immutable _Node _private::Ptr{Void} typ::Cint name::Cstring children::Ptr{_Node} last::Ptr{_Node} parent::Ptr{_Node} next::Ptr{_Node} prev::Ptr{_Node} doc::Ptr{_Node} end
Juliaã¯ãªãã¸ã§ã¯ãã解æ¾ãããã¨ãã«é¢æ°ãå¼ã³åºãä»çµã¿(finalizer
é¢æ°)ãæä¾ãã¦ãããããã使ããã¨ã§ãªã½ã¼ã¹ã®è§£æ¾ãã§ãã¾ãããããããã¯ãæ³åããããã¡ãã£ã¨è¤éã§ããä½æ
ãªããXMLã®ãã¼ãã¯libxml2å
ã§ç¹ãã£ã¦ãã¾ãããã誤ã£ã¦ã¤ãªãã£ã¦ããä¸é¨ã®ãã¼ããåæã«è§£æ¾ãã¦ãã¾ãã¨ãä¸è²«æ§ãä¿ãããªããªã£ã¦ãã¾ãã¾ãã以ä¸ã®ã³ã¼ããè¦ã¦ã¿ã¾ãããã
function extract_first_child(filename) doc = readxml(filename) r = root(doc) c1 = firstchild(r) return c1 end # foo.xml: # <root> # <child1/> # <child2/> # </root> child = extract_first_child("foo.xml")
ãã®é¢æ°ã§ã¯XMLææ¸ä¸ã®æåã®è¦ç´ ãè¿ãã¦ãã¾ããé¢æ°ãè¿ã£ãå¾ã¯Juliaã¬ãã«ã§ã¯doc
ã¸ã®åç
§ãç¡ããªãã¾ããããJuliaã®ã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³(GC)ã¯doc
ã解æ¾ãããã¨ãã¾ãããã®ã¨ãã«libxml2ã®ã¡ã¢ãªã¼ã解æ¾ãã¦è¯ãããã«æãã¾ãããå®éã«ã¯ããã§ã¯ããã¾ãããdoc
ãåç
§ãã¦ããlibxml2ã®æ§é ä½ã¯r
ãéãã¦c1
ã«ã¤ãªãã£ã¦ãã¦ãc1
ãdoc
ã¸ã®ãã¤ã³ã¿ã¼ãæã£ã¦ãã¾ããã§ãã®ã§ãé åã解æ¾ããã«ã¯ãXMLã®ããªã¼ã®ã©ãã«ãJuliaããã®åç
§ãæ®ã£ã¦ããªãç¶æ
ã§ããå¿
è¦ãããã¾ãã
ãã®åé¡ãåé¿ããããã«ãEzXML.jlã§ã¯ã¡ã¢ãªã¼ã®è§£æ¾ã¯å¸¸ã«ç¹å®ã®ãã¼ãããèµ·ããããã«ãã¦ãã¾ããå®è£
ä¸ã¯ãNode
åã®ãªãã¸ã§ã¯ãã¯ptr
ã¨ããlibxml2ã®ãã¼ãã表ãæ§é ä½ã¸ã®ãã¤ã³ã¿ã¼ã¨èªèº«ã管çãã¦ããowner
ã¨ãã2ã¤ã®ãã£ã¼ã«ããæã£ã¦ãã¾ãããã®owner
ãªãã¸ã§ã¯ããä»ã®ãã¼ãã®ã¡ã¢ãªã¼ç®¡çãæ
ã£ã¦ãããªãã¸ã§ã¯ãã§ãã
type Node ptr::Ptr{_Node} owner::Node end
Node
ã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã®å
é¨ã§ã¯ã以ä¸ã®æ§ãªã³ã¼ãã使ã£ã¦æä¸ä½ã®Node
ã®ãã¤ã³ã¿ã¼ãæç¹°ãå¯ãã¦ããã®ãã¼ãã®owner
ã«æå®ãã¦ãã¾ããã¤ã¾ãããªã¼ãã¼ã¯XMLããªã¼ã®æä¸ä½ã«ãããã¼ãããã®å½¹å²ãæããã¾ãã
# determine the owner of this node owner_ptr = ptr while unsafe_load(owner_ptr).parent != C_NULL owner_ptr = unsafe_load(owner_ptr).parent end
ãã®owner
ãã£ã¼ã«ããæã¤çç±ã¯ä»ã«ãããã¾ããowner
ãæã¤ãã¨ã§ãGCããã®å(åå«)ãã¼ãããå
ã«ãªã¼ãã¼ã解æ¾ãããã¨ããã®ãé²ããã¨ãã§ãã¾ããä¸ã®é¢æ°ã®ä¾ã§è¨ãã°ãc1
ãdoc
ã¸ã®åç
§ãJuliaã®ã¬ãã«ã§ä¿æãã¦ãããããc1
ãããéãdoc
ãå
ã«è§£æ¾ããããã¨ã¯ããã¾ãããããã«éä¸ã«æã¾ãã¦ããr
ã¯èªèº«ãä»ã®ãã¼ãã®ãªã¼ãã¼ã§ã¯ãªãã®ã§ãJuliaã¯r
ãGCããã¨ãã«libxml2ã§ç¢ºä¿ãããã¡ã¢ãªã¼é åã解æ¾ãã¾ããããããã¦ã常ã«ãã¼ãã¯æä¸ä½ã®ãã¼ãã¸ã®åç
§ãæã¡ãæä¸ä½ã®ãã¼ããã¡ã¢ãªã¼ã解æ¾ãããã¨ã«ãã¦ã誤ã£ã¦GCãããã®ãé²ãã§ãã¾ãã
ãããå®ã¯ãããã²ã¨ã¤èããªããã°ãããªãåé¡ãããã¾ããããã¯ãJuliaå´ããããlibxml2ã®ãã¼ãã«å¯¾ãã¦è¤æ°ã®åç §ãä½ã£ã¦ãã¾ããã¨ã§ãã以ä¸ã®æ§ãªç°¡åãªã³ã¼ããèãã¦ã¿ãã¨ãããã«åé¡ãåããã¾ãã
doc1 = readxml("foo.xml")
doc2 = document(root(doc1))
ãã®ã¨ããåç´ã«å®è£
ããã¨doc1
ã¨doc2
ã¯libxml2ã®åããã¼ããæãã¦ããã®ã«å¥ã
ã®Juliaãªãã¸ã§ã¯ãã«ãªã£ã¦ãã¾ããããã¨ããããã¼ãã®å¯¾ãã¦2ã¤ã®ãªã¼ãã¼ãåå¨ãããã¨ã«ãªãã両è
ãæçµçã«åããã¼ãã解æ¾ãããã¨ãã¦å¾ã®ã»ããä¸æ£ãªæä½ã«ãªãã¾ãã
ãã®åé¡ã¯libxml2ã®ãã¼ãã®æ§é ä½ã«Juliaãªãã¸ã§ã¯ãã¸ã®ãã¤ã³ã¿ã¼ãä¿æãããã¨ã§è§£æ±ºãã¦ãã¾ããæ¢ã«è¿°ã¹ãããã«ãlibxml2ã®ãã¼ãã®æ§é ä½ã«ã¯_private
ã¨å¼ã°ãããã£ã¼ã«ãããããã¦ã¼ã¶ã¼ãèªç±ã«ä½¿ããã¨ãã§ãã¾ããããã§ããã®ãã£ã¼ã«ãã«Juliaã®Node
ãªãã¸ã§ã¯ãã«å¯¾ãããã¤ã³ã¿ã¼ãä¿æãã¦ããããããã¼ãã«å¯¾ããJuliaå´ã®Node
ãªãã¸ã§ã¯ããä½ãéã«ã¯ãããããã°ãããããªãã¸ã§ã¯ããåãåºãã¦ãã¾ããã¤ã¾ããæ¢ã«Juliaå´ã§ãããã¼ãã«å¯¾ãããªãã¸ã§ã¯ããä½æãã¦ããã°ããã使ããç¡ããã°æ°ããä½ã£ã¦ãã¤ã³ã¿ã¼ããã¼ãã®ä¿æãã¦ããã¨ããæä½ãNode
ã®ã³ã³ã¹ãã©ã¯ã¿ã¼å
ã§è¡ã£ã¦ãã¾ãããããããã¨ã§ãããlibxml2ã®ãã¼ãã«å¯¾ããJuliaå´ã®Node
ãªãã¸ã§ã¯ãã¯å¸¸ã«é«ã
1åã«ãªããäºéã«é åã解æ¾ãã¦ãã¾ããã¨ãé²ããã¨ãã§ãã¾ãã
æçµçã«ãNode
ã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã¯ä»¥ä¸ã®æ§ãªæãã«ãªãã¾ã(å®éã®ã³ã¼ããå°ãç°¡ç¥åãã¦ãã¾ã)ã
Cã®ãã¤ã³ã¿ã¼ããJuliaã®ãªãã¸ã§ã¯ããåãåºãã®ãunsafe_pointer_to_objref
ã§ãJuliaã®ãªãã¸ã§ã¯ãã®ãã¤ã³ã¿ã¼ãåå¾ããã®ãpointer_from_objref
ã§ãã
function Node(ptr::Ptr{_Node}) # return a preallocated proxy object if any str = unsafe_load(ptr) if str._private != C_NULL # found a valid proxy return unsafe_pointer_to_objref(str._private)::Node end # find the owner of this node owner_ptr = ptr while unsafe_load(owner_ptr).parent != C_NULL owner_ptr = unsafe_load(owner_ptr).parent end if ptr == owner_ptr # manage itself node = new(ptr) node.owner = node else # delegate management to its owner owner = Node(owner_ptr) node = new(ptr, owner) end # register finalizer and store the pointer to a node object finalizer(node, finalize_node) unsafe_store!(convert(Ptr{UInt}, ptr), convert(UInt, pointer_from_objref(node))) return node end
finalize_node
ã¯èªèº«ããªã¼ãã¼ã§ããå ´åã¯ããã以ä¸ã®ç®¡çãããã¼ãã«ã¤ãªãã£ã¦ããJuliaã®ãªãã¸ã§ã¯ããåãé¢ããlibxml2ã®é¢æ°ãå¼ãã§ãã¼ãã解æ¾ãã¦ãã¾ãããããªã¼ãã¼ã§ãªããã°ã_private
ã«NULL
ãã¤ã³ã¿ã¼ã代å
¥ãã¦ãJuliaã®ãªãã¸ã§ã¯ããGCããã¦ããåå¨ããªããã¨ã示ãã¦ãã¾ãã
# Finalize a Node object. function finalize_node(node) node_ptr = node.ptr if node === node.owner # detach pointers to C structs of descendant nodes traverse_tree(node_ptr) do ptr str = unsafe_load(ptr) if has_proxy(str) # detach! unsafe_extract_proxy(str).ptr = C_NULL end end # free the descendants if unsafe_load(node_ptr).typ == DOCUMENT_NODE ccall((:xmlFreeDoc, libxml2), Void, (Ptr{Void},), node_ptr) else ccall((:xmlFreeNode, libxml2), Void, (Ptr{Void},), node_ptr) end elseif node_ptr != C_NULL # indicate the proxy does not exit anymore store_proxy_pointer!(node, C_NULL) end return nothing end
以ä¸ã§ãEzXML.jlã®å¤§ã¾ããªå é¨å®è£ ããåããã«ãªããã¨æãã¾ããå®éã«ã¯ããµãããªã¼ãå¥ã®ããªã¼ã¸ã¨åããéã«ã¯ãªã¼ãã¼ãã¢ãããã¼ãããå¿ è¦ããããªã©ç´°ããå¦çãå¤å°ããã®ã§ããããã®ã¸ãã¾ã§èå³ããæ¹ã¯å ã®ã³ã¼ããèªãã§ã¿ã¦ä¸ãããã¾ã æ°ããããã±ã¼ã¸ã§ãã®ã§ã使ã£ã¦ã¿ã¦ãã°ãææ¡ãªã©ãããå ´åã¯å ±åãã¦ä¸ããã
Rã¦ã¼ã¶ã¼ã®ããã®Julia100å100ç
R Adevnt Calendar 8æ¥ç®ã®è¨äºã§ããå¤§å¹ ã«é ãã¦å¤§å¤ç³ã訳ãªãã§ãã
ãã®è¨äºã§ã¯Rè¨èªã¦ã¼ã¶ã¼ã®ããã«100å100çå½¢å¼ã§Juliaãç´¹ä»ãã¦ãããã¨æãã¾ãã
Juliaè¨èª
Juliaã£ã¦ã©ãããè¨èªãªã®?
Juliaã¯é«ã¬ãã«ã§ãã¤ããã©ã¼ãã³ã¹ãªæè¡è¨ç®ã®ããã®åçè¨èªã ããæ¸ããããã¨å®è¡é度ã®ä¸¡ç«ãã¦ãªã®è¨èªã ãã
誰ãä½ã£ã¦ãã®?
主ã«ãã¹ãã³ã®MITã®äººéãä¸å¿ã«ä½ã£ã¦ããè¨èªã ããç¹ã«Jeff Bezonson, Stefan Karpinski, Viral Shah, Alan Edelmanã®4人ãåæã®éè¦äººç©ã ãã
èªç±ã«ä½¿ããã®?
Juliaã®å¦çç³»ã¯MITã©ã¤ã»ã³ã¹ã§é å¸ããã¦ãããããåç¨ã§ããªãã§ãããªãèªç±ã«ä½¿ãããã
ã©ããããéãã®?
ãããéããï¼å¤§ä½Cè¨èªã®2å以å ãããã®åã¾ãé度ã ãã
Rã¨æ¯è¼ãã¦ã©ããªã®?
æ°åããæ°ç¾åãããé«éããªãç¹ã«ã«ã¼ããé¢æ°å¼ã³åºããããããããå ´åã«ã¯é¡èãªå·®ãåºããã
å ¬å¼ãµã¤ãã«ãããã³ããã¼ã¯ã®æç²ãè¼ãã¦ããã (Cè¨èª=1.0)ã
å®ä¾ã¯ãã?
forã«ã¼ãããããããªå ´åã¯ããªãã¯ã£ããå·®ãåºããã
ä¾ãã°1ããNã¾ã§ã®ã³ã©ããäºæ³ãå®è¨¼ããé¢æ°collatz
ãJuliaã¨Rã§æ¸ãã¦ã¿ããã
function collatz(N) for n in 1:N while n != 1 if iseven(n) n = div(n, 2) else n = 3n + 1 end end end end
collatz <- function (N) { for (n in 1:N) { while (n != 1) { if (n %% 2 == 0) { n = n %/% 2 } else { n = 3 * n + 1 } } } }
N=100,000ã§è©¦ãã¦ã¿ãã¨Juliaã¯Rã®300åãããé«éã ã£ããã
julia> @time collatz(100000)
0.026903 seconds (4 allocations: 160 bytes)
R> system.time(collatz(100000))
user system elapsed
8.429 0.028 8.479
ã©ããã¦éãã®?
LLVMã¨ããã³ã³ãã¤ã©ã®åºç¤ã©ã¤ãã©ãªãã¤ãã£ã¦å®è¡æã«ã³ã³ãã¤ã«ãè¡ã£ã¦ããããã ãããã¨Juliaè¨èªèªä½ãæåããããã©ã¼ãã³ã¹ãèæ ®ãã¦è¨è¨ããã¦ãããã詳ããã¯Bezansonãè«æãåç §ãã¦ãã
ã©ããæè¡è¨ç®ã«åãã¦ãã®?
å®è¡é度ãéãããã¤åçããã°ã©ãã³ã°è¨èªã§æ°è»½ã«å®è¡ã§ããããçãã¹ã¯ãªããã§è¨ç®ããã®ã«åãã¦ããããã¨ãæ°å¤è¨ç®ã§ããç¨ãããããã¼ã¿åãé¢æ°ãæåããç¨æããã¦ãããããã¤ã³ã¹ãã¼ã«ãã¦ãã使ãå§ããããããä¾ãã°ãç·å½¢ä»£æ°ã®ããã«BLASãLAPACKã®é¢æ°ãæ¨æºã§å ¥ã£ã¦ããã
Juliaã®ã¬ãã¸ããªã¯ã©ã?
GitHubã®ã¬ãã¸ããªãããã«ããã: https://github.com/JuliaLang/julia
ã¤ã³ã¹ãã¼ã«ç¨ã®ãã¤ããªã¯?
å ¬å¼ãµã¤ãã®ãã¦ã³ãã¼ããã¼ã¸ã«Windows, Mac, Linuxç¨ã®ãç¨æãã¦ããã: http://julialang.org/downloads/
Juliaã®ããã¥ã¢ã«ããã¥ã¼ããªã¢ã«ã¯?
Juliaã¯ããã¥ã¡ã³ããããæ¸ããã¦ãããããå ¬å¼ããã¥ã¢ã«ãèªãã®ããªã¹ã¹ã¡ã ãã æ¥æ¬èªã ã¨ç§ãæ¸ããJuliaã®ãã¥ã¼ããªã¢ã«ãM.Hiroiããã®ãµã¤ãããããã
ç°å¢
ã©ããã£ã¦å®è¡ããã®?
次ã®ã¹ã¯ãªãããhello.jlã¨ãã¦ãã¡ã¤ã«ã«ä¿åããã¨ãããã
println("hello, world")
Juliaãã¤ã³ã¹ãã¼ã«ãã¦ãããªãjulia
ã³ãã³ããããã¯ããªã®ã§ãããã«ãã£ãã®ãã¡ã¤ã«ã渡ãã°Rscript
ã¿ããã«å®è¡ã§ãããã
$ julia hello.jl
hello, world
対話ã»ãã·ã§ã³(REPL)ã使ãã«ã¯ãå¼æ°ç¡ãã§julia
ãå®è¡ããããããããã¨ãä¸ã®ããã«Rã¿ãããªJuliaã®REPLãç«ã¡ä¸ããã!
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.5.0 (2016-09-19 18:14 UTC)
_/ |\__'_|_|_|\__'_| |
|__/ | x86_64-apple-darwin14.5.0
julia> 1 + 2
3
julia>
ãã«ãã®èª¿ã¹æ¹ã¯?
Rã¿ããã«REPLã«?
ã¨æã£ã¦ã¿ãããããããã¨ããã³ãããhelp?>
ã«å¤ãããããç¥ãããé¢æ°ã®ååãããã«æã£ã¦Enterãæ¼ãã!
help?> sum
search: sum sum! sumabs summary sumabs2 sumabs! sum_kbn sumabs2! cumsum cumsum! consume cumsum_kbn isnumber isalnum SlotNumber
sum(itr)
Returns the sum of all elements in a collection.
sum(A, dims)
Sum elements of an array over the given dimensions.
sum(f, itr)
Sum the results of calling function f on each element of itr.
ãã¼ã¯ã¼ãæ¤ç´¢ãããå ´åã¯ãhelp?>
ããã³ããã§ãã¼ã¯ã¼ãã"..."
ã§å²ãã§å
¥åããã!
help?> "variance"
Base.var
Base.cov
Base.varm
RStudioã¿ãããªã®ã¯ããã®?
RStudioã»ã©ãããªããããããªããã©ãJunoã¨ããAtomã¨ãã£ã¿ã¼ããã¼ã¹ã«ããéçºç°å¢ã人æ°ã ããä»ã«ã¯Eclipseåãã®ãã©ã°ã¤ã³(https://github.com/JuliaComputing/JuliaDT)ãVS Codeåãã®ãã©ã°ã¤ã³(https://github.com/JuliaEditorSupport/julia-vscode)ããããã
EmacsãVimã®äººã¯?
Emacsç¨ãã©ã°ã¤ã³(https://github.com/JuliaEditorSupport/julia-emacs)ã¨Vimç¨ãã©ã°ã¤ã³(https://github.com/JuliaEditorSupport/julia-vim)ããã¡ãããããã
ãã¼ãããã¯ã¯ããã®?
IJulia.jlã使ã£ã¦ãJupyter Notebookãå©ç¨ã§ãããããã試ãã¦ã¿ãã®ã«JuliaBoxã使ãã¨ããããããªãããª?
CRANã¿ãããªããã±ã¼ã¸ã®ã¬ãã¸ããªã¯ããã®?
ãããï¼ããã±ã¼ã¸ã®ä¸è¦§ãhttp://pkg.julialang.org/ããé²è¦§ã§ãããã
ããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«ã¯ã©ãããã®?
Rã®install.packages
ã«å½ããã®ãPkg.add
ã ãããã¡ããä¾å解決ããã¦ããããã
GitHubã«ãããªãããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«ã¯?
Pkg.clone
ã«ã¬ãã¸ããªã®URLãå
¥ããã¨éè¯ããã±ã¼ã¸ã§ãã¤ã³ã¹ãã¼ã«ã§ããããããã§ãä¾å解決ããã¦ããããã
Rãå¼ã³åºããããã ãã©?
RCall.jlã使ãã! RCall.jlã¯Juliaã®ã³ã¼ãã«Rãåãè¾¼ãã ãRã®REPLã使ã£ããã§ãããã
Pythonãå¼ã³åºããããã ãã©?
PyCall.jlã使ãã! ã¤ãã§ã«ãC++ãå¼ã³åºããCxx.jlã¨ãMATLABãå¼ã³åºããMATLAB.jlãªãã¦ã®ããããã
CãFortranã®ã³ã¼ãã®å¼ã³åºãã¯?
Juliaã¯CãFortranã®ã³ã¼ããå¼ã³åºãä»çµã¿ãæ¨æºã§ç¨æãã¦ãããã 詳ããã¯å¾ã§èª¬æãããã
Rã®ãµã³ãã«ãã¼ã¿ãèªã¿ãããã ãã©?
RDatasets.jlã«Rã§ãã使ããããã¼ã¿ã»ãããããã±ã¼ã¸ããã¦ããã!
ã¯ã¼ã¯ã¹ãã¼ã¹ãä¿åããã«ã¯ã©ãããã®?
JLD.jlã®@save
ãã¯ãã使ããã
julia> using JLD
julia> n = 5
5
julia> x = randn(n, n)
5Ã5 Array{Float64,2}:
-2.18618 0.0831716 2.19386 0.806268 1.11444
-0.99689 -0.187922 -0.138358 -0.141601 0.19058
0.0971969 0.149858 -1.19826 1.15507 1.0969
-0.140956 -0.727159 0.780267 1.17143 0.979918
-0.161376 -0.162103 -0.175158 -0.402853 0.916248
julia> @save "workspace.jld"
ã¯ã¼ã¯ã¹ãã¼ã¹ã復å ããã«ã¯?
åããJLD.jlã®@load
ãã¯ãã使ããã
julia> using JLD
julia> @load "workspace.jld"
3-element Array{Symbol,1}:
:ans
:n
:x
julia> n
5
julia> x
5Ã5 Array{Float64,2}:
-2.18618 0.0831716 2.19386 0.806268 1.11444
-0.99689 -0.187922 -0.138358 -0.141601 0.19058
0.0971969 0.149858 -1.19826 1.15507 1.0969
-0.140956 -0.727159 0.780267 1.17143 0.979918
-0.161376 -0.162103 -0.175158 -0.402853 0.916248
ã³ãã¥ããã£
Juliaã®ã³ãã¥ããã£ã¯ã©ããªæããªã®?
Juliaã®ã³ãã¥ããã£ã¯å°ããããã©ãã¨ã¦ããªã¼ãã³ãªã³ãã¥ããã£ãå½¢æãã¦ãããã ç¹ã«åå¿è ã«åªãã(Juliaã¦ã¼ã¶ã¯ã ãããåå¿è )ã®ã§ãæ°è»½ã«ã³ãã¥ãã±ã¼ã·ã§ã³ãåãããã
質åã¯ã©ãã§ããã°ããã®?
è±èªãªãDiscourseãStack Overflowã ããStack Overflowã§è³ªåããæã¯"julia-lang"ã®ã¿ã°ãã¤ããããã«ãããã Discourseã®æ¹ãJuliaããã«ã¼ãè¦ã¦ãçãé«ãæ°ããããããJuliaã«éå®ãã質åãªãDiscourseã®æ¹ããªã¹ã¹ã¡ããã
æ¥æ¬èªè©±è ã®ã³ãã¥ããã£ã¯?
æ¥æ¬ã§ã¯Julia Tokyoãç¥ãéãå¯ä¸ã®ã³ãã¥ããã£ããª? Facebookã»Slackã»GitHubã®ã¢ã«ã¦ã³ããããããä¸å®æã§ãã¼ãã¢ãããéå¬ãã¦ããã
ä»ã«ã¯ã©ããªã³ãã¥ããã£ãããã®?
Juliaã®ã³ãã¥ããã£ã¯åãã¡ã¤ã³ã«åããã¦ãããã¤ãªãã£ã¦ãããã ã³ãã¥ããã£ã®ãªã¹ãã¯ãã(http://julialang.org/community/)ã«ãããã ããã¤ãæããã¨ä»¥ä¸ã®ãã¡ã¤ã³ã®ã³ãã¥ããã£ã¯çµæ§æ´»çºããªã
ãã¡ã¤ã³ | GitHub |
---|---|
çµ±è¨ | JuliaStats |
æé©å | JuliaOpt |
ãã¼ã¿ãã¼ã¹ | JuliaDB |
GPU | JuliaGPU |
çç©å¦ | BioJulia |
å¾®å | JuliaDiff |
ã°ã©ãã£ã¯ã¹ | JuliaGraphics |
è¨éçµæ¸å¦ | QuantEcon |
ãããã§æ´»çºã«éçºããã¦ããããã±ã¼ã¸ã¯æ¦ãå®å¿ãã¦ä½¿ããããªã
ã©ããªäººéãJuliaã使ã£ã¦ãã®?
Juliaã®ä½¿ãæ¹ã¯è²ã ããããã©ããã£ã±ãæ°å¤è¨ç®ç®çã«ä½¿ã£ã¦ããã¦ã¼ã¶ãå¤ãããªã Julia Computing, Inc.ã®äºä¾ãè¦ãã¨ãéèã»çµæ¸åéãå§ãã¨ãããã¼ã¿åæçéã§ä½¿ããå§ãã¦ããå°è±¡ããããªã
HPCã§ã®äºä¾ãç§å¦è«æã§Juliaã使ã£ãè¨ç®ã®å®è£ ãæè¿ããè¦ãããã«ãªã£ã¦ãããã
ä»ã«ã¯ãStanfordãMITã§ã¯æè¡è¨ç®ã®ææ¥ã«ãJuliaã使ããã¦ãããã
ãã¼ã¯ã¨ãã®æ åã¯?
YouTubeã®ãã£ã³ãã«ã«ã«ã³ãã¡ã¬ã³ã¹ã®åç»ãªã©ãããããããã!
https://www.youtube.com/user/JuliaLanguage
Juliaçã®Hadley Wickhamã¯?
Juliaçã«ã¯(è¯ããæªãã)ãããããç®ç«ã£ã¦ãã人ã¯ããªãããªã Juliaã®éè¦ãªããã±ã¼ã¸ã¯åorganizationãã³ãã¥ããã£ã¨ãã¦éçºã»ç®¡çãã¦ããã
ææ³
Rã®æ§æã¨ã®å¯¾å¿é¢ä¿ãæãã¦?
ããã!
ä»£å ¥
# R x <- 100
# Julia x = 100
åå²
# R if (x == 0) { print("zero") } else if (x < 0) { print("negative") } else { print("positive") }
# Julia if x == 0 println("zero") elseif x < 0 println("negative") else println("positive") end
foræ
# R for (i in 1:10) { print(i) if (i > 5) { break } }
# Julia for i in 1:10 println(i) if i > 5 break end end
é¢æ°
# R add <- function (x, y) x + y add <- function (x, y) { return (x + y) }
# Julia add(x, y) = x + y function add(x, y) return x + y end
ã©ã¤ãã©ãªèªã¿è¾¼ã¿
# R library(ggplot2)
# Julia using DataFrames
ãã¾ã«Juliaã®ã³ã¼ãã«ãã@
ã¯ä½?
@
ããå§ã¾ãã³ã¼ãã¯Juliaã®ãã¯ãå¼åºãã ãã
ãã¯ãã¯Juliaã®ã³ã¼ããå¥ã®ã³ã¼ãã«æ¸ãæãããããã¡ã¿ããã°ã©ãã³ã°ã®ä¸ç¨®ã ãã
ä¾ãã°æ¬¡ã®ã³ã¼ãã«åºã¦ãã@inbounds
ã¯é
åã¢ã¯ã»ã¹ã®å¢çãã§ãã¯ãç¡ããã¦å°ãé«éåãããã¨ãã§ãããã@show
ã¯ãã®æã®å¤ãããæãã«è¡¨ç¤ºãã¦ããããã
x = randn(10) s = 0 @inbounds for i in 1:endof(x) s += x[i] end @show s
ãã¾ã«Juliaã®ã³ã¼ãã«ããr"..."
ã¯ä½?
ãããJuliaã®ãã¯ãã ã!
æ¨æºã©ã¤ãã©ãªã§ã¯r"..."
ã¨æ¸ããã¨ã§æååã§ãªãæ£è¦è¡¨ç¾ãä½ããã¨ãã§ãããã
julia> r"foo"
r"foo"
julia> typeof(r"foo")
Regex
julia> b"foo"
3-element Array{UInt8,1}:
0x66
0x6f
0x6f
julia> typeof(b"foo")
Array{UInt8,1}
ãã®ä»çµã¯ã¦ã¼ã¶ãããæ¡å¼µå¯è½ãªã®ã§ãä¾ãã°Bio.jlã§ã¯DNAé åãä½ã£ããããã®ã«ä½¿ã£ã¦ããã
julia> using Bio.Seq
julia> dna"ACGTAG"
6nt DNA Sequence:
ACGTAG
ãã¼ã¿
主ãªæ°å¤åã¯?
æ´æ°ã¯Int
åãå精度浮åå°æ°ç¹æ°ã¯Float64
åãããã©ã«ãã§ãã使ããããã
Int
ã¯32-bitç°å¢ãªã32-bitã64-bitç°å¢ãªã64-bitã§è¡¨ç¾ããããã
Juliaã¯æ°å¤ã®åãè±å¯ã§ã8-bitãã128-bitã¾ã§ç¬¦å·ã®æãç¡ãã®çµã¿åããããã¹ã¦ç¨æããã¦ããããè¤ç´ æ°(Complex{T}
)ãæçæ°(Rational{T}
)ããããã
TRUE
ãFALSE
�
Juliaã§ã¯å
¨é¨å°æåã®true
ã¨false
ã ãã
æ¬ æå¤ã¯æ±ããã®?
Juliaã§ã¯Nullable{T}
ã¨ããåããã£ã¦ãåT
ã®æ¬ æå¤ã表ããã
nothing
ã¨ããå¤ããããã©ããã©ã¼ãã³ã¹ä¸ã®çç±ã§ãã¾ããå§ãããªããã
JuliaãRã¿ããã«å¸¸ã«é åã¨ãã¦æ°å¤ãæ±ãã®?
Juliaã§ã¯åä¸ã®å¤ã¨é
åã¯åºå¥ãããããä¾ãã°ã3
ã¨æ¸ãããæ´æ°ã®3ãæå³ãããã[3]
ã¨æ¸ããã1ã¤ã®æ´æ°3ãããªããã¯ãã«ã ãã
3
ã¨æ¸ãããæµ®åå°æ°ç¹æ°ãããªãã¦æ´æ°ãªã®?
ããã ããRã¨éã£ã¦Juliaã§ã¯æ´æ°ã¨ãã¦æ±ããããã
ãããæµ®åå°æ°ç¹æ°ã®3ãä½ãã«ã¯?
3.0
ã¨æ¸ãã°æµ®åå°æ°ç¹æ°ã¨ãã¦æ±ããããã
ã©ããã£ã¦ç¢ºèªããã®?
Rã¿ããã«typeof
ã¨ããé¢æ°ã使ããï¼
julia> typeof(3)
Int64
julia> typeof(3.0)
Float64
R> typeof(3)
[1] "double"
R> typeof(3.0)
[1] "double"
æ¼ç®åã¯ã©ããªæã?
ã»ã¨ãã©Rã¨åãã ãã
julia> 3 + 4
7
julia> 3 - 4
-1
julia> 3 * 4
12
julia> 3 / 4
0.75
R> 3 + 4
[1] 7
R> 3 - 4
[1] -1
R> 3 * 4
[1] 12
R> 3 / 4
[1] 0.75
Boolæ¼ç®(&&
, ||
, !
)ãåãã ããæä»çè«çåã¯v0.5ã§ã¯$
ã ãã©æ¬¡æãã¼ã¸ã§ã³ã§xor
ã¨ããååã«å¤æ´ãããäºå®ã ãã
list
ã¯ããã®?
Juliaã§ä¸çªè¿ãã®ã¯Dict
ããª?
Pythonã®dict
ã¨åãããã·ã¥ã使ã£ãé£æ³é
åã ãã
julia> d = Dict("one" => 1, "two" => 2, "three" => 3)
Dict{String,Int64} with 3 entries:
"two" => 2
"one" => 1
"three" => 3
julia> d["two"]
2
data.frame
�
æ®å¿µãªããJuliaã®æ¨æºã©ã¤ãã©ãªã«ã¯ãªãããã§ãDataFrames.jlã¨ããããã±ã¼ã¸ãJuliaã®ãã¼ã¿ãã¬ã¼ã ã®æ¨æºçãªå®è£ ã ãã ãã®è¾ºã¯å¾ã§è©³ããè¿°ã¹ããã
å¤æ¬¡å è¡åãæ±ããã®?
ãã¡ããæ±ããã! Rã®matrix
ã¿ãããªãã¨ã次ã®ããã«ã§ãããã
julia> [1 2 3
4 5 6]
2Ã3 Array{Int64,2}:
1 2 3
4 5 6
julia> [1 2 3; 4 5 6] # ã»ãã³ãã³ã¯æ¹è¡ã¨åãæ±ã
2Ã3 Array{Int64,2}:
1 2 3
4 5 6
R> matrix(c(1, 2, 3, 4, 5, 6), nrow=2)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
空ã®é åã®ä½æã¯?
ãã¯ãã«(1次å
é
å)ãªã[]
ã ãã
julia> []
0-element Array{Any,1}
ãã è¦ç´ ã®åãåãã£ã¦ããå ´åã¯ãããæãã¦ãããã¨Juliaã¯é«éã«åããã
julia> Int[]
0-element Array{Int64,1}
julia> Float64[]
0-element Array{Float64,1}
matrix(0, 3, 4)
ã¿ããã«0ã§åæåãããé
åã®ä½æã¯?
zeros
é¢æ°ã使ãã!
julia> zeros(3, 4) # 3è¡4åã®è¡å
3Ã4 Array{Float64,2}:
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
julia> zeros(Int, 3, 4)
3Ã4 Array{Int64,2}:
0 0 0 0
0 0 0 0
0 0 0 0
1ã§åæåãããé
åãªãones
ã ãã
é åè¦ç´ ã¸ã®ã¢ã¯ã»ã¹ã¯ã©ãããã®?
åºæ¬çã«Rã¨åãã ããJuliaã®é
åã1å§ã¾ãã§column majorã ãã
ãã ãRã ã¨x[i,]
ã¨ãx[,j]
ã¨æ¸ãã¨ãããJuliaã§ã¯x[i,:]
ã¨ãx[:,j]
ã¨ãæ¸ããã
julia> x = [1 2 3; 4 5 6]
2Ã3 Array{Int64,2}:
1 2 3
4 5 6
julia> x[1,2]
2
julia> x[2,2:3]
2-element Array{Int64,1}:
5
6
julia> x[:,2]
2-element Array{Int64,1}:
2
5
2:3
ã¿ãããªã®ã¯ãã¯ãã«?
Juliaã§ã¯m:n
ã¯m
ããn
ã¾ã§ã®ç¯å²(Range
)ã表ããã
julia> 2:3
2:3
julia> typeof(2:3)
UnitRange{Int64}
ãã¯ãã«ã¨éã£ã¦ã¡ã¢ãªã¼ãæ¶è²»ããªãã®ã§ãé·ãç¯å²ã§ãä¸ç¬ã§ä½ãããã
character
ã¿ãããªæåååã¯?
Juliaã§ã¯String
åããããã
æååã¯Unicodeã使ãã?
使ããã! Juliaã§ã¯UTF-8ã§ã¨ã³ã³ã¼ãã£ã³ã°ãã¦ãããã
æååæä½é¢æ°ãæãã¦?
stringrããã±ã¼ã¸ã®é¢æ°ã¨å¤§éæã«å¯¾å¿ãä»ãããã
R (stringr) | Julia |
---|---|
str_c |
string |
str_detect |
ismatch |
str_extract |
match |
str_dup |
^ |
str_split |
split |
str_replace |
replace |
str_trim |
strip , lstrip , rstrip |
str_pad |
lpad , rpad |
str_to_lower |
lowercase |
str_to_upper |
uppercase |
as.double
ã¨ãas.integer
ã¿ãããªåå¤æã¯ã©ãããã®?
Juliaã®åå¤æã¯åºæ¬çã«convert
é¢æ°ã使ããã
julia> convert(Float64, 42)
42.0
julia> convert(Int64, 42.0)
42
ãã ãæååããã¼ã¹ãã¦æ°å¤ã«ããã«ã¯parse
ã使ããã
julia> parse(Int, "42")
42
julia> parse(Float64, "42")
42.0
è¡åãååã¯ã©ãã¤ããã®?
Juliaã®æ¨æºã§ã¯è¡åãååããµãã¼ããã¦ããªããã ã§ãNamedArrays.jlã¨ããé åã«ååãä»ããããããã«ããããã±ã¼ã¸ã¯ãããã
æ¬ æå¤ã®ããé åã¯ã©ãæ±ãã°ããã®?
æ¬ æå¤ã®æ±ãã¯ã¾ã Juliaçéã§ãå®å
¨ãªåæã«éãã¦ãªã課é¡ãªã®ã§ãã¡ãã£ã¨ããããããªã
åã«ãè¿°ã¹ãããã«Nullable{T}
ã¨ãããã¼ã¿åããããã©ãããã«é¢ããæä½ãå°æ¥å¤ããå¯è½æ§ããããã
ã§ããåºæ¬çã«ã¯NullableArrays.jlã使ãã!
å å(factor)ãæ±ãã«ã¯ã©ãããã®?
ãããã¾ã Juliaã§ã¯ã¡ãã£ã¨æ±ããé£ãããã©ãCategoricalArrays.jlã¨ããããã±ã¼ã¸ããããã NullableArrays.jlãCategoricalArrays.jlã¯è¿ããã¡ã«DataFrames.jlã«æ¡ç¨ãããäºå®ã®ããã ãã
é¢æ°
é¢æ°ã¯ã©ããã£ã¦å®ç¾©ããã®?
function
ãã¼ã¯ã¼ãã使ã£ãã¹ã¿ã¤ã«ã¨ç縮è¨æ³ããããã
# é常ã®è¨æ³ function add(x, y) return x + y end # ç縮è¨æ³ add(x, y) = x + y
ããã©ã«ãå¼æ°ããã¼ã¯ã¼ãå¼æ°ã¯?
ããã! ã§ãJuliaã§ã¯ããã©ã«ãå¼æ°ã¨ãã¼ã¯ã¼ãå¼æ°ã§è¨æ³ãåããã¦ãããã
# ããã©ã«ãå¼æ° function default(x, y=1, z=10) return x + y + z end # ãã¼ã¯ã¼ãå¼æ° (ã»ãã³ãã³ã«æ³¨ç®) function keyword(x; y=1, z=10) return x + y + z end
試ãã«å®è¡ãã¦ã¿ãã
julia> default(1)
12
julia> default(1, 0)
11
julia> default(1, 0, 5)
6
julia> keyword(1)
12
julia> keyword(1, z=5)
7
julia> keyword(1, z=5, y=0)
6
ã¯ãã¼ã¸ã£ãä½ãã?
ãã¡ãã!
function counter() i = 0 function x(n=1) i += n return i end end
julia> c = counter()
(::x) (generic function with 2 methods)
julia> c()
1
julia> c()
2
julia> c()
3
julia> c(3)
6
julia> c()
7
+
ã¨ãã¯é¢æ°ãããªãã®?
é¢æ°ã ã! åç½®è¨æ³ã§æ¸ããã¨ãã§ããã!
julia> +(1, 2)
3
R> `+`(1, 2)
[1] 3
ãããRã¿ããã«if
ã¨ãfor
ãé¢æ°ãªã®?
Juliaã§ã¯if
ã»for
ã»while
ã¯é¢æ°ãããªããã
é 延è©ä¾¡ã¯ããã®?
Juliaã«ã¯é 延è©ä¾¡ã¯ãªãã¦ãã¹ã¦æ£æ ¼è©ä¾¡ã ããã¤ã¾ãé¢æ°ã«æ¸¡ãããå¼æ°ã¯å¼ã³åºãåã«ããªããè©ä¾¡ããããã
apply
ã¿ãããªé¢æ°ãé
åã®è¦ç´ ã«é©ç¨ããé¢æ°ã¯?
map
ã使ãã! ãã ãé¢æ°ãæåã®å¼æ°ã ãã
julia> map(log, [1,2,3])
3-element Array{Float64,1}:
0.0
0.693147
1.09861
julia> map(exp, [1,2,3])
3-element Array{Float64,1}:
2.71828
7.38906
20.0855
R> sapply(c(1,2,3), log)
[1] 0.0000000 0.6931472 1.0986123
R> sapply(c(1,2,3), exp)
[1] 2.718282 7.389056 20.085537
ãã æè¿ã¯ããã¼ããã£ã¹ãã使ãæ¹æ³ãä¸è¬çãªã®ã§ããã¡ãã使ã£ãã»ããè¯ããã
julia> log.([1,2,3])
3-element Array{Float64,1}:
0.0
0.693147
1.09861
julia> exp.([1,2,3])
3-element Array{Float64,1}:
2.71828
7.38906
20.0855
ãã®é¢æ°åã®å¾ã®ãããã¯ä½?
ããã¼ããã£ã¹ãé¢æ°å¼ã³åºãã®æ§æç³è¡£ã ããä¾ãã°f.(x)
ã¯broadcast(x, f)
ã«å¤æããããã
broadcast
ã¯map
ã®åãçã ãã
ãã¯ãã«å士ã®è¶³ãå¼ãã¨ãã¯?
+
ã-
ããã®ã¾ã¾ä½¿ããããã§ã.
ãä»ãã.+
ã.-
ãªãå·¦å³ã§é
åã®ãµã¤ãºãéã£ãå ´åã§ãRã®ããã«åãã¦ããããã
julia> [1,2,3] + [4,5,6]
3-element Array{Int64,1}:
5
7
9
julia> [1,2,3] .+ [4]
3-element Array{Int64,1}:
5
6
7
julia> [1,2,3] .+ 4
3-element Array{Int64,1}:
5
6
7
R> c(1, 2, 3) + c(4, 5, 6)
[1] 5 7 9
R> c(1, 2, 3) + c(4)
[1] 5 6 7
R> c(1, 2, 3) + 4
[1] 5 6 7
æ¸ããé¢æ°ãæã£ãããé ããã ãã©?
ä¸çªããããåå ãé¢æ°å
ã§ä½¿ããã¦ããå¤æ°ã®åãä¸å®å®ã«ãªã£ã¦ããããã ãã
ä¾ãã°æ¬¡ã®ã³ã¼ãã¯xs
ã®è¦ç´ ãInt
ã®ã¨ãã«ã¯é«éã«åããã©ãæµ®åå°æ°ç¹æ°ã®é
åã¨ãã渡ãã¨ãããé
ããã
function sumup(xs) s = 0 for x in xs s += x end return s end
julia> xs = rand(1:10, 10000);
julia> sumup(xs); # ã¦ã©ã¼ã ã¢ãã
julia> @time sumup(xs)
0.000011 seconds (5 allocations: 176 bytes)
54649
julia> xs = rand(10000);
julia> sumup(xs); # ã¦ã©ã¼ã ã¢ãã
julia> @time sumup(xs)
0.000337 seconds (30.00 k allocations: 468.906 KB)
5015.030044010504
åå ã¯å¤æ°s
ã¯æ´æ°åã§åæåããã¦ãã®ã«ãã«ã¼ãã®ä¸ã§æµ®åå°æ°ç¹æ°ã¨è¶³ããã¨ã§s
ã®åãæ´æ°ãæµ®åå°æ°ç¹æ°ãJuliaã«ã¯åãããªããªã£ã¦ãã¾ãããã ãããããJuliaçéã§ã¯åã®ä¸å®å®ã(type instability)ã¨è¨ã£ã¦ããã
ãããåé¿ããã«ã¯eltype
ã¨zero
é¢æ°ã次ã®ããã«ä½¿ããã¨ã§ãs
ã®åãxs
ã«åããã¦è¨å®ã§ãããã
function sumup(xs) s = zero(eltype(xs)) for x in xs s += x end return s end
ãããããã¨ã§ãã£ãããä½ååãéããªã£ãã!
julia> sumup(xs);
julia> @time sumup(xs)
0.000015 seconds (5 allocations: 176 bytes)
5015.030044010504
é¢æ°ã®ãããã¡ã¤ã«ãåãã«ã¯ã©ãããã®?
@profile
ãã¯ãã¨ãProfile.print()
ããããã¦ä½¿ãã!
@profile <code>
ã§ã³ã¼ãå®è¡ã®ãããã¡ã¤ã«ãã¨ã£ã¦ãProfile.print()
ã§ãããã¡ã¤ã«çµæãããªã³ãã§ãããã
Cè¨èªã®é¢æ°ã®å¼ã³åºãæ¹æ³ãæãã¦!
Juliaã«ã¯ccall
ã¨ããCè¨èªãå¼ã³åºãå°ç¨ã®å½ä»¤ãããããä¾ãã°ãGNU libcã«å®è£
ããã¦ããexp
é¢æ°ãå¼ã³åºããªã次ã®ããã«æ¸ãããã
julia> ccall((:exp, "libc"), Float64, (Float64,), 1.0)
2.718281828459045
julia> ccall((:exp, "libc"), Float64, (Float64,), -1.0)
0.36787944117144233
ccall
ã¯æåã«å¼ã³åºãé¢æ°ãæå®ãã¦ããã®å¾ã«è¿ãå¤ã®åã¨å¼æ°ã®åãæå®ãã¦ãæå¾ã«å¼æ°ã渡ãããä¸ã®ä¾ã§è¨ãã°ã(:exp, "libc")
ãå¼ã³åºãé¢æ°ã§ãFloat64
ãè¿ãå¤ã®åã(Float64,)
ãå¼æ°ã®åã§ã1.0
ã-1.0
ãCã®é¢æ°ã«æ¸¡ãããå®éã®å¼æ°ã ãã
Juliaã¯æ°å¤ä»¥å¤ã«ãæååãããè¤éãªæ§é ä½ãCã®ã©ã¤ãã©ãªã¨ããåããããã¨ãã§ããããã«è¨è¨ããã¦ããã®ã§ããã詳ããã¯ããã¥ã¢ã«ã®ãããèªãã§ã¿ã¦ãã å ·ä½çãªä¾ã¨ãã¦ã¯Juliaã®æ¨æºã©ã¤ãã©ãªãLibz.jlãªãããåå¼·ã«ãªããã
åã·ã¹ãã ã¨ã¡ã½ãã
Juliaã®åã·ã¹ãã ã¯ã©ããªã£ã¦ãã®?
Juliaã®åã¯å¤§ããå ·ä½å(concrete type)ã¨æ½è±¡å(abstract type)ã«åãããã¦ãã¦ãã¡ã½ããã®é¸æã¯å¤éãã£ã¹ãããã¨ããã·ã¹ãã ãä¸å¿ã«æ§ç¯ããã¦ãããã
å ·ä½åã¨ãæ½è±¡åã£ã¦ä½?
å
·ä½åã¯ã¤ã³ã¹ã¿ã³ã¹å(ãªãã¸ã§ã¯ããä½ãã)ã§ããåã§ãæ½è±¡åã¯ããã§ãªãåã ãã
ä¾ãã°Float64
ã¯1.0
ãªã©å¤ãä½ãããã©Real
ã¨ããæ½è±¡åã¯å¤ãä½ããªããã
å¤ãä½ããªãæ½è±¡åã¯ä½ã®å½¹ã«ç«ã¤ã®?
è¤æ°ã®åãã¾ã¨ããã®ã«ä½¿ãããããKeitaNakamuraããã®ã³ã¼ãã使ããã¦ãããã¨ãä¾ãã°Real
ãé ç¹ã¨ããåã¯æ¬¡ã®ãããªæ§é ã«ãªã£ã¦ãããã
julia> print_tree(Real)
Real
ââââ AbstractFloat
| ââââ BigFloat
| ââââ Float16
| ââââ Float32
| ââââ Float64
ââââ Integer
| ââââ BigInt
| ââââ Bool
| ââââ Signed
| | ââââ Int128
| | ââââ Int16
| | ââââ Int32
| | ââââ Int64
| | ââââ Int8
| ââââ Unsigned
| ââââ UInt128
| ââââ UInt16
| ââââ UInt32
| ââââ UInt64
| ââââ UInt8
ââââ Irrational{sym}
ââââ Rational{T<:Integer}
function print_tree(typ) println(typ) print_tree(typ, []) end function print_tree(typ, level) stypes = subtypes(typ) for stype in stypes if stype !== stypes[end] println(join(level) * "ââââ $stype") push!(level, "| ") else println(join(level) * "ââââ $stype") push!(level, " ") end print_tree(stype, level) pop!(level) end end
ã¯ã©ã¹(å)ã®å®ç¾©ã¯ã©ãããã®?
Rã®S4ã«ä¼¼ãä»çµã¿ãJuliaã«ã¯ããããä¾ãã°ä»¥ä¸ã®2ã¤ã®å®ç¾©ã¯å¤§ä½å¯¾å¿ãã¦ãããã
# R setClass("Person", representation(name = "character", age = "numeric"))
# Julia type Person name::String age::Int end
Juliaã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãæºåãã¦ããããããªãã¸ã§ã¯ãã¯æ¬¡ã®ããã«ä½æã§ãããã
julia> hadley = Person("Hadley", 31)
Person("Hadley",31)
julia> hadley.name
"Hadley"
julia> hadley.age
31
ãã ããJuliaã®type
ã§å®£è¨ãããªãã¸ã§ã¯ãã¯å¤æ´å¯è½ãªã®ã§ãã®æ¯ãèãã¯Rã®åç
§ã¯ã©ã¹(RC)ã«è¿ãããªã
ç¶æ¿ã¯?
Juliaã¯setClass
ã®contains
å¼æ°ã«å½ãããããªç¶æ¿ã®ä»çµã¿ã¯ãªãããã¤ã¾ããè¤æ°ã®åéã§æ§é ãç¶æ¿ããæ¹æ³ã¯ãªããã
ãã®ä»£ãããå®ç¾©ããåãããæ½è±¡åã®ãµãã¿ã¤ãã«ããä»çµã¿ã¯ãã£ã¦ãJuliaã§ã¯ãã¡ãã使ããã
ä¾ãã°ãJuliaã§è¦ç´ ã®æ°ã3ã¤ã®ãã¯ãã«ã£ã½ãåãä½ãå ´åã«ã¯ã次ã®ããã«AbstractVector
ã®ãµãã¿ã¤ãã¨ãã¦å®£è¨ããã¨è¯ããã
type Triplet{T} <: AbstractVector{T} x::T y::T z::T end
ããã§ãT
ã¨æ¸ããã¦ããã®ã¯åãã©ã¡ã¼ã¿ã§ãT = Int64
ãT = Float64
ã代å
¥ããã¦å
·ä½çãªåã«ãªããã
ã¾ããåæ½è±¡åã«ã¯å®è£
ãã¦ãããã¨ãæå¾
ãããã¡ã½ãããããã®ã§ããããå®è£
ããã¹ãã ã (AbstractVector
ãªãBase.size
ã»Base.getindex
ãªã©ãå®è£
ãã¹ã)ã
ã¡ã½ããã®å®ç¾©ã¯?
é常ã®é¢æ°å®ç¾©ã«åæå®ãããã®ãRã®setMethod
ã«è¿ããã
# R setGeneric("greet", function(x) standardGeneric("greet")) setMethod("greet", signature("Person"), function(x) paste("Hello, ", x@name))
# Julia function greet(x::Person) return string("Hello, ", x.name) end
åæå®ã¯è¤æ°ã«å¼æ°ã«å¯¾ãã¦ãæå®ã§ããããæ½è±¡åãæå®ã§ãããããããJuliaã§ã¯å¤éãã£ã¹ãããã¨è¨ããã
åå¦ç
CSV/TSVãã¡ã¤ã«ã®èªã¿è¾¼ã¿ã¯?
3éãã®æ¹æ³ãããã!
- æ¨æºã©ã¤ãã©ãªã®
readcsv
- DataFrames.jlã®
readtable
- CSV.jlã®
CSV.read
ã©ããããã®?
æ¨æºã©ã¤ãã©ãªã®readcsv
ã¯æ¨æºãªã®ã§ãã使ãããã©ãè¡åã¨ãã¦ãã¼ã¿ãèªã¿è¾¼ããããå ´åã«ãã£ã¦ã¯ã¡ãã£ã¨ä½¿ãã¥ããããª?
DataFrames.jlã®readtable
ã¯ãã¼ã¿ãã¬ã¼ã ãèªã¿è¾¼ããã©ãå°æ¥çã«ä½¿ãããªããªãå¯è½æ§ãé«ããã
ãªã®ã§ãç¾å¨ã§ã¯CSV.jlã®CSV.read
ã使ãã®ããªã¹ã¹ã¡ã ãã
julia> using DataFrames
julia> using CSV
julia> head(CSV.read("iris.csv"))
6Ã5 DataFrames.DataFrame
â Row â SepalLength â SepalWidth â PetalLength â PetalWidth â Species â
âââââââ¼ââââââââââââââ¼âââââââââââââ¼ââââââââââââââ¼âââââââââââââ¼âââââââââââ¤
â 1 â 5.1 â 3.5 â 1.4 â 0.2 â "setosa" â
â 2 â 4.9 â 3.0 â 1.4 â 0.2 â "setosa" â
â 3 â 4.7 â 3.2 â 1.3 â 0.2 â "setosa" â
â 4 â 4.6 â 3.1 â 1.5 â 0.2 â "setosa" â
â 5 â 5.0 â 3.6 â 1.4 â 0.2 â "setosa" â
â 6 â 5.4 â 3.9 â 1.7 â 0.4 â "setosa" â
詳ãã使ãæ¹ã¯CSV.jlã®ãã«ããåç §ãã¦ãã
SQLiteã®ãã¼ã¿ãèªã¿ãããã ãã©?
SQLite.jlã使ãããSQLãçºè¡ããçµæã¯DataFrame
ã¨ãã¦è¿ãã¦ããããã
JSONãæ±ãã«ã¯?
JSON.jlã使ããã
julia> using JSON
julia> JSON.parse("""{"foo": 100, "bar": [1.1, 2.0]}""")
Dict{String,Any} with 2 entries:
"bar" => Any[1.1,2.0]
"foo" => 100
XMLãHTMLã®ãã¼ã¿ãå¦çããã«ã¯?
æä½ã®EzXML.jlããªã¹ã¹ã¡ã ããé·ã使ããã¦ããLightXML.jlã¨ããã®ãããããã©ãæ©è½ãå°ãªããããã¾ãã¤ã³ã¿ã¼ãã§ã¼ã¹ãããããããªãã...
julia> using EzXML
julia> readxml("ex1.xml")
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fcda9030400>))
julia> readxml("ex1.xml") |> print
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING" tag="first">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
ã§ãHTMLã«é¢ãã¦ã¯Googleã®gumboãã©ããããGumbo.jlã®æ¹ãè¯ãããª?
dplyr�
æ®å¿µãªããdplyrã»ã©ã®å®æ度ã®ããã±ã¼ã¸ã¯ä»ã®ã¨ããJuliaã«ã¯ç¡ãããªã ãã Juliaã®äººã«ã¯R使ããå¤ãã®ã§ãdplyrã®å©ä¾¿æ§ã¯èª°ããèªããã¨ããã ãã ããã§æè¿ã¯ä¼¼ããã¨ãJuliaã§ãããã¨ãã¦ãã人éãåºã¦ãã¦ãããã¤ãããã±ã¼ã¸ãã§ãã¦ãããã
Juliaã®ãã¼ã¿ãã¬ã¼ã ã®è¿æ³ã¨å°æ¥ã«ã¤ãã¦ã¯ããã«ã¾ã¨ã¾ã£ã¦ããã
UTF-8以å¤ã®ãã¡ã¤ã«ãèªã¿è¾¼ã¿ãããã ãã©?
StringEncodings.jlã使ãã! libiconvã使ã£ã¦ãã®ã§SJISã¿ãããªã¢ã¬ãªã¨ã³ã³ã¼ãã£ã³ã°ã§ãã¡ããã¨æ±ããã!
çµ±è¨/æ©æ¢°å¦ç¿
å¹³åã¨ãåæ£ã®è¨ç®ã¯?
mean
, median
, var
, cov
, cor
ã¯Rã¨ååã®é¢æ°ãæ¨æºã©ã¤ãã©ãªã«ããããæ¨æºåå·®ã¯sd
ã§ãªãstd
ã«ãªã£ã¦ãã®ã§æ³¨æã ãã
rowSums
ã¨ãcolSums
ã¨ãã¯?
sum(array, dim)
ã使ãããã
julia> x
2Ã3 Array{Int64,2}:
1 2 3
4 5 6
julia> sum(x, 1) # colSums
1Ã3 Array{Int64,2}:
5 7 9
julia> sum(x, 2) # rowSums
2Ã1 Array{Int64,2}:
6
15
ä»ã«ãmean
ãvar
ãªããã§ãåããã¨ãã§ãããã
ä¹±æ°çæã¯ã©ãããã®?
ä¸æ§ä¹±æ°runif(n)
ã«å¯¾å¿ããã®ã¯rand(n)
ãæ£è¦åå¸rnorm(n)
ã«å¯¾å¿ããã®ã¯randn(n)
ã ãã
ã¬ã³ãåå¸ã¨ããã¢ã½ã³åå¸ã¿ãããªä»ã®åå¸ã¯?
Distributions.jlã使ããã
PCA(主æååæ)ãããããã ãã©?
MultivariateStats.jlã使ããã
julia> using MultivariateStats
julia> using RDatasets
julia> iris = dataset("datasets", "iris")
julia> pca = fit(PCA, Matrix(iris[:,1:4])') # è¡åã«å¤æãã¦è»¢ç½®
PCA(indim = 4, outdim = 3, principalratio = 0.99479)
julia> transform(pca, Matrix(iris[:,1:4])')
3Ã150 Array{Float64,2}:
2.68413 2.71414 2.88899 2.74534 2.72872 2.28086 ⦠-1.94411 -1.52717 -1.76435 -1.90094 -1.39019
0.319397 -0.177001 -0.144949 -0.318299 0.326755 0.74133 0.187532 -0.375317 0.0788589 0.116628 -0.282661
0.0279148 0.210464 -0.0179003 -0.0315594 -0.0900792 -0.168678 -0.177825 0.121898 -0.130482 -0.723252 -0.36291
GML(ä¸è¬åç·å½¢ã¢ãã«)ãæ±ããããã ãã©?
GLM.jlã使ãããRã§ãæåãªDouglas Bateså çã®ä½ã£ãããã±ã¼ã¸ã ããå®å¿ã ã!
Lasso�
Juliaå®è£ ã®Lasso.jlãglmnetãã©ããããGLMNet.jlãè¯ãã¨æããã
Stanã使ããããã ãã©?
Stan.jlã使ãã!
è¡åç©ã®è¨ç®ã¯?
Rã®%*%
ãJuliaã§ã¯*
ã ãã
julia> A = randn(2, 3);
julia> B = randn(3, 4);
julia> A * B
2Ã4 Array{Float64,2}:
2.42828 -1.39917 0.28215 -1.61981
0.292669 -0.411146 0.234041 1.82686
R> A <- matrix(rnorm(6), 2)
R> B <- matrix(rnorm(12), 3)
R> A %*% B
[,1] [,2] [,3] [,4]
[1,] 0.8448714 -0.6084743 -0.7458281 -0.653775
[2,] 0.2133118 -0.9305351 0.3933490 1.105863
solve
ã¿ãããªç·å½¢æ¹ç¨å¼ã®è§£ã®è¨ç®ã¯?
A * X = B
ã解ãã«ã¯ãA \ B
ã¨è¨ç®ãããã
julia> A = randn(3, 3)
3Ã3 Array{Float64,2}:
0.864382 -0.945314 -0.496754
0.119709 0.83993 -0.962021
-0.737123 0.221293 0.205341
julia> X = randn(3, 2)
3Ã2 Array{Float64,2}:
-0.224859 1.64374
0.0726233 0.898944
-0.387804 0.697085
julia> B = A * X
3Ã2 Array{Float64,2}:
-0.0703728 0.224752
0.407157 0.28121
0.102188 -0.869566
julia> A \ B
3Ã2 Array{Float64,2}:
-0.224859 1.64374
0.0726233 0.898944
-0.387804 0.697085
julia> A \ B â X
true
A
ãå®å¯¾ç§°è¡åã§ãããã¨ãåãã£ã¦ããã ãã©?
cholfact(A)
ã§ã³ã¬ã¹ãã¼å解ãã¦ããã§ãåæ§ã«è¨ç®ã§ãããï¼
julia> A = randn(3, 3);
julia> A = A'A
3Ã3 Array{Float64,2}:
1.13074 2.65079 -0.0602727
2.65079 7.18991 -0.474305
-0.0602727 -0.474305 0.488933
julia> issymmetric(A)
true
julia> X = randn(3, 2)
3Ã2 Array{Float64,2}:
0.809623 0.595824
-0.25161 -2.33654
0.97426 1.46868
julia> B = A * X;
julia> A \ B
3Ã2 Array{Float64,2}:
0.809623 0.595824
-0.25161 -2.33654
0.97426 1.46868
julia> cholfact(A) \ B
3Ã2 Array{Float64,2}:
0.809623 0.595824
-0.25161 -2.33654
0.97426 1.46868
log(sum(exp(x)))
ã¨ãlog(exp(x)+1)
ã¨ããå®å®çã«è¨ç®ããããã ãã©?
StatsFuns.jlã«æ¨æºã©ã¤ãã©ãªã«ã¯ãªããã©ãæ°å¤è¨ç®ã§ããè¦ãé¢æ°ã®å®è£ ããããããã£ã¡ãæ¢ãã¦ã¿ããã
julia> x = randn(10)
10-element Array{Float64,1}:
0.0410153
-0.407537
1.26758
-0.52522
-0.0969684
-2.52716
-0.633663
-1.2695
-0.474312
-0.800294
julia> log(sum(exp(x)))
2.165782421890441
julia> logsumexp(x)
2.165782421890441
julia> log(sum(exp(1000x))) # 1000åããã¨è¨ç®ã§ããªã
Inf
julia> logsumexp(1000x) # StatsFuns.jlã®å®è£
ãªã大ä¸å¤«
1267.5802777924926
ããããä½ãã«ã¯ã©ãããã®?
æ®å¿µãªãããä»ã®ã¨ããJuliaã«ã¯æ±ºå®çãªããããã®ããã®ä»çµã¿ãããããã§ã¯ãªãã®ã§ã次ã®ãã®ãåèã«å¥½ã¿ã«åã£ãã®ã使ãã!
- Gadfly.jl
- æãæ´å²ã®ããJuliaã®
- ä¸çªã¹ã¿ã¼ãå¤ã
- Plots.jl
- æ°èã§æ¥æé·ä¸ã®ã©ã¤ãã©ãª
- éçºãæ´»çº
- PlotlyJS.jl
- Plot.lyã®ã©ããã¼ã©ã¤ãã©ãª
- ã¤ã³ã¿ã©ã¯ãã£ããªå¯è¦åãå¯è½
- PyPlot.jl
- Pythonã®matplotlibã®ã©ããã¼ã©ã¤ãã©ãª
- æãé«æ©è½ãã
- GR.jl
- GRãã¬ã¼ã ã¯ã¼ã¯ã®ã©ããã¼ã©ã¤ãã©ãª
- ããã©ã¼ãã³ã¹ã«åªãã
- UnicodePlots.jl
- Unicodeæåã使ã£ã¦ã¿ã¼ããã«ä¸ã§ããããã§ããã©ã¤ãã©ãª
- ããç°¡åãªããããããã©ãã¨è¦ããã¨ãã«åãã¦ã
é¢æ°ã®æ大ã»æå°å¤ãæ¢ããããã ãã©?
Optim.jlã使ãã!
julia> using Optim
julia> rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
rosenbrock (generic function with 1 method)
julia> optimize(rosenbrock, randn(2))
Results of Optimization Algorithm
* Algorithm: Nelder-Mead
* Starting Point: [0.4289709134439212,-1.3041568238701216]
* Minimizer: [1.0000314591723356,1.0000648302370259]
* Minimum: 1.354834e-09
* Iterations: 69
* Convergence: true
* â(Σ(yáµ¢-yÌ)²)/n < NaN: false
* Reached Maximum Number of Iterations: false
* Objective Function Calls: 136
R> rosenbrock <- function (x) (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
R> optim(rnorm(2), rosenbrock)
$par
[1] 0.9995882 0.9991591
$value
[1] 2.002849e-07
$counts
function gradient
129 NA
$convergence
[1] 0
$message
NULL
Juliaã®æ©æ¢°å¦ç¿ããã±ã¼ã¸ã¯ã©ããªã£ã¦ãã®?
Juliaã¦ã¼ã¶ã«ã¯æ©æ¢°å¦ç¿ã常ç¨ãã人ãå¤ãããã©ãPythonãRã»ã©å å®ã¯ãã¦ãªããã ä¸å¿ãScikitLearn.jlã¨ããscikit-learnã®ã©ããã¼ãJuliaã®ããã±ã¼ã¸ã«ãªã£ã¦ãã®ã§ãscikit-learnã§ã§ãããã¨ã¯ã§ããã¯ãã ãã
Gradient boostingã¨ãã¯?
XGBoost.jlã使ããã! ååããåããéãXGBoostã¸ã®Juliaã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã ãã
Deep learningã®ã©ã¤ãã©ãªã¯?
MXNet.jlã¨ããMXNetã®ããã±ã¼ã¸ãããããã¾ãDMLCã ããä»ã«ã¯Mocha.jlã¨ããJulia製ã®ãã¬ã¼ã ã¯ã¼ã¯ãããããã©ãã¡ã¤ã³ã®éçºè ãmxnetã®æ¹ã«ç§»ã£ããããªã®ã§ä»ã¯ããã»ã©éçºãæ´»çºã§ããªãããªã ä»ã«ã¯TensorFlow.jlã¨ããPythonã®TensorFlowã®ã©ããã¼ã©ã¤ãã©ãªããããã
ãã£ã¨Juliaã£ã½ããã¤ã ã¨ãTransformations.jlã¨ãNAISTã®é²è¤å çãä½ã£ã¦ããMerlin.jlã¨ããããã±ã¼ã¸ããããã
çµå±ãJuliaã§ãã¼ã¿è§£æã¯ã©ããªã®?
Juliaã¯æ©æ¢°å¦ç¿ã®ã¢ã«ã´ãªãºã ãçµãã«ã¯è¯ãè¨èªã ã¨æããã©ãã©ã¤ãã©ãªããã¼ã«ãã¨ã¦ãå°ãªããã ãªã®ã§ç¾ç¶ãã¤ã³ã¿ã©ã¯ãã£ããªæ¢ç´¢çãã¼ã¿è§£æ(exploratory data analysis)ããããããªå ´åã«ã¯JuliaããRã®æ¹ããªã¹ã¹ã¡ãããã
ãã ãæè¿JuliaMLã¨ããæ©æ¢°å¦ç¿ã«ç¹åããã³ãã¥ããã£ãã§ãã¦Juliaã§æ°è»½ã«æ©æ¢°å¦ç¿ãã§ããããã«ãã¦ãããããã¼ã¿ãã¬ã¼ã ã«æ¬æ ¼çã«è¦ç´ããã¨ãã¦ãããã¨ããæ¹åã«åãã¦åãã¦ããããä»ãªããããã¨ãããããããã®ã§ãæ´»åã«åå ãã¦ã¿ã¦ã¯ã©ãããª?
Juliaç¾ç¶ç¢ºèª (è¨èªç·¨)
ããã¯Juliaã¢ããã³ãã«ã¬ã³ãã¼1æ¥ç®ã®è¨äºã§ãã2016å¹´ã«ãããJuliaã®ãç¾ç¶ãã確èªãã¦ãããã¨æãã¾ãã
Juliaè¨èªã¨ã¯
Juliaã¨ã¯ä¸ä½ã©ã®ãããªè¨èªã§ãããããjulialang.orgã®æåã®èª¬æãç¨ããã¨ããä»ã®æè¡è¨ç®ç°å¢ã®ã¦ã¼ã¶ã«é¦´æã¿ã®ããæ§æãåãããé«ã¬ãã«ã»é«ããã©ã¼ãã³ã¹ãªæè¡è¨ç®ã«ããã®åçããã°ã©ãã³ã°è¨èªã("a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical computing environments.")ã§ããããã§è¨ããä»ã®æè¡è¨ç®ç°å¢ãã¨ã¯ãMATLABãPythonã®ãã¨ãæãã¦ããã¨èãããã¾ããå®éãJuliaã®æ§æãé¢æ°åã¯MATLABã¨ã®é¡ä¼¼æ§ãé«ããããOctaveã®ãããªãªã¼ãã³ã½ã¼ã¹çã®MATLAB代æ¿ã¨æãããããã¨ãå¤ãããã§ãããããã°ã©ãã³ã°è¨èªã¨ãã¦ã¯ããªãæ§è³ªãç°ãªãã¾ããã¾ããPythonã¨ã¯ææ³ãé¢æ°åã®é¢ã§ã¯ããã»ã©é¡ä¼¼æ§ã¯ç¡ãããã«æãã¾ãããä¼¼ã¦ããç¹ãå¤æ°ããã¾ãã
Juliaã®æ大ã®ç¹å¾´ãæãããªãã°ãåçããã°ã©ãã³ã°è¨èªã§ãããªããCè¨èªãªã©ã«å¹æµããã»ã©å®è¡ãé«éã§ããã¨ããç¹ã§ããããå¤ãã®æè¡è¨ç®ã§ã¯é¢æ°ã®å¼ã³åºããã«ã¼ããé »ç¹ã«ç¨ãããã¾ãããJuliaã§ã¯å®è¡æã³ã³ãã¤ã«(JIT)ã®ãããã§ãããã®å®è¡ã³ã¹ãããã¤ãã£ãã³ã¼ãã«ã³ã³ãã¤ã«ããããã°ã©ãã³ã°è¨èªä¸¦ã«ãªã£ã¦ãã¾ããããããJuliaãè¡ãå®è¡æã®ã³ã³ãã¤ã«ã¯ãä»ã®è¨èªã§ããã¿ãããJITã³ã³ãã¤ã©ã¨ã¯æ§è³ªãç°ã«ããããã§ããPyPy(Pythonã®JITã³ã³ãã¤ã©å®è£ )ãLuaJIT(Luaã®JITã³ã³ãã¤ã©å®è£ )ã¯Tracing JITã¨å¼ã°ãããã®ã§ãå®è¡æã«ãããã¡ã¤ã«ãè¡ã£ã¦ãããã¹ããããåå®ããå®è¡æã®æ å ±ãéããã³ã³ãã¤ã«ã¨æé©åãè¡ãã¾ããããããJuliaã®JITã³ã³ãã¤ã©ã¯ã³ã¼ãã®å®è¡åã«åæ¨è«ã¨æé©åãè¡ãããã®å¾ã³ã¼ããå®è¡ãã¾ããåæ¨è«ã®çµæãåã決å®ã§ããé¨åã§ã¯å¼·åãªæé©åãè¡ããææ§æ§ãæ®ãé¨åã§ã¯å®è¡æã®åã«åããã¦å®è¡ã§ããããã«ãã¦ãã¾ãã
Juliaè¨èªã®ç¾ç¶
Juliaè¨èªã®å®è£ ã§ããJuliaå¦çç³»ã¯ãã¹ã¦GitHubä¸ã§éçºããã¦ãã¾ããã¬ãã¸ããªã¯ãã¡ã( https://github.com/JuliaLang/julia)ã§ãã2016å¹´12æ1æ¥ç¾å¨ã®ææ°ã®ãªãªã¼ã¹çã¯v0.5.0ã§ã9æ20æ¥é ãªãªã¼ã¹ããããã®ã§ãã次æãã¼ã¸ã§ã³ã¯v0.6ã§ã»ã¼ééããªãã¨æãã¾ããæ°ã«ãªãã®ã¯v1.0ã®ãªãªã¼ã¹ææã§ãããä»å¹´ã®JuliaConã§Juliaã®çã¿ã®è¦ªã®ã²ã¨ãã§ããStefan Karpinskiæ°ã宣è¨ãããã¨ã«ããã¨ãJulia 1.0ã®ãªãªã¼ã¹ã¯2017å¹´ãäºå®ãã¦ããããã§ãã次æãã¼ã¸ã§ã³ã®v0.6ã¯v1.0ã®Î±ççãªæå³åãã§éçºããã¦ãããããªã®ã§ãv0.6ãåºã¦ããv1.0ããªãªã¼ã¹ããããã®ã¨æããã¾ãããããå®ã¯ãv1.0ãåºã¦ãã°ãã1.0ã®ã¾ã¾ã¨ãããã¨ã¯ãªãã1-2å¹´ã®ãã¡ã«v2.0ãåºãã¤ããã®ããã§ãã
æ¯ãè¿ã£ã¦ãç¾è¡ã®v0.5ã§ã¯ã©ã®ãããªå¤æ´ããã£ãã®ãã確èªãã¦ã¿ã¾ããã¾ãæ§è½é¢ã§å¤§ããªå½±é¿ããã£ãã®ããã¯ãã¼ã¸ã£ãé«éã«ãªã£ããã¨ã§ããv0.4ã¾ã§ã®Juliaã§ã¯ãé¢æ°ããã¹ã¦ä¸ã¤ã®åã«ã¾ã¨ãããã¦ããæé©åã®æ©æµãåãã«ããã£ãã®ã§ãããv0.5ããã¯é¢æ°ãå¥ã
ã®åãæã¤ããã«ãªãã¾ãããããã«ãã£ã¦ãç¡åé¢æ°ãªã©ã®é¢æ°ã®å¼ã³åºããé«éåãããå®å¿ãã¦ä½¿ããããã«ãªãã¾ãããä¾ãã°ãmap(x -> 2x, array)
ã®ãããªã³ã¼ããforã«ã¼ãã¨åçã®é度ãåºãããã«ãªãã¾ããã¾ããASCIIã¨UTF-8ã«åããã¦ããæåååãString
åã«çµ±ä¸ãããå
é¨ã®ã¨ã³ã³ã¼ãã£ã³ã°ãæ°ã«ãã使ããããã«ãªãã¾ãããããã§ãJuliaå
ã§ã¯å¸¸ã«UTF-8ã®ã¨ã³ã³ã¼ãã£ã³ã°ã®ã¿ãèæ
®ããã°ãããããã°ã©ã ãæ¸ãã®ã大å¤æ¥½ã«ãªãã¾ãããããã«ãæ°æ©è½ã¨ãã¦ãå®é¨çã«ãã«ãã¹ã¬ããã£ã³ã°ã®ãµãã¼ããå
¥ãã¾ãããä»ã®ã¨ãããJuliaå
é¨ã§ã¾ã ãã«ãã¹ã¬ããã£ã³ã°ã«å¯¾å¿ãã¦ããªãé¨å(ä¾ãã°IOãªã©)ãããã®ã§ã常ã«åé¡ãªã使ããããã§ã¯ããã¾ãããã競åãªã©ããªãå ´åã«ã¯forã«ã¼ãã«@threads
ãã¤ããã ãã§ä¸èº«ã並åã«å®è¡ãã¦ããã¾ãããããã®å¤æ´ã®ä»ã«ããgeneratorå¼(i for i in 1:10
ãªã©ãå¼ã¨ãã¦åã渡ãã§ãã)ãfusingæ§æ(sin.(cos.(x))
ãªã©ããã¯ãã«x
ã«å¯¾ãã¦ä¸æé
åãä½ããªã)ãªã©ã®æ°ããæ©è½ãå°å
¥ããã¦ãã¾ãã
ã§ã¯ãv0.6ã¯ã©ããªãã§ãããããv0.6ã¯v1.0ã®ãªãªã¼ã¹ã®ããã«ãã¤ãããæ©ãã«feature freezeãå ¥ãããã§ãã®ã§ãããããæ°æ©è½ãå¤æ´ç¹ãªã©ãåºæãã¨æãã¾ãããã®ä¸ã§ãããããä¸çªå¤§ããªå¤æ´ãåã·ã¹ãã ã®å¤æ´ã«ãªãã§ããããå®è£ ã¯ãã(https://github.com/JuliaLang/julia/pull/18457)ã«ãã«ãªã¯ãããã®ã§ãããã«è©¦ããã¨ãã§ãã¾ãããããå°ã詳ãã説æãã¦ã¿ã¾ãã
Juliaã«ã¯Set
ã¨å¼ã°ããéåã表ãåãããã¾ããããã¯åãã©ã¡ã¼ã¿ãåããã¨ãã§ããåã§åãã©ã¡ã¼ã¿ãT
ã¨ããã¨ãSet{T}
ã¨æ¸ãã¾ããã¤ã¾ãSet{T}
ã¯T
åã®è¦ç´ ãæã¤éåã§ãããã®ã¨ãåãã©ã¡ã¼ã¿T
ã¯ã©ã®ãããªåã§ãè¯ãã®ã§ãã(注: å®è¡æã«ã¯hash
ãå®è£
ãã¦ãããªã©ã®å¶ç´ããã)ãéã«T
ã«ä½ããã®å¶ç´(T<:Integer
ãªã©)ãä»ããåã¯ä½ããã¨ãã§ãã¾ããã§ããããããã次æãã¼ã¸ã§ã³ã®Juliaã®åã·ã¹ãã ã§ã¯ãSet{T} where T <: Integer
ãªã©ã®ããã«T
åã«å¶ç´ãã¤ãããã¨ãã§ãã¾ãã以ä¸ã®ä¾ã§ã¯ãåå¶ç´ããªãå ´åã¨ããå ´åã§ã©ã®ããã«ãµãã¿ã¤ãã³ã°ã®çµæãç°ãªããã示ãã¦ãã¾ãã
julia> Set{T} where T # åå¶ç´ãªã Set{T} where T julia> Set{Int} <: (Set{T} where T) true julia> Set{Float64} <: (Set{T} where T) true julia> Set{T} where T <: Integer # åå¶ç´ãã Set{T} where T<:Integer julia> Int <: Integer true julia> Set{Int} <: (Set{T} where T <: Integer) true julia> Float64 <: Integer false julia> Set{Float64} <: (Set{T} where T <: Integer) false
ãµãã¿ã¤ãã³ã°ã«ä½¿ããã¨ãããã¨ã¯ãå¤éãã£ã¹ãããã«ã使ããã¨ãããã¨ã§ããå®éã以ä¸ã®æ§ãªå¼æ°ã®åãæ¸ãã¾ãã
julia> f(xs::(Tuple{V,V} where V <: AbstractVector{T}), x::T) where T <: Integer = (xs[1] + x, xs[2] - x) f (generic function with 1 method) julia> f(([1,2,3], [1,2,3]), 2) ([3,4,5],[-1,0,1])
ãããå®éã©ã®ããã«å½¹ç«ã¤ã®ãã¯ãå®ä¾ãç¡ãã®ã§ç§ã«ãã¾ã ããåãã£ã¦ãã¾ãããã¾ããè¨æ³ã«é¢ãã¦ã¯ã¾ã å¤æ´ãå ¥ãããããã¾ããã
ããã²ã¨ã¤éè¦ãªæ°æ©è½ã¯ãä¾åããé¢æ°ã®èªåæ´æ°ã§ããJulia v0.5ã§ã¯ä»¥ä¸ã®ããã«é¢æ°g
ãå®è¡ããå¾f
ãæ´æ°ãã¦ãf
ã®å¤æ´ã¯åæ ããã¾ããã§ããã
julia> f(x) = x + 1 f (generic function with 1 method) julia> g(x) = f(2x) g (generic function with 1 method) julia> g(1) 3 julia> f(x) = x - 1 WARNING: Method definition f(Any) in module Main at REPL[1]:1 overwritten at REPL[4]:1. f (generic function with 1 method) julia> g(1) 3
ããã§ã¯ãéçºä¸ãªã©é »ç¹ã«é¢æ°ãæ¸ãæããã¨ãã«ãã¡ãã¡åèµ·åããå¿
è¦ãããã¾ããJuliaã¯ã³ã¼ããã³ã³ãã¤ã«ãããããã®ãããªåçãªå¤æ´ã¯å®è£
ä¸å¯è½ã ã¨ç§ã¯æã£ã¦ãã¾ããããç¾å¨é²è¡ä¸ã®å¤æ´(https://github.com/JuliaLang/julia/pull/17057)ãåãå
¥ããã¨ãf
ã®å¤æ´ãæ£ããg
ã®å®è¡ã«åæ ãããããã«ãªãã¾ãã
julia> f(x) = x + 1 f (generic function with 1 method) julia> g(x) = f(2x) g (generic function with 1 method) julia> g(1) 3 julia> f(x) = x - 1 WARNING: Method definition f(Any) in module Main at REPL[1]:1 overwritten at REPL[4]:1. f (generic function with 2 methods) julia> g(1) 1
大å¤ä¸æè°ã§ããã©ãããworldã¨ããå©ç¨å¯è½ãªã¡ã½ããããã§ãã¯ã§ããããã«ããæ©æ§ãçµã¿è¾¼ã¾ãã¦ãããããªã®ã§ãããç§ã«ã¯è©³ããåããã¾ãããããä½ãåããã¾ããããã¾ãè¨äºã«ãããã¨æãã¾ãã
ãã®è¨äºã§ã¯Juliaè¨èªã®ç¾ç¶ãç°¡åã«ã¾ã¨ãã¦ã¿ã¾ãããJuliaè¨èªä»¥å¤ã®ã³ãã¥ããã£ã®ååãªã©ã¯ãä»ã®æ©ä¼ã«ã¾ããç´¹ä»ãããã¨æãã¾ããææ¥ã¯ãIshitaããã®æ å½ã§ãã
Juliaè¨èªã®0.5ã®å¤æ´ç¹
9æ20æ¥ã«Juliaè¨èªã®ææ°çã§ãã0.5ããªãªã¼ã¹ããã¾ãããJuliaã®ã¡ã¼ãªã³ã°ãªã¹ãã«æ稿ãããã¢ãã¦ã³ã¹ã¡ã³ãã¯ãã¡ãã§ã: https://groups.google.com/d/msg/julia-users/J2DiH1GnM8o/aO2Ku8o-CgAJ
ãã£ã¨è¿ããã¡ã«æ¬å®¶ã®ããã°ã§è©³ããå¤æ´ç¹ã®ç´¹ä»ãããã¨æãã¾ãããç§ã®ããã°ã§ãä¸è¶³å ã«ä¸»è¦ãªå¤æ´ç¹ããç´¹ä»ãããã¨æãã¾ãã
ã¯ãã¼ã¸ã£ã®å¹çå
ã¨ãããéè¦ãªå¤æ´ç¹ã¨ãã¦æããããã®ãJuliaã®ã¯ãã¼ã¸ã£ãå¹çåããããã¨ã§ãã
0.4ã¾ã§ã®Juliaã§ã¯ãJuliaã®é¢æ°ã«é¢æ°ã渡ãããJuliaã§é¢æ°ãè¿ããããªé¢æ°ãä½ãã¨ããã®å®å¹é度ã極ãã¦é
ããã¨ãåé¡ã§ããã
ããã¯ããã¹ã¦ã®é¢æ°ã Function
ã¨ããåã«ã¾ã¨ãããã¦ããããã§ãJuliaã®ã³ã³ãã¤ã©ãç¹åããã³ã¼ããåããªãããã§ããã
ããã0.5ã§ã¯é¢æ°ã¯åã
èªåå°ç¨ã®åãæã¤ããã«å¤æ´ããããããã¯ãã¼ã¸ã£ã使ã£ãã³ã¼ãã§ãé
ããªãã¨ãããã¨ãç¡ããªãã¾ããã
é¢æ°ãåãåãé¢æ°ã®ä¾ã¨ãã¦ããä¸ããããã®ãmap
é¢æ°ã§ãããã
以ä¸ã®ç°¡åãªãã³ããã¼ã¯ãè¦ã¦ãJulia 0.5ã§ã¯0.4ã«æ¯ã¹ã¦10åã»ã©é度ãåä¸ãã¦ãã¾ãã
Julia 0.5.0:
julia> f = x -> 2x (::#1) (generic function with 1 method) julia> x = rand(100000); julia> map(f, x); julia> @time map(f, x); 0.000572 seconds (134 allocations: 789.078 KB)
Julia 0.4.6:
julia> f = x -> 2x (anonymous function) julia> x = rand(100000); julia> map(f, x); jjulia> @time map(f, x); 0.005628 seconds (200.01 k allocations: 3.815 MB)
ã¯ãã¼ã¸ã£ã使ããããªããã°ã©ã ã§ã¯ããã§ãã¶ã¤ã³ã®å¹ ãåºãããä»ã¾ã§å®è¡å¹çã®è¦³ç¹ããã§ããªãã£ãã³ã¼ããæ¸ããããã«ãªãã¾ãã
ã¸ã§ãã¬ã¼ã¿å¼
ã¸ã§ãã¬ã¼ã¿å¼ã¨ããæ°ããå¼ã0.5ããå ããã¾ããã
ããã¯ãã¤ãã¬ã¼ã¿ãã©ãããã¦å¼ã«ãããããªãã®ã§ãå¤ã次ã
çæãããã¨ãã§ããå¼ã§ãã
ç°¡åãªä¾ã§ã¯ã 2x for x in 1:9
ã®ãããªãã®ãæ®éã®å¤ã¨ãã¦æ±ããããã«ãªãã¾ããã
julia> g = (2x for x in 1:9) Base.Generator{UnitRange{Int64},##5#6}(#5,1:9) julia> collect(g) 9-element Array{Int64,1}: 2 4 6 8 10 12 14 16 18
ãã¡ããé¢æ°ã¯ãã®ã¸ã§ãã¬ã¼ã¿ãå¤ã¨ãã¦åãåããã¨ãã§ããããã次ã®ãããªè¨ç®ãã§ãã¾ãã
julia> sum(g) 90
ã¾ããif
ã§å¤ããã£ã«ã¿ã¼ãããã¨ãå¯è½ã§ãã
julia> h = (2x for x in 1:9 if isodd(x)) Base.Generator{Filter{Base.#isodd,UnitRange{Int64}},##7#8}(#7,Filter{Base.#isod d,UnitRange{Int64}}(isodd,1:9)) julia> collect(h) 5-element Array{Int64,1}: 2 6 10 14 18 julia> sum(h) 50
æåååã®çµ±å
Julia 0.4ã®æ¨æºã©ã¤ãã©ãªã«å¤§éã«ãã£ãæåååãæ´çããã0.5ã§ã¯ä¸»ã«String
åã¨SubString
åã®ã¿ã«ãªãã¾ããã
String
åã¯å¾æ¥ã®UTF8String
åã«å½ãããã®ã§ãã¦ãã³ã¼ãæååã表ç¾ã§ãã¾ããSubString
åã¯å¾æ¥ããããã¾ããããããã¯String
åã®ä¸é¨åãåãåºãéã«ä½¿ããã¦ãã¾ãã
å°æ¥çã«ã¯SubString
åãString
åã«çµ±åãããäºå®ã®ããã§ããã0.5æç¹ã§ã¯0.4ã®ASCIIString
ã¨UTF8String
ãString
åã«çµ±åãããã¨è¦ãã®ãè¯ãããã§ãã
Julia 0.5.0:
julia> typeof("foo") String julia> typeof("ããã") String
Julia 0.4.6:
julia> typeof("foo") ASCIIString julia> typeof("ããã") UTF8String
æã®æåååã¯LegacyStrings.jlããã±ã¼ã¸ã«ç§»åãããUTF-16ãªã©ã®ã¨ã³ã³ã¼ãã£ã³ã°æ¹å¼ã¯StringEncodings.jlããã±ã¼ã¸ã§æ°ãã«ãµãã¼ããããããã§ãã
Fused broadcastingæ§æ
Fused broadcastingæ§æã¯ãJuliaã®ãã¯ãã«è¨ç®ãå¹çåããæ°ããæ§æã®æ¡å¼µã§ãã 0.4ã¾ã§ã®Juliaã§ã¯ãé åã«å¯¾ãã¦ä½åº¦ãé¢æ°é©ç¨ãè¡ãã¨ãã®é½åº¦æ°ããé åãçæããã¦ãã¾ããã æ°ããJuliaã§ã¯ããããæ§æã¬ãã«ã§èå(fuse)ãããããã«ãªãã¾ãã
sin.(cos.(x))
ã¨ããå¼ããã£ãã¨ãã¾ãããã
0.4ã§ã¯ã¾ãcos.(x)
ãè©ä¾¡ãããx
ã®åè¦ç´ ã«cos
é¢æ°ãé©ç¨ããæ°ããé
åãä½ããã¾ãã
ç¶ãã¦ãã®åè¦ç´ ã«sin
é¢æ°ãé©ç¨ãããã¾ãæ°ããé
åãä½ãããå
ã»ã©ã®é
åã¯å°æ¥çã«GCã«ããç ´æ£ããã¾ãã
0.5ã§ã¯ããã®å¼ã¯ã¾ã broadcast(x -> sin(cos(x)), x)
ã®ãããªå¼ã«å¤æããã¾ãã
ããã¯ãx
ããå¤ãä¸ã¤ãã¤åãåºãã¦cos
ã¨sin
ãé ã«é©ç¨ãããããä¸æçãªé
åã®çæãèµ·ãããä¸æ°ã«2ã¤ã®é¢æ°ãé©ç¨ããé
åãçæããã¾ãã
ããã«ãx .= ...
ã®ãããªæ§æãbroadcast!(identity, x, ...)
ã«å¤æãããã®ã§é
åã¸ã®ä»£å
¥ãin-placeã«è¡ããã¨ãã§ããããã«ãªãã¾ããã
ãã«ãã¹ã¬ããã®ãµãã¼ã
ãã«ãã¹ã¬ããã使ã£ãè¨ç®ã®ä¸¦ååãæ°ãã«ãµãã¼ãããã¾ããã
ä»ã®ã¨ããã@threads
ãã¯ãã使ã£ã¦foræã®ä¸¦ååãè¡ããã¨ãã§ãã¾ãã
ä¾ãã°ã以ä¸ã®ããã«è¤æ°ã®ã¿ã¹ã¯ã並åã«å¦çãããã¨ãã§ãã¾ãã
using Base.Threads function dotasks(tasks) @threads for i in 1:endof(tasks) dotask(tasks[i]) end end
é åãªã©ãé åã§åå²ãã¦ããã°é åã«å¯¾ããå¦çã®ä¸¦ååãã§ãã¾ãããç§ã®ä½ã£ãBGZFStreams.jlã§ã¯gzipã®è§£åå¦çããã®ãã«ãã¹ã¬ããæ©è½ã使ã£ã¦ä¸¦ååãã¦ãã¾ãã
ãã¹ããã¬ã¼ã ã¯ã¼ã¯ã®å¼·å
Juliaæ¨æºã®ãã¹ããã¬ã¼ã ã¯ã¼ã¯ãããå¼·åããããã¹ãããããã«æ§é åã§ããããã«ãªãã¾ããã
ä»ã¾ã§Base.Test
ããæä¾ããã¦ããæ©è½ã§ã¯è¤æ°ã®ãã¹ããã¾ã¨ããæ©è½ããªãããã©ããã«ãã¹ã¦ã®ãã¹ããæ¸ãé£ããããæ¹æ³ãããã¾ããã§ããã
ããã0.5ããã¯@testset
ãã¯ããå ããã次ã®ããã«ãã¹ããæ§é åãããã¨ãã§ããããã«ãªãã¾ããã
@testset "sum" begin @test sum([1,2,3]) == 6 @test sum([1.0, 2.0, 3.0]) == 6.0 end
ãã®ä»ã®å¤æ´ç¹
0.5ã§ã¯å é¨çãªãã®ãå«ãå¤æ°ã®å¤æ´ããªããã¦ãã¾ãããã®ãã¹ã¦ã¯Juliaã®ãªãªã¼ã¹ãã¼ãããåç §ãããã¨ãã§ãã¾ãã®ã§ãæ°ã«ãªãæ¹ã¯æ¯é調ã¹ã¦ã¿ã¦ä¸ããã
JuliaããRã使ã
å æ¥ã®WACODEå¤æè¬ç¿ã§RCall.jlã®ãã¢ããããããã¯ãã¦ã±ãè¯ãã£ããããªã®ã§æ¹ãã¦ç´¹ä»ããã¾ãã
RCall.jlã¯JuliaããRè¨èªã®æ©è½ãå¼ã³åºããã¼ã«ã§ãããã¼ã¿ã®åã渡ãããREPLã§ã®ã¤ã³ã¿ã©ã¯ãã£ããªå®è¡ã»ãããããç°¡åã«ã§ãã¾ããJuliaã使ã£ã¦ã¿ãããã©ãRã®è±å¯ãªè³ç£ãæ¨ã¦ã訳ã«ã¯ãããªãã¨ãã£ãæ¹ã«ã¯ããã¿ãªã®ã©ã¤ãã©ãªã§ãã
ã¤ã³ã¹ãã¼ã«ã¯ãJuliaã®æ¨æºçãªæ¹æ³éããjulia -e 'Pkg.update(); Pkg.add(âRCallâ)â
ãå®è¡ãã¦ä¸ãããããã§ææ°çã®RCall.jlãã¤ã³ã¹ãã¼ã«ããããã¨ã«ãªãã¾ããå°ã次æJuliaã®ãªãªã¼ã¹åè£v0.5-RC1ã§ã¯ç¾å¨åããªãããã§ããããªãªã¼ã¹ãã©ã³ãã§ã¯ç´ã£ã¦ããã®ã§RC2ã§ã¯ä½¿ããã¨æãã¾ãã
ç°¡åãªæ¼ç®ã§æ£ããã¤ã³ã¹ãã¼ã«ã§ãããã確èªãã¾ããããJuliaã®REPLãèµ·åãã¦ãusing RCall
ã¨ãã¦RCall.jlãèªã¿è¾¼ã¿ãRâ1 + 1â
ã¨æã£ã¦Rè¨èªã§1 + 1
ãå®è¡ãã¦ã¿ã¾ãã以ä¸ã®ããã«è¨ç®çµæã表示ãããã°OKã§ãã
Rââ¦"
ã¨ããè¨æ³ã¯ãJuliaã®éæ¨æºæååã¨å¼ã°ããæ©è½ãå©ç¨ãããã®ã§ãããã®æååã®å
å´ã§ã¯Rã®ã³ã¼ãã¨ãã¦è§£éãããRã®å¦çç³»ãã³ã¼ããå®è¡ãã¦ããã¾ããããã«ã¯ã¦ã©ã¼ããå«ã¾ããå ´åã¯ããã¯ã¹ã©ãã·ã¥ã§ã¨ã¹ã±ã¼ãããããRâââ ⦠âââ
ã¨ããªãã«ã¯ã¦ã©ã¼ãã使ããã¨ãã§ãã¾ãã
Rå´ã®è©ä¾¡å¤ãåãåºãå ´åã«ã¯ãrcopy
ã使ãã¾ãã
julia> rcopy(R"1 + 1") 2.0 julia> rcopy(R"1:5") 5-element Array{Int32,1}: 1 2 3 4 5 julia> rcopy(R"""list(x=10, y="foo")""") Dict{Symbol,Any} with 2 entries: :y => "foo" :x => 10.0
Rå´ã¸Juliaã®å¤ã渡ãã«ã¯$
ã§å¤æ°ãåãè¾¼ã¿ã¾ãã
julia> x = 1 1 julia> R"1 + $x" RCall.RObject{RCall.RealSxp} [1] 2
Juliaã®DataFrames.jlããã±ã¼ã¸ãæä¾ãã¦ããDataFrame
ãªã©ãèªåçã«Rã®data.frame
ã¸ã¨å¤æãã¦ããã¾ãã
julia> using RDatasets julia> iris = dataset("datasets", "iris"); julia> typeof(iris) DataFrames.DataFrame julia> R"head($iris)" RCall.RObject{RCall.VecSxp} SepalLength SepalWidth PetalLength PetalWidth Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa
ãããããç°¡åã«ã§ãã¾ãã以ä¸ã®ããã«ããã¨ãJuliaãã渡ãããã¼ã¿ãRããããããã¦ããã¾ãã
julia> R"""pairs($iris[1:4], pch=21, bg=c("red", "green3", "blue")[unclass($iris$Species)])""" RCall.RObject{RCall.NilSxp} NULL
ãã¡ãã¡Rââ¦â
ã§å²ãã ããé¢åãªããRã®REPLã«å
¥ãã¾ããããJuliaã®REPLãã$ãã¼ãæ¼ãã¨ãRã®REPLã«å
¥ãã¾ããJuliaã«æ»ãã«ã¯ããã¯ã¹ãã¼ã¹ã§ãã
ããã§ã¯Rã®ææ³ãèªç±ã«ä½¿ããã®ã§ãRã¦ã¼ã¶ã¼ã«ã¨ã£ã¦ã¯ãããããã§ãããããã¡ãããJuliaå´ãããã¼ã¿ãåãåããã¨ãRââ¦â
ã®æã¨åãããã«å¯è½ã§ãã
R> library(ggplot2) R> ggplot($iris, aes(x=SepalLength, y=SepalWidth, color=Species)) + geom_point()
Rã®ã»ãã·ã§ã³ã§ã¯ãã«ããªã©ãåç §ã§ãã¾ããè£å®ãå¹ãã¾ããJuliaã¯æ°ã«ãªããã©Rããé¢ããã®ã¯ãã§ããâ¦ã¨ããæ¹ã¯æ¯éä¸åº¦è©¦ãã¦ã¿ã¦ãã ããã