1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
## ----setup, include=FALSE-----------------------
knitr::opts_chunk$set(cache=TRUE)
library(Rcpp)
library(inline)
options("width"=50, digits=5)
## ---- eval=FALSE--------------------------------
# vignette("Rcpp-jss-2011")
# vignette("Rcpp-introduction")
# vignette("Rcpp-attributes")
## ---- eval=FALSE--------------------------------
# vignette("Rcpp-package")
## -----------------------------------------------
fx <- cxxfunction(signature(x = "numeric"),
'NumericVector xx(x);
return wrap(
std::accumulate(xx.begin(),
xx.end(),
0.0)
);',
plugin = "Rcpp")
res <- fx(seq(1, 10, by=0.5))
res
## ---- eval=FALSE--------------------------------
# fx <- cxxfunction(signature(),
# paste(readLines("myfile.cpp"),
# collapse="\n"),
# plugin = "Rcpp")
## -----------------------------------------------
cppFunction('double accu(NumericVector x) {
return(
std::accumulate(x.begin(), x.end(), 0.0)
);
}')
res <- accu(seq(1, 10, by=0.5))
res
## -----------------------------------------------
inc <- 'template <typename T>
class square :
public std::unary_function<T,T> {
public:
T operator()( T t) const {
return t*t;
}
};
'
src <- '
double x = Rcpp::as<double>(xs);
int i = Rcpp::as<int>(is);
square<double> sqdbl;
square<int> sqint;
return Rcpp::DataFrame::create(
Rcpp::Named("x", sqdbl(x)),
Rcpp::Named("i", sqint(i)));
'
fun <- cxxfunction(signature(xs="numeric",
is="integer"),
body=src, include=inc,
plugin="Rcpp")
fun(2.2, 3L)
## ---- eval = FALSE------------------------------
# lines = '// copy the data to armadillo structures
# arma::colvec x = Rcpp::as<arma::colvec> (x_);
# arma::mat Y = Rcpp::as<arma::mat>(Y_) ;
# arma::colvec z = Rcpp::as<arma::colvec>(z_) ;
#
# // calculate the result
# double result = arma::as_scalar(
# arma::trans(x) * arma::inv(Y) * z
# );
#
# // return it to R
# return Rcpp::wrap( result );'
#
# writeLines(a, file = "myfile.cpp")
## ---- eval = FALSE------------------------------
# fx <- cxxfunction(signature(x_="numeric",
# Y_="matrix",
# z_="numeric" ),
# paste(readLines("myfile.cpp"),
# collapse="\n"),
# plugin="RcppArmadillo" )
# fx(1:4, diag(4), 1:4)
## -----------------------------------------------
fx <- cxxfunction(signature(),
'RNGScope();
return rnorm(5, 0, 100);',
plugin="Rcpp")
set.seed(42)
fx()
fx()
## -----------------------------------------------
cppFunction('Rcpp::NumericVector ff(int n) {
return rnorm(n, 0, 100); }')
set.seed(42)
ff(5)
ff(5)
set.seed(42)
rnorm(5, 0, 100)
rnorm(5, 0, 100)
## -----------------------------------------------
src <- 'Rcpp::NumericVector v(4);
v[0] = R_NegInf; // -Inf
v[1] = NA_REAL; // NA
v[2] = R_PosInf; // Inf
v[3] = 42; // c.f. Hitchhiker Guide
return Rcpp::wrap(v);'
fun <- cxxfunction(signature(), src, plugin="Rcpp")
fun()
## ---- eval = FALSE------------------------------
# txt <- 'arma::mat Am = Rcpp::as< arma::mat >(A);
# arma::mat Bm = Rcpp::as< arma::mat >(B);
# return Rcpp::wrap( Am * Bm );'
# mmult <- cxxfunction(signature(A="numeric",
# B="numeric"),
# body=txt,
# plugin="RcppArmadillo")
# A <- matrix(1:9, 3, 3)
# B <- matrix(9:1, 3, 3)
# C <- mmult(A, B)
# C
## ---- eval = FALSE------------------------------
# # simple example of seeding RNG and
# # drawing one random number
# gslrng <- '
# int seed = Rcpp::as<int>(par) ;
# gsl_rng_env_setup();
# gsl_rng *r = gsl_rng_alloc (gsl_rng_default);
# gsl_rng_set (r, (unsigned long) seed);
# double v = gsl_rng_get (r);
# gsl_rng_free(r);
# return Rcpp::wrap(v);'
#
# plug <- Rcpp::Rcpp.plugin.maker(
# include.before = "#include <gsl/gsl_rng.h>",
# libs = paste(
# "-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp",
# "-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib",
# "-L/usr/lib -lgsl -lgslcblas -lm")
# )
# registerPlugin("gslDemo", plug )
# fun <- cxxfunction(signature(par="numeric"),
# gslrng, plugin="gslDemo")
# fun(0)
## ---- eval=FALSE--------------------------------
# myplugin <- getPlugin("Rcpp")
# myplugin$env$PKG_CXXFLAGS <- "-std=c++11"
# f <- cxxfunction(signature(),
# settings = myplugin, body = '
# // fails without -std=c++0x
# std::vector<double> x = { 1.0, 2.0, 3.0 };
# return Rcpp::wrap(x);
# ')
# f()
## ---- eval = FALSE------------------------------
# src <- '
# Rcpp::NumericMatrix x(2,2);
# x.fill(42); // or another value
# Rcpp::List dimnms = // list with 2 vecs
# Rcpp::List::create( // with static names
# Rcpp::CharacterVector::create("cc", "dd"),
# Rcpp::CharacterVector::create("ee", "ff")
# );
# // and assign it
# x.attr("dimnames") = dimnms;
# return(x);
# '
# fun <- cxxfunction(signature(),
# body=src, plugin="Rcpp")
# fun()
## ---- eval = FALSE------------------------------
# BigInts <- cxxfunction(signature(),
# 'std::vector<long> bigints;
# bigints.push_back(12345678901234567LL);
# bigints.push_back(12345678901234568LL);
# Rprintf("Difference of %ld\\n",
# 12345678901234568LL - 12345678901234567LL);
# return wrap(bigints);',
# plugin="Rcpp", includes="#include <vector>")
#
# retval <- BigInts()
#
# # Unique 64-bit integers were cast to identical
# # lower precision numerics behind my back with
# # no warnings or errors whatsoever. Error.
#
# stopifnot(length(unique(retval)) == 2)
## -----------------------------------------------
a <- 1.5:4.5
b <- 1.5:4.5
implicit_ref(a)
a
explicit_ref(b)
b
## -----------------------------------------------
a <- 1:5
b <- 1:5
class(a)
int_vec_type(a)
a # variable a changed as a side effect
num_vec_type(b)
b # b unchanged as copy was made for numeric
## -----------------------------------------------
x <- 1:10 # an integer sequence
# returning an altered value
const_override_ex(x)
# but the original value is altered too!
x
## -----------------------------------------------
vec_scalar_assign(5L, 3.14)
## -----------------------------------------------
mat_scalar_assign(2L, 3.0)
## -----------------------------------------------
test_long_vector_support()
## -----------------------------------------------
set.seed(123)
(X <- sample(c(LETTERS[1:5], letters[1:6]), 11))
preferred_sort(X)
stl_sort(X)
## -----------------------------------------------
x <- c("B", "b", "c", "A", "a")
sort(x)
rcpp_sort(x)
|