Upgrade to Pro — share decks privately, control downloads, hide ads and more …

あまぁいRcpp生活

teuder
August 19, 2016

 あまぁいRcpp生活

Rの関数をC++で実装することを可能にするパッケージRcppの紹介です。

teuder

August 19, 2016
Tweet

More Decks by teuder

Other Decks in Technology

Transcript

  1. 3DQQΛར༻͍ͯ͠Δ ύοέʔδ਺ ύʔηϯςʔδ       

              Ҏ্ͷύοέʔδͰར༻͞Ε͍ͯΔ ύοέʔδ਺ 
  2. 1 RcppArmadillo 353992 0.0235 2 Rcpp 254575 0.0169 3 ggplot2

    219765 0.0146 4 digest 179117 0.0119 5 stringr 171733 0.0114 6 plyr 170953 0.0113 7 stringi 170182 0.0113 8 scales 167722 0.0111 9 magrittr 159638 0.0106 10 gtable 150171 0.0099 ࠷΋ଟ͘μ΢ϯϩʔυ͞Ε͍ͯΔ ύοέʔδͷͻͱͭ %-਺ ׂ߹ IUUQDSBOMPHTSTUVEJPDPN͔Β ೥݄ʹμ΢ϯϩʔυ͞ΕͨύοέʔδͷϥϯΩϯά
  3. 3UPPMTͷΠϯετʔϧ : jd* WeW g #H; B# P- !=6ooHca cS_

    [ WdooIooI)/*/*-ooT[ ooi207 U6ooIeaa dooT[ 7 U6ooIeaa do o_[ hR20ooT[ 7v wu : djdeW_ # '' )g# Ld[ Tf[ e)[ dbWUd* =GDD = RA==9U6oIeaa do_[ hR20oT[ oARRn-* O =GDD = RD GRNI;HH I9U6+Ieaa d+_[ hR20+T[ +**+ [TWiWU+ UU+i42R20)h20) _[ h/.+0*5*/+ ea)hcSbbWc*WiW v w cWSV _aVW 6 bad[i UU gWcd[a 0*5*/ i42R20)bad[i)dW ( <f[ e Tj E[ AN)N20 bca WUe ֬ೝͷͨΊ3Ͱ࣍ͷίϚϯυΛೖྗ͠ ԼͷΑ͏ͳग़ྗ͕ಘΒΕͨΒ0,
  4. E 55 : IUbb66dafcUW=bb # a]jaI*Ubb# : e[_Wd ha 0.

    P- 40 : e[_Wd ha U -(.(/ P- . 0 2 ϑΝΠϧ/FX'JMF$ 'JMF ద౰ͳ໊લͰϑΝΠϧʹอଘͯ͠4PVSDFΛԡ͢ʂ 3DQQͰ࡞੒ͨؔ͠਺͸௨ৗͷ3ؔ਺ͱಉ༷ʹ࢖༻Ͱ͖Δ
  5. E 55 // Rcpp Λར༻͢ΔͨΊʹඞཁͳϔομϑΝΠϧ #include <Rcpp.h> // ͜ΕΛهड़͠ͳ͍৔߹͸ Rcpp::NumericVector

    // ͱॻ͔ͳ͍ͱ͍͚ͳ͍ using namespace Rcpp; // ͜ͷهड़ͷ௚Լͷؔ਺͕R͔Βར༻ՄೳʹͳΔ // [[Rcpp::export]] // ؔ਺ͷఆٛ NumericVector timesTwo(NumericVector x) { return x * 2; } /* C++ ίʔυதʹRͷίʔυΛຒΊࠐΊΔ ίϯύΠϧ௚ޙʹࣗಈͰ࣮ߦ͞ΕΔ */ /*** R timesTwo(42) */
  6. df_I 8) Xf Ue[a i l df_ 8) , Xac

    ii [ i l df_ 8) df_ ' ii m cWefc df_ m ϕϯνϚʔΫ ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ
  7. ϕϯνϚʔΫ ++ PPIUbb66Wibace VafT W df_IUbb Ff_Wc[UMWUeac i l [

    e 9 i* W e 7 VafT W df_ 9 ,*,7 Xac [ e [9,7 [ 8 7 ''[ l df_ 9 df_ ' iP[ 7 m cWefc df_7 m ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ
  8. ϕϯνϚʔΫ ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ ++ PPIUbb66b f [ d Ubb-- ++ PPIUbb66Wibace

    VafT W df_IUbb=bb-- Ff_Wc[UMWUeac i l VafT W df_ 9 ,*,7 Xac VafT W ii 6 i l df_ 9 df_ ' ii7 m cWefc df_7 m
  9. ϕϯνϚʔΫ : cTW U _Sc]66TW U _Sc] ' df_I i

    ( ' df_ i ( ' df_IUbb i ( ' df_IUbb=bb-- i ( ' df_IUbb f Sc i ( ' Ua f_ d9U !eWde!( !cWb [USe[a d!( !W SbdWV!( !cW Se[gW! ( ' acVWc 9 FLDD( ' cWb [USe[a d9-,, eWde cWb [USe[a d W SbdWV cW Se[gW - df_I i -,, 1,*045 .30*/53 . df_ i -,, ,*.0/ -*/.- / df_IUbb i -,, ,*-5. -*,0/ 0 df_IUbb=bb-- i -,, ,*.,. -*,54 1 df_IUbb f_ i -,, ,*-40 -*,,, 3DQQ͸$ݴޠͰ࣮૷͞Εͨ3ͷؔ਺ΑΓ଎͍৔߹͕͋Δ
  10. 3DQQͷσʔλܕ ̋̋.BUSJY ஋ܕ 3 ϕΫτϧ 3DQQ ϕΫτϧ 3DQQ ߦྻ 3DQQ

    εΧϥʔ $  εΧϥʔ ࿦ཧ MPHJBM -PHJDBM7FDUPS -PHJDBM.BUSJY  CPPM ੔਺ JOUFHFS *OUFHFS7FDUPS *OUFHFS.BUSJY  JOU ࣮਺ OVNFSJD /VNFSJD7FDUPS /VNFSJD.BUSJY  EPVCMF ෳૉ਺ DPNQMFY $PNQMFY7FDUPS $PNQMFY.BUSJY 3DPNQMFY DPNQMFY จࣈྻ DIBSBDUFS $IBSBDUFS7FDUPS $IBSBDUFS.BUSJY 4USJOH TUSJOH ೔෇ %BUF %BUF7FDUPS  %BUF  ೔࣌ 104*9DU %BUFUJNF7FDUPS  %BUFUJNF UJNF@U
  11. 4VHBSؔ਺ ベクター関係 head, tail, rep_each, rep_len, rep, rev, seq_along, seq_len,

    cumsum, diff, pmin, pmax 確率分布 d/q/p/r 全てのRの提供する確率分布 数学関数 abs, acos, asin, atan, beta, ceil, ceiling, choose,cos, cosh, exp, expm1, factorial, floor, gamma, lbeta, lchoose, lfactorial, lgamma, log, log10, log1p, pentagamma, psigamma, round, signif, sin, sinh, sqrt, tan, tanh, tetragamma, trigamma, trunc ཁ໿౷ܭྔ NFBO NJO NBY TVN TE WBS ஋ͷݕࡧ NBUDI TFMG@NBUDI XIJDI@NBY XIJDI@NJO ॏෳͷ஋ EVQMJDBUFE VOJRVF BQQMZؔ਺ MBQQMZ TBQQMZ NBQQMZ ࿦ཧ஋ JGFMTF BMM BOZ
  12. ϕΫτϧͷ࡞੒ ++ g 8) cWb ,( -, Ff_Wc[UMWUeac g -,

    7 ++ g 8) U -(.(/ ++ PPIUbb66b f [ d Ubb-- Ff_Wc[UMWUeac g 9 l-(.(/m7 ++ g 8) U i9-( j9.( k9/ Ff_Wc[UMWUeac g 9 Ff_Wc[UMWUeac66UcWSeW FS_WV "i", - ( FS_WV "j" 9. ( RP"k" 9/ 7
  13. ϕΫτϧཁૉ΁ͷΞΫηε ࣮਺ɾ੔਺ɾจࣈྻɾ࿦ཧϕΫτϧͰΞΫηε Ff_Wc[UMWUeac [ 9 l,(.(0m7 C eW WcMWUeac [

    9 l,(.(0m7 = ScSUeWcMWUeac [ 9 l!i!(!j!(!k!m7 Da [US MWUeac [ 9 g:,7 Ff_Wc[UMWUeac c 9 gP[ 7 gP[ 9 gS 7
  14. ߦྻͷ࡞੒ ++ _ 8) _Sec[i ,( cah 9 .( Ua

    9 / Ff_Wc[UESec[i _ .( / 7 ++ _ 8) _Sec[i g( cah 9 .( Ua 9 / Ff_Wc[UESec[i _ .( / ( g*TW [ 7
  15. ߦྻͷཁૉ΁ͷΞΫηε ++ , . _ , ( . 7 ++

    , _ , ( R 7 ++ . _ R ( . 7 ++ ,s- .s/ _ IS W ,(- ( IS W .(/ 7
  16. σʔλϑϨʔϜͷ࡞੒ >SeS cS_W VX 9 >SeS cS_W66UcWSeW g-(g.(g/ 7 ++

    { >SeS cS_W66UcWSeW FS_WV ! -!( g- ( FS_WV ! .! 9g. ( RP! /! 9g/ 7
  17. σʔλϑϨʔϜͷཁૉ΁ͷΞΫηε ࣮਺ɾ੔਺ɾจࣈྻɾ࿦ཧϕΫτϧͰΞΫηε >SeS cS_W VX- 9 VXPg 7 VXPg 9

    VX.7 Ff_Wc[UMWUeac g- 9 VXP, 7 VXP!i! 9 g.7 ࣮਺ɾ੔਺ɾจࣈྻεΧϥʔͰΞΫηε ΧϥϜͷ஋ʹ͸g-Λհͯ͠ΞΫηε͢Δ VXP(- ΍VXP- P- ͳͲ͸ବ໨
  18. D[de D 9 D[de66UcWSeW g-(g.(g/ 7 ++ { D[de66UcWSeW FS_WV

    ! -!( g- ( FS_WV ! .! 9g. ( RP! /! 9g/ 7 ˞W W Wͷܕ΍αΠζ͸ԿͰ΋ྑ͍ Ϧετͷ࡞੒
  19. Ϧετͷཁૉ΁ͷΞΫηε ࣮਺ɾ੔਺ɾจࣈྻɾ࿦ཧϕΫτϧͰΞΫηε D[de D- 9 DPg 7 DPg 9 D.7

    Ff_Wc[UMWUeac g- 9 DP, 7 DP!i! 9 g.7 ࣮਺ɾ੔਺ɾจࣈྻεΧϥʔͰΞΫηε Ϧετཁૉͷཁૉʹ͸g-Λհͯ͠ΞΫηε͢Δ
  20. MWUeac M M* W e ++ M* S_Wd ++ ESec[i

    E E* cah ++ E* Ua ++ cah S_Wd E ++ Ua S_Wd E ++ >SeS cS_W > > * W e ++ > * S_Wd ++ > *Seec !cah* S_Wd! ++ D[de D D* W e ++ D* S_Wd ++ ϕΫτϧͷ௕͞΍ཁૉ໊ͳͲ
  21. ଐੑ஋ MWUeac M M*Seec ! S_Wd! ++ ESec[i E7 E*Seec

    !V[_! 9 C eW WcMWUeac66UcWSeW ( 7 E*Seec !V[_ S_Wd! 9 D[de66UcWSeW ( 7 >SeS cS_W > 7 > *Seec ! S_Wd! ++ > *Seec !cah* S_Wd! ++ D[de D7 D*Seec ! S_Wd! ++
  22. 3ͷؔ਺Λݺͼग़͢ f Ue[a X !c ac_! 7 Ff_Wc[UMWUeac cWd 9

    X 1( FS_WV !_WS !(- ( FS_WV !dV!(. 7 g[ca _W e W g 9 g[ca _W e66 S_WdbSUWRW g ! r ! 7 f Ue[a X 9 W gP! ! 7 ͜ͷํ๏Ͱύοέʔδؔ਺Λݺͼग़͢৔߹͸ MJCSBSZ ύοέʔδ໊ ͢Δඞཁ͕͋Δ ͪ͜Β͸MJCS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έʔε
  32. ++ X g. ~ ++ ga[V X Ff_Wc[UMWUeac g. l

    g.P, 957 m : X g- : bc[ e g- P- 5 . / 3DQQͷ஫ҙ఺ ϝϞϦʔ g- 5(.(/ g. ؔ਺ͷҾ਺ͷҰ෦ͷཁૉΛมߋ͢Δέʔε
  33. ඪ४$ ͷJOUಉ࢜ͷԋࢉࢠ ++ PPIUbb66Wibace C eW WcMWUeac SVVR[ eW Wc

    C eW WcMWUeac i( C eW WcMWUeac j l [ e 9 i* W e 7 C eW WcMWUeac k 7 Xac [ e [9,7 [8 7 ''[ l kP[ 9 iP[ ' jP[ 7 ++ [ e ' [ e m cWefc k7 m : i'j P- F; . : SVVR[ eW Wc i(j P- ).-0304/203 . : i 8) U - ( - : j 8) U F;( -