Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rcpp for everyone
Search
teuder
March 31, 2017
Technology
1
940
Rcpp for everyone
Brief introduction of learning resources for Rcpp.
teuder
March 31, 2017
Tweet
Share
More Decks by teuder
See All by teuder
あまぁいRcpp生活
teuder
1
450
Other Decks in Technology
See All in Technology
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
690
Taming you application's environments
salaboy
0
190
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
180
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
AI前提のサービス運用ってなんだろう?
ryuichi1208
8
1.4k
Lambdaと地方とコミュニティ
miu_crescent
2
370
Platform Engineering for Software Developers and Architects
syntasso
1
520
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
150
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
760
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
Featured
See All Featured
Music & Morning Musume
bryan
46
6.2k
Code Reviewing Like a Champion
maltzj
520
39k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
What's in a price? How to price your products and services
michaelherold
243
12k
Six Lessons from altMBA
skipperchong
27
3.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Designing for humans not robots
tammielis
250
25k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Being A Developer After 40
akosma
87
590k
Transcript
3DQQGPSFWFSZPOF ବ@teuder
"CPVUNF ాਅथ .BTBLJ&5TVEB 1I% -JGFTDJFODFT #VTJOFTT$POTVMUBOU XJUI %BUB4DJFODF
3DQQ 3DQQJTBQBDLBHFUIBUFOBCMFTZPV UPJNQMFNFOU3GVODUJPOTJO$ #ZVTJOH3DQQ ZPVDBOXSJUF$ DPEFJOBTUZMFTJNJMBSUP3
None
%JSL&EEFMCVFUUFM
5PEBZ *EPOPUUBMLBCPVUIPXUPVTF3DQQ CVUJOUSPEVDFZPVTPNFVTFGVMSFTPVSDFT XIFOZPVXBOUUPMFBSO3DQQ
5IFNBKPSSFTPVSDFTGPS MFBSOJOH3DQQ "EWBODFE3 3DQQ(BMMFSZ 3DQQ/PUF 7JHOFUUFT
"EWBODFE3 (SFBUJOUSPEVDUJPOUP3DQQ #VUJUMBDLTTPNFEFUBJMT )PXDBO*NPWFUPUIFOFYUTUFQ
3DQQ(BMMFSZ (SFBUDPNQJMBUJPOPGHPPEFYBNQMFT #VUUIFJOGPSNBUJPOJTGSBHNFOUFE )PXDBO*HFUUJEZLOPXMFEHFBCPVU3DQQ
3DQQ/PUF (SFBU$ TUZMFSFGFSFODFEPDVNFOU #VUJUPOMZQSPWJEFMJNJUFEJOGPSNBUJPO )PXDBO*HFUNPSFQSBDUJDBM BOENPSFTZTUFNBUJDBMLOPXMFEHF
*OUIFTFEBZT *DPVMEOPUpOEBEPDVNFOUMJLFUIBU*XBOU #VUPOFEBZ *SFNFNCFSFEBQISBTF *`WFTFFOPOUIFJOUFSOFU
If there does not exist what you want, just create
it. That should have been the oath of HOXO-M ! ͳ͚Ε࡞Δɻ ͦΕ͕ϗΫιΤϜͷ͍ͩͬͨͣʂ
IUUQTXXXHJUCPPLDPNCPPLUFVEFSJOUSPEVDUJPOUPSDQQEFUBJMT *XSPUFB3DQQ8FC#PPL
&OHMJTIWFSTJPOJTB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OHUIFEBUB EJSFDUMZUPTQBSTFNBUSJY
#include <Rcpp.h> using namespace Rcpp; %FpOJOHBGVODUJPO // [[Rcpp::export]] S4 asSparseMatrix(
DataFrame df ){ // prerequisite : // all the elements DataFrame is numeric/integer // and not containing NAs. // number of rows and columns int nrow = df.nrows(); int ncol = df.length();
6TJOHTUEWFDUPSJOTUFBEPG3DQQ7FDUPS #FDBVTFDIBOHJOHTJ[FPGWFDUPSBUSVOUJNF JTOPUFGpDJFOUJO3DQQ7FDUPS std::vector<R_xlen_t> rows; std::vector<R_xlen_t> cols; std::vector<double> vals;
4UPSJOHUIFQPTJUJPOTBOEWBMVFTPG OPO[FSPFMFNFOUTJOUIF%BUB'SBNF for(R_xlen_t col=0; col<ncol; ++col){ NumericVector column = df[col]; for(R_xlen_t row=0; row<nrow; ++row){ if(column[row]!=0.0){ rows.push_back(row+1); cols.push_back(col+1); vals.push_back(column[row]); } } }
$BMMJOH.BUSJYTQBSTF.BUSJY Environment env = Environment::namespace_env("Matrix"); Function sparseMatrix = env["sparseMatrix"];
$POWFSUJOHTUEWFDUPSUP/VNFSJD7FDUPS S4 sm = sparseMatrix( Named("i") = wrap(rows), Named("j") = wrap(cols), Named("x") = wrap(vals), Named("dims") = NumericVector::create(nrow,ncol)); 4FUUJOHSPXOBNFTBOEDPMOBNFT List dimnames = List::create(R_NilValue, df.names()); sm.attr("Dimnames") = dimnames; 3FUVSOJOHUIFTQBSTFNBUSJY return sm; }
$PODMVTJPO w :PVDBOJNQMFNFOU3GVODUJPOJO$ XJUI3DQQ w 5IFSFBSFTV⒏DJFOUSFTPVSDFTGPSMFBSOJOH3DQQ w 3DQQJTTVJUBCMFUPCSFBLCPUUMFOFDLTPGZPVSDPEF *IPQFNZl*OUSPEVDUJPOUP3DQQz XPVMEBJECSPBESBOHFPGQFPQMF
&OKPZ
"EWFSUJTJOH 605 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 24 ষɹɹRcpp Rcpp RcppRͷؔΛC++Ͱ࣮Ͱ͖ΔύοέʔδͰ͢ɻRͱྨࣅͨ͠ελΠϧͰهड़Ͱ͖ ΔΑ͏ʹ࣮͞Ε͍ͯΔͨΊɺC++ʹਂ͍͕ࣝͳͯ͘ར༻͘͢͠ͳ͍ͬͯ·͢ɻ͠ ͔ɺͦͷͨΊͷ࣮ߦ٘ਜ਼ʹ͞Ε͍ͯͳ͍ͷͰɺ୭ͰϋΠύϑΥʔϚϯεͳ݁ՌΛ ಘΔ͜ͱ͕Ͱ͖·͢ɻ 24-1 Rcppͷ׆༻γʔϯ ࣍ͷΑ͏ͳέʔεC++Ͱ࣮͢Δ͜ͱʹΑΓɺRͱൺͯߴԽ͕ݟࠐΊ·͢ɻ ɾ ܁Γฦ͠ॲཧɺಛʹ࣍ͷॲཧ͕લͷॲཧʹґଘ͓ͯ͠ΓฒྻԽͰ͖ͳ͍ ɾ ϕΫ τϧߦྻͷݸʑͷཁૉΞΫηε͢Δඞཁ͕͋Δ ɾ ϕΫ τϧͷαΠζΛಈతʹมߋ͍ͨ͠ ɾ ߴͳσʔλߏΞϧΰϦζϜΛ༻͍ͨॲཧΛߦ͍͍ͨ RcppͷύϑΥʔϚϯεΛࣔͨ͢Ίɺ܁Γฦ͠ॲཧͷྫͱͯ͠MCMCΞϧΰϦζϜͷҰछͰ͋ ΔΪϒεαϯϓϥʔ ʢ1ʣ ͷ࣮ྫΛࣔ͠·͢ɻ͜ͷྫͰɺΪϒεαϯϓϥʔʹΑΓඪ४2ม ਖ਼ن͔ΒnαϯϓϦϯά͍ͯ͠·͢ɻ ·ͣൺֱͷͨΊRΛ༻͍࣮ͨྫΛࣔ͠·͢ ʢϦετ24.1ʣ ɻ Ϧε τ24.1ɹGibbs.R GibbsR <- function(b, n, t){ # 2มඪ४ਖ਼ن͔ΒnαϯϓϦ ϯά # b : 2มͷڞࢄ # n : αϯϓϧ # t : αϯϓϦ ϯάࣺͤͣͯΔִؒ X <- matrix(0, nrow = n, ncol = 2) x1 <- x2 <- 0 sd <- sqrt(1-b^2) for(i in 1:n){ for(j in 1:t){ x1 <- rnorm(1, b*x2, sd) x2 <- rnorm(1, b*x1, sd) ʢ1ʣ ߴ࣍ݩͷෳࡶͳ֬ʹै͏ཚΛੜ͢ΔϚϧίϑ࿈ϞϯςΧϧϩ๏ͱݺΕΔΞϧΰϦζϜͷҰछͰ͢ɻ 24-1 24 ষ ٕज़ධࣾɹ੫ࠐԁ