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
kargoの魅力について伝える
magisystem0408
0
210
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
740
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
140
.NET 9 のパフォーマンス改善
nenonaninu
0
900
第3回Snowflake女子会_LT登壇資料(合成データ)_Taro_CCCMK
tarotaro0129
0
190
生成AIのガバナンスの全体像と現実解
fnifni
1
190
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
170
KnowledgeBaseDocuments APIでベクトルインデックス管理を自動化する
iidaxs
1
260
Wvlet: A New Flow-Style Query Language For Functional Data Modeling and Interactive Data Analysis - Trino Summit 2024
xerial
1
120
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
UI State設計とテスト方針
rmakiyama
2
580
Google Cloud で始める Cloud Run 〜AWSとの比較と実例デモで解説〜
risatube
PRO
0
100
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Gamification - CAS2011
davidbonilla
80
5.1k
RailsConf 2023
tenderlove
29
940
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Cost Of JavaScript in 2023
addyosmani
45
7k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Statistics for Hackers
jakevdp
796
220k
Visualization
eitanlees
146
15k
Agile that works and the tools we love
ras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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 ষ ٕज़ධࣾɹ੫ࠐԁ