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
950
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
AWSの生成AIサービス Amazon Bedrock入門!(2025年1月版)
minorun365
PRO
7
470
実践! ソフトウェアエンジニアリングの価値の計測 ── Effort、Output、Outcome、Impact
nomuson
0
2.1k
Building Scalable Backend Services with Firebase
wisdommatt
0
110
AWSマルチアカウント統制環境のすゝめ / 20250115 Mitsutoshi Matsuo
shift_evolve
0
110
【Oracle Cloud ウェビナー】2025年のセキュリティ脅威を読み解く:リスクに備えるためのレジリエンスとデータ保護
oracle4engineer
PRO
1
100
FODにおけるホーム画面編成のレコメンド
watarukudo
PRO
2
280
PaaSの歴史と、 アプリケーションプラットフォームのこれから
jacopen
7
1.5k
Reactフレームワークプロダクトを モバイルアプリにして、もっと便利に。 ユーザに価値を届けよう。/React Framework with Capacitor
rdlabo
0
130
月間60万ユーザーを抱える 個人開発サービス「Walica」の 技術スタック変遷
miyachin
1
140
20250116_JAWS_Osaka
takuyay0ne
2
200
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
350
.NET AspireでAzure Functionsやクラウドリソースを統合する
tsubakimoto_s
0
190
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
Six Lessons from altMBA
skipperchong
27
3.6k
Designing for humans not robots
tammielis
250
25k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
How to Ace a Technical Interview
jacobian
276
23k
The Cult of Friendly URLs
andyhume
78
6.1k
Speed Design
sergeychernyshev
25
740
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
173
51k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
113
50k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
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 ! ͳ͚Ε࡞Δɻ ͦΕ͕ϗΫιΤϜͷ͍ͩͬͨͣʂ
IUUQTXXXHJUCPPLDP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BUJWFMZMBSHF BCPVUTFWFSBMUFOTPG(# df %>% asSparseMatrix 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 ষ ٕज़ධࣾɹ੫ࠐԁ