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
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
230
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
390
Application Development WG Intro at AppDeveloperCon
salaboy
0
190
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
180
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
870
Lambdaと地方とコミュニティ
miu_crescent
2
370
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
190
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
Featured
See All Featured
Side Projects
sachag
452
42k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Practical Orchestrator
shlominoach
186
10k
Thoughts on Productivity
jonyablonski
67
4.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Producing Creativity
orderedlist
PRO
341
39k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
97
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Facilitating Awesome Meetings
lara
50
6.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Optimizing for Happiness
mojombo
376
70k
How to Ace a Technical Interview
jacobian
276
23k
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 *XSPUFB3D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4P*DSFBUFB3DQQGVODUJPODPOWFSUJOHUIFEBUB 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 ষ ٕज़ධࣾɹ੫ࠐԁ