3 # Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois
5 # This file is part of Rcpp.
7 # Rcpp is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
12 # Rcpp is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20 suppressMessages(library(Rcpp))
21 suppressMessages(library(RcppGSL))
23 ## NOTE: This is the old way to compile Rcpp code inline.
24 ## The code here has left as a historical artifact and tribute to the old way.
25 ## Please use the code under the "new" inline compilation section.
27 suppressMessages(library(inline))
29 firstExample_old <- function() {
30 ## a really simple C program calling three functions from the GSL
36 r = gsl_rng_alloc (gsl_rng_default);
38 printf(" generator type: %s\\n", gsl_rng_name (r));
39 printf(" seed = %lu\\n", gsl_rng_default_seed);
41 printf(" first value = %.0f\\n", v);
47 ## turn into a function that R can call
48 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
49 funx_old <- cxxfunction(signature(), gslrng,
50 includes="#include <gsl/gsl_rng.h>",
53 cat("Calling first example\n")
58 secondExample_old <- function() {
60 ## now use Rcpp to pass down a parameter for the seed
62 int seed = Rcpp::as<int>(par) ;
68 r = gsl_rng_alloc (gsl_rng_default);
70 gsl_rng_set (r, (unsigned long) seed);
74 printf(" generator type: %s\\n", gsl_rng_name (r));
75 printf(" seed = %d\\n", seed);
76 printf(" first value = %.0f\\n", v);
80 return Rcpp::wrap(v) ;
83 ## turn into a function that R can call
84 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
85 ## use additional define for compile to suppress output
86 funx_old <- cxxfunction(signature(par="numeric"), gslrng,
87 includes="#include <gsl/gsl_rng.h>",
89 cat("\n\nCalling second example without -DBeSilent set\n")
93 ## now override settings to add -D flag
94 settings <- getPlugin("RcppGSL")
95 settings$env$PKG_CPPFLAGS <- paste(settings$PKG_CPPFLAGS, "-DBeSilent")
97 funx_old <- cxxfunction(signature(par="numeric"), gslrng,
98 includes="#include <gsl/gsl_rng.h>",
100 cat("\n\nCalling second example with -DBeSilent set\n")
106 thirdExample_old <- function() {
108 ## now use Rcpp to pass down a parameter for the seed, and a vector size
110 int seed = Rcpp::as<int>(s) ;
111 int len = Rcpp::as<int>(n);
115 std::vector<double> v(len);
117 r = gsl_rng_alloc (gsl_rng_default);
119 gsl_rng_set (r, (unsigned long) seed);
120 for (int i=0; i<len; i++) {
121 v[i] = gsl_rng_get (r);
125 return Rcpp::wrap(v) ;
128 ## turn into a function that R can call
129 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
130 ## use additional define for compile to suppress output
131 funx_old <- cxxfunction(signature(s="numeric", n="numeric"),
133 includes="#include <gsl/gsl_rng.h>",
135 cat("\n\nCalling third example with seed and length\n")
136 print(funx_old(0, 5))
141 fourthExample_old <- function() {
143 ## now use Rcpp to pass down a parameter for the seed, and a vector size
145 int seed = Rcpp::as<int>(s);
146 int len = Rcpp::as<int>(n);
150 std::vector<double> v(len);
152 r = gsl_rng_alloc (gsl_rng_default);
154 gsl_rng_set (r, (unsigned long) seed);
155 for (int i=0; i<len; i++) {
156 v[i] = gsl_rng_get (r);
163 ## turn into a function that R can call
164 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
165 ## use additional define for compile to suppress output
166 funx_old <- cxxfunction(signature(s="numeric", n="numeric"),
168 includes=c("#include <gsl/gsl_rng.h>",
169 "using namespace Rcpp;",
170 "using namespace std;"),
172 cat("\n\nCalling fourth example with seed, length and namespaces\n")
173 print(funx_old(0, 5))
178 ## NOTE: Within this section, the new way to compile Rcpp code inline has been
179 ## written. Please use the code next as a template for your own project.
181 firstExample <- function() {
182 ## a really simple C program calling three functions from the GSL
186 #include <gsl/gsl_rng.h>
188 // [[Rcpp::depends(RcppGSL)]]
196 r = gsl_rng_alloc (gsl_rng_default);
198 printf(" generator type: %s\\n", gsl_rng_name (r));
199 printf(" seed = %lu\\n", gsl_rng_default_seed);
201 printf(" first value = %.0f\\n", v);
207 cat("Calling first example\n")
212 secondExample <- function() {
214 ## now use Rcpp to pass down a parameter for the seed
216 ## turn into a function that R can call
217 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
218 ## use additional define for compile to suppress output
222 #include <gsl/gsl_rng.h>
224 // [[Rcpp::depends(RcppGSL)]]
227 double funx(int seed){
233 r = gsl_rng_alloc (gsl_rng_default);
235 gsl_rng_set (r, (unsigned long) seed);
239 printf(" generator type: %s\\n", gsl_rng_name (r));
240 printf(" seed = %d\\n", seed);
241 printf(" first value = %.0f\\n", v);
248 sourceCpp(code=gslrng, rebuild = TRUE)
250 cat("\n\nCalling second example without -DBeSilent set\n")
254 ## now override settings to add -D flag
255 o = Sys.getenv("PKG_CPPFLAGS")
256 Sys.setenv("PKG_CPPFLAGS" = paste(o, "-DBeSilent"))
258 sourceCpp(code=gslrng, rebuild = TRUE)
260 # Restore environment flags
261 Sys.setenv("PKG_CPPFLAGS" = o )
263 cat("\n\nCalling second example with -DBeSilent set\n")
269 thirdExample <- function() {
271 ## now use Rcpp to pass down a parameter for the seed, and a vector size
273 ## turn into a function that R can call
274 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
275 ## use additional define for compile to suppress output
279 #include <gsl/gsl_rng.h>
281 // [[Rcpp::depends(RcppGSL)]]
284 std::vector<double> funx(int seed, int len){
288 std::vector<double> v(len);
290 r = gsl_rng_alloc (gsl_rng_default);
292 gsl_rng_set (r, (unsigned long) seed);
293 for (int i=0; i<len; i++) {
294 v[i] = gsl_rng_get (r);
301 cat("\n\nCalling third example with seed and length\n")
307 fourthExample <- function() {
309 ## now use Rcpp to pass down a parameter for the seed, and a vector size
311 ## turn into a function that R can call
312 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
313 ## use additional define for compile to suppress output
317 #include <gsl/gsl_rng.h>
319 using namespace Rcpp;
322 // [[Rcpp::depends(RcppGSL)]]
325 std::vector<double> funx(int seed, int len){
329 std::vector<double> v(len);
331 r = gsl_rng_alloc (gsl_rng_default);
333 gsl_rng_set (r, (unsigned long) seed);
334 for (int i=0; i<len; i++) {
335 v[i] = gsl_rng_get (r);
342 cat("\n\nCalling fourth example with seed, length and namespaces\n")