t-rc R PA gCUSSrc Nmatt-wand.utsacademics.info/webRCPPDG/WandRcppPack... · T t-rc R cpp PA LWA GT...
Transcript of t-rc R PA gCUSSrc Nmatt-wand.utsacademics.info/webRCPPDG/WandRcppPack... · T t-rc R cpp PA LWA GT...
T t-rc R cpp PA LWA GT
DI gCUSSrc N G Rr: ltP
PARI 1 WAr*tU l,tfStopY l,tr if HI tot^J ,\l ESS o F R
ovfRcoMIAJ6
i)
hloud
J4u
Elu, led wxk o( 41 lSe. i,re*\ t4Jas (uu bnf*n R)
ani, li 4qey ou* ha, I Wri{e lr*-levnl
a,nguary, x gr,6Me lapc 4t a+ ca,, 6e
h&r &red \tui-/a fr usi,- ,,fi trr*{-,Ms
rFt>
Ir
5
INIYrA t- N0TE 9
M. P. wAN b s EprEM FeR 20 ts
9u& os *c{) qnl rFer-l"ran(),
a9\ucnl Ff.rdr tatn t C hf hns 1",1+I
E#r* 11 nt d n uttdb" g aol oqle
Oter' ll,n 5a$"9 Vlor,l has rnqfra*d
l, \e+ lorr R 7a'k 444s o.1 -lhn
fo{n gre[ws rve A Afth t€ ivc l*,u, kTr{/r"t'
a
t
il9tr]' i.nt ,j..'. r
"fiaVl% &p&*'
\Mnd& For*ra-T0 rfner oA.A C++
0&4r gietl l* wctrlr Soch ag *oiles h lae} ll'hnfi
appruK !&rsor{ctors
rllcni * hlqoke.n $rtroo{h
LowRonkfiP 1rry1,,ro; y \rran/
TCItgpho,rr & L\la,l*r,9{ ( egq
2-ot f)Wui
&\t{und } Yug0tnm [P
tzato) &'d lrldnn lhegis Fpe's vseL E'ltan 17'
i
I
I
I
I
I
I
I
II
i
I
d '-.'t'^'"
-rh
PAfiT J[: ]trtu CofNFro*rER Peo&LuM
C"nClar+ Ptotrlo^ deS, u"te
T
t9
&9 6lla.rgL,{I
tIt.\
b=aIn
It
(
I
xldL
$:ltkteiguwl,,e"forges*
(r, i,+ ({/
A Grd) , bh*t)
_o +* A {a"I
Fo. I "t) r.r)
6)L+l_0 +-
*
t
I b I2 dt
= tr( )I0yl ,*: tr*n o $ -n I Jl
L
R rr,a ARt<s 0N T&E Co$N F'rr*rEF ?%B,Lth+
\5n, l,,;t{ a'eeu rn e Jlro+ A and, b
o$e guc;r hed ail tno*)x irrudrees
haol ed *o o bt,l,, 4't* ou{P''* 0+'J '
I+ gee,rnr n'Po gsil e l" *'lve {'t'P
golve ha Corn fl*'* Pi'blPnt
w\&w* & I wY o\er I { t! rl. '
LroF dvai d,orc* R l" o&;*t Surtn
&9 {uff{q( } un/ er^l€eP() scbff}
J. be t) geliess ,
c
2
? Fo' modsrte( ,ler-tea |a+Tc^rmI
Spj-d B"t isfl: I,OTSA,AOA i+
i5 & $;*l-r LlorE*us distuffi t't
d a$ra n &*|.,*a11
i', R hos rc*aroilee.6.d=loo an|,
rruon'* be Lhq* luickwb Ie^ qnJ n"Je
- ui{A "ranJ*" nqfiep+ Th;src
Lrg {^ 6fr4,
{*-_J - .{ *./*-*./-*] _?.. n{@flARr I rss ()EE wITr{ troRr84Hr -7 7
fvon $au{ Wan/ \as use{, &n [r"'h" 17qf wa& l" o\drcoxr, iy slnilners o( g -0"
aramA 25 Eezxe nffit, i+ lws Sl{n?e
dt*bo.ks'o Yng*nvit\ c^rt| debuggtu,g is Vtarc
d4ffi rr(l dun )4 14. lark o{&olo g4,pa*og* Suclt as /rsi, anJ{.edoe,r {uo.li ue surJn As yrrq{*;x todi*rg( n.tt r0tru gums {-) , ele vaa*w;*. divisio^).
(, )., Cs* ]ukr" lA-Z a hvres &s rnoJ^*Nna +0 Cnnvqr+ 0,n p fn oehu l-a
Fo{.h6^ 77.o ferfuq 11 har {e ol &rcha;c ,,les
rnlitt&, o,s Code be{,^*a taluy ns 6 aLn}10
wi 'il^ {&ur I *o 0, /
gpce A" d.nrac{g -
eg?xr Sbqu:r.4 )4
{""t"ia{to
osa+4
tt6
' lhn , Fun*ra* ( ) $unrl:;* ueel
4" tirk br{mn 17 b}n tu;4tL
fr, lo*E ',,r,t) upf€r h,t; t- i q 4h e
nu/kl Wf o-( efg Ut"t0,4+f €yal $ 65.
t iE hasn' { .4a yn-gol q0, e
c
haal larcrk e Lr{ re.Clghas beu ^ wSN h in d-n'attce
h*b Yo+ ;6Ll< ho' 'l*,
bn d,n*{* ndl -bu{ i"{ Uou
fit 5Q+ Na or tYis{$ PQ \
C*a W Colo C/j^ CWh "
(b* Iro{ wsisla^*4 } w,{tro*& {o{ u.t t}ues-
'q
8.9.
S:. I\A PtC EXA M pLE Fu$v{T}#Nr>a!fnegr<1r'bftrtt'+'t!i-*r:r;rr4$f2.,?!.%yui
rr:., ,t 4ta,arhrrttitl+rr'tsrt..i.r r,
!-'*t'
gutr Re.i?* /") xt,),, . \ deti,'e
t-= t
,qt
Ta
L
Su,rn Rec\te ( 5) = I +,, r 1 r ** t = Yrg 2.2.g 23.
In, Q, ure c qn ds Si* E ,
sr;rn Re*ips Vca'R +qnd sb-lor^ vatues oSAr4ufl1od\+9.
fc.ns+ lor (w)fru{orn (sum kitnillf"r po${"}u€ i"' {egcr
N*rro cwe; rl*+ Jdu p'ubW o$ wri*rrte
n arWeA
Re*ipeViaCpp( )
t''on,R fun.{;0,
sa wr fla.iP*
9u rn
lphich usef C++ 0"e hn "pn
S" #ne gum pa*i rn ov€r )he
' fh 4he R*ffIteiuu^ {ae f"
5t*Q^llroO{n
itadpx ,taia\e
Y orkourn +4 ty tsk\ tw achr&ed
. \lt
nc{ icPr a! i'
Ort
Fun"| irn { )
########## R function: sumRecipsViaCpp #########
t. Fo-r obtaining the sum of the reciprocals# of the first "n" integers. This vbrsion# uses Rcpp and C++.
# Last changed = L0 SEp 2019
lppFunction("double sumRecipsViaCpp(int n){double total = 0;for(inti=L;i<-n;++i){ total += t.0/i;)return totat i
)")End of sumRecipsViaCpp #########
,t
wrft Assne'-lrb uha+ [oe5
nx I
9u{y'tRacipsQ= atL=l
oCtrr\Or{e 0\ tt ) a? (
tI
YERCTS E
!. $;{e naMed
surn Sl.ts \ iocPp. *&t Souc CMpc,|er
|a^ a
I lsi n4 .Ff F,,n"{;-( )
Wa I o9 U)
an , K lunrlirn none/.
++ Ittrhol t)s es C eva lua{e
z -f;
P*g*
in {hn safie
(9 i'ri*esqMSqr{sVro Cff( )
tt
oSi
nqIj
L P*uE
L=l inlegU u4s nrl ,n
P
When $ ;n is h pJ -l-esJ- i{.l&" cp(h {vt cn)s
(n B US
?M,, To ltrle CoRN FLotlrER ?UgttM
l,t nlik" 1n s",{"pl" lrns on .lh"
?re uious la5es, J,N c^n Pvbb,
C,'lf€ 9 (iquuqlueg o"nd fiah,.k in YArs iang
frrt trb w€ naed 44n R ?ockaT
A r rndd il{ o
rt{
'?r
Whd^ is &n Add-^ '1, R.PP
W erpp**,s tin u{ aQeLMic
Cnu,p ,rta{.0n9'
A., Rccy o*al [rpy&r* d',{1" solrlL.1,/"n corn tla*w ? mLb*, is ngk+.
########## R function : cornf,lowerViaCpp
t. f?, solving the Cornftower probtem via C++ using# the R packages Rcpp and RcppArmadillo.
# Last changed: t0 SEp 2019
cppFunction("double cornflowerviacpp(armar :mat& A, arma: :vec& b)t// Declare and set all non-input variables:arma::vec eigvals;arma::mat OmegaL = A;int n = b.n_elem;
// larry out the toop over i=L,...'tl// (note C++ array indexing from 0 to n_1):for(inti=0;i<n;i++){
// }btain eigenvalues of symmetrised 0megaL:
eigvals = eig_sym(0.5x(Omegal + 0megaL.t( ) ) ) ;
// Update Omegal matrix:
- Omega1 = (Omega1 + eigvals.max0xb(i)).iO;) - \-"--\"
return trace(0mega1);
)", depends=" RcppArmad itlo,, )
############ End of cornflowerViaCpp #########
Fx eRcrs€,
\N rj{s l*0" h^ nanEAL
Tredian E,rgnV i C
rui{a & C++
hot
/(&r6 raOPl
An$ 91uo* fialtiY A
rl,(he
a )
(h+
D1
rt
h Attu
Ib ol I
U * I th"{he ei qonr/q{ u€S
neJ rQrl
o$ AT
A fpitox, Srnr-E PrAp /pm1ut;o MEr TrruGE
Ktru u 0 F Fr6t,R tD yur t
r (p*ti,4 R ft,,g|;*r tt\Ah C-H,t\ijvl9in {e /orpr
oF
I
{t
$rc e$
( $uYa (a es
t l,r,.l(c^r, Alqrl ydic ymrcl.qfts
ExA F4 pt Ef o f ppl;lteM s-To TRV ro Fr 6u{E aur Saotu:
c N eslnd ,[.unch$ns,I Mo{rix alraebra g t4nbx cw{ cuv hli^r
(ns A*8, AB, A-,g ), Nerlpl fi.J; o*s 1i,.* C++ ,,,6rds)t Ra t'Aoftr h uyr'r I,* lJlnerafi{vt ( ,r c+ + crdq" )0 ( ?ecVa4ing w'&+' f;.pp {qnJ, an$ {u*rr.)
@R*ott 4hn hu, soluel o^ PeLe @ l* fuf)
c r-"'cr)Rxl Fr-owEB FRcBt-FM
fn ,ls A /auc) ,
_o <- A
b /nn 1)
t
f 0r TL , t ,') hUr. * torSes$ eiSeavqlue a{-o' €- (fi, * u)rb. lor;;
end $.or
ib,o$I)\-l)
0u* tt (O,)
the n ex{ Co u [,/* o{ Vry, in {t'l u&.
satn€ new -les{ p*b l_ar*s
S;5"rin4 ot* how besl fu trodtavlssla/ f-nol4*s W\{a Rc pp . . .
lh
TI"46 PBAWN DUMPLTNC PRCBTEM
?to*n burnpt,% P*Llen dnQ; oeds4
&s &llaus'
fn A ia*d) , b (n* I)Uls
5
g <- s xf co, nilatt.uer (h, b)l + 3
( mehia,nk) > * rn in/b) )
C [hu Sin5rlar valueso+A
Cocnf,loruer (5A, # t)
<- Veclar Olotx t )
7g
+
CI, <- Lq cornf lower(zA,-+L)Elee
€n +d a
I
o+ o + rqedion (b),Sihh, tf-rrtn llon'r,- fi+ A,16$ I
!Ou* q* ct
-l-ilE Couftr JtsTER PfrosLeM
-1h e Courl- Jesler Prrf hP dn$v eol ast$
-f,' tlows'
In A (dud) , b hxi)uts ,
a, + 6 coon lls,,ufi, l) +2prown bu *llio1{A,,$).
- 6{*rh I **${a.rer / l,t)11
fu )tu= 1r.-,,3!0+ ol prawnDu*Flfog
0_ <-
Ond'Cor
O €- q-ffiL,corn.fl*.uer&nfi,#t){*a) Arryfi otttQrfAr[)
x fror"n Dor,/ry 0+,L)
lr+ lt
A,.[,Tt€+3h
u**. ^
0rl'
Co uRT JEsre n PPostF/*l ExPuPrg
cot^rl Jesler = t9 + , 2858,. .
To qa+ Jrnts A(\\Wei ha Q)lvrp,,let
\tr*s la 9o[ te,
o 72.6 s ing.rlor vqlqn dewfosi*i Qts
o ?AsL ei6a,r talw f rz, Llan s
a $o 96 ^hah iV i nvcrsicvt s .
g(1BIoI
[; ;],
l8CIrr. ) cour{ Jesl pt(.,.) €ualu&i*is pte{+b &lvtp-h{-i uqll hk,b
pVs
&uc" $-- z,*oz.'.,4 and 6x j b I
RD StEI\4 :
!\jhd &"'1 bps}' wag -1"
tur'sk" Rch - 4fS* $,*
e
9
P*'' barnp?** k/rlLl) vr* Jes* {,{ YYILIP*r
{r\}a* .laru dn{oad&arerf
ChA "Y?on* {.;v iat u$€r da'ft* eA
t\i\f
on 9.
-,tr'----...-.-. ..r-*-,- . ..
-n{t S uu oFPRoBlem rurrf{
R ECr P QocATS
rdN(rIoN trJ €sruNg
={r2,L,,ft
yok Milk(A,b) =
Fo,
lruhex e
9 oh Reergs (n) =
;p (i) =
u {Y1S QC
,
a.'l
TI
Lg)R );p ( i
''/l,rgRec c
E \Ak Mr Lk Pno BLEM
For a d * at rnahk A anJ mx I ver*ur- b
l cocnllonu* (A,t)-tg ls + lcrr,,Tb, er QA,rU)l
+z+ I"s(4t* l.oorrfiot e," fgagt)
NEgTED FuNCTroXig -rEST
Cro bArE)E KAM PITS
S utR il5&
CocnTl uU) Qt
Yok Milk
cou$loutp;
I rawnDunpling
ComDlwu
IrcwnDumplV
Cour*Sasfer
ngPeci p
t I LE9 N [f DeD F-oR Y{E
Sr ytll tsr woR Irrrv G V ERq$od\
F 9un4 Re.ip{rr) WfrL{ rtgRe 'iP(i)N $TED ENE lDE
'/u EA DE F"Frr ETn bflecig.
.r f#include <RcPP. h>/ / t tRcPP: :exPortl J
double mYReciP(int i){
return t'0/ t;) doubte myRecip(int i);
5un* R ec rf9. ct?#include <Rcpp, h>#include "myRecip. h"// [[Rcpp::exportlJdouble sumRecips(int n).{
double totat = 0;double currRecip;
/x Now add the first n reciprocals: x/for(inti=L;i<-n;++i)
,t, {
currReciP = myRecip(i) ;total += currRecip;)return total;
)
EXER CTgE
ryL
DeQnt gurn gq rls (n) = ngx S{,t(r )LJ
l,ohere *g&.+(L) = I t
Cr#" ond eJ.;t *he l; les,rn59gt.cfpTn5 \r*' h
S1r+s. .
9o Jho* gum 51 ,*s dn ) wi$, "[hu, hesl ed
$un.,t rm Mb9{tr (t ) wor}rg in R ui,&"lhn R"tf yckttge bodeA.-[}sl b'r.[h Jhe .C, lJdr"in4 cool et
Sa na PP
+&'83))
SAFE GUARLTTG HERD ER
fzLEs
Evo"' JA,oug\. hovi*lj mg Bec', p, f'-
Co.n{arninq Jus+
19 Sr$-0;c; qatr good C++ {ad in\ tuh;cv, istr,,i.il',,1n rlh e 9qme *o{es h"*
S ile,,trn,
irt'gafun* iT {*c*e &re
Ptt u.t
5 p na'{ice
Ynl,ng 0,,, nd ovs,
di*
rvr g Rnc ip,h Sho,ld, hau" -lhn
Sa(e5,r*d,ut {*rv,;
#ifndef MYRECIP_H#define MYRECIP_Hdouble myRecip(int i);
#endif
\he sJtin3 " MyR gcrP- li .' can b* anb U; n3- f rov idea i+ is ttrtiqtte F b,** -la..,e. cfvplr$r- Lc+TrR\Wiu a€ Mn tur'drn rtqfvre Srdl owel by ,,-l-( u
i9 a Co+nF?on Convqnlion
*gRecip. h (Lnf i)doub{ e
\Rec;e.hTn
(-coNrIAI tJrD )EXF$crs E
EJ i| &r," Itl* ng
0n
m5&r*" h
$oc,,r CotulP ulr* 0(,A fi'Jt& ifSa ,h"t i I ts 5a {e gaaurde| i}", 4hn
SaFe wq, as n5fieci ph c)4 P%n
Trsl Jrho uydald r.ad"e
C0.ft,tnands o*1 pASeu;in 'lAn4
a
Nex ).. ,
Yok Milk?rab{ a,M .
R.pf Arrradilla & $h*
Tue, R PncrAGE; f;"lF P{aspe.,
eeli-k
As o{ tha 4.t\ ry) ng us J&;tno*t R r,o,kqg,
Jas { $ou*#cM,sdiScuS:ion $" Rcp?Plooil*"
toc, ? , e
66v.{arhs dlt
d,etnwed, J" d "+e \Nork 'ng wi ++t o.n R
yoc.Vage has *hn {'llw'wa aduanhges:
ooll *,cff ,and *"h o/tcslorro{
jn Crtta P.torn
o coppilafic^ is J*u Jurin3 f&1tnc f€ahcm ona{ thn lauees 4}*+
h e,1Y utiUr'' gourcecry{ ) cdlls
0.re e liro ;oaI eA
O $nc* Jvt'* Yockope is boJ"d
Inu d4',* *,.m g $"'h ie h4'ha fu&Sro,ad a,J ujQ ctkl
sis^pb Ep* €.s -
> b + Ez lA 1 p rawn DrrrnplrnS(A, b)
I l"lU Co*n P*r fv trNTS oF
A' pf Plos pe'
R, ?(..fr cons isls o $ J+rn *(l*t'{tq
L+ us€r- eii{ el li {es,
a pOnrrl
bEScRr PrlortNAMUE PAceCrrrnQ/ ower,. ttcotrrl Jgslsr. .Flp
prown DumP lmg.c?P
yok M;lk,cpp
Sun Recips. cpp
nngBec;p..fp
Surn S{.k .c1pm5s{r+. crf,
cornflcx" e.r.h
tn6Rec'tp,h
n gJq r1. hp na,, 0,, npli,tn h
an/ e cprnp,{e, ga)(xa{e} I tn, :
R. pp Expor*s R
R. Pf Exp*r*s, CPP
cumPi{* A{ltibu{ast)
\una,o,u***"*t3-*lll*jh' Sr"{'*
!{Ur,,,t -fo Bt'ILb Arun
IN srA t-i- R. fp P Iu3 Pe n
oR orHeR ftcPP - SePm;Dti\JT
PA cKAGES
At' /u*\ **o Ltrags d''*
v Via Uxt Ix shetl
( rne'U'uo{ ug,e}
AU*lrN O-(
Q skdio fienus
-Scr ip
b5dJ"eS e
{q
llr,{eE)
g
Frw A l- bI9Coss }rtvtuhqt-e Jtne Poi ^l( aoa urhq+
QuEst[anr€:o ( ?"\n +€rs' ?
&rc "pain*ers" 0nEwqge)
LN} OF
NsrE s
S flRrNG 2o{3
L.\& ree noles Ma4 have gorne
aJ.dili6ns "'rn ,Aufurn 2-o%)