|
Rcpp Version 0.9.10
|
00001 #!/usr/bin/r -t 00002 # 00003 # Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois 00004 # 00005 # This file is part of Rcpp. 00006 # 00007 # Rcpp is free software: you can redistribute it and/or modify it 00008 # under the terms of the GNU General Public License as published by 00009 # the Free Software Foundation, either version 2 of the License, or 00010 # (at your option) any later version. 00011 # 00012 # Rcpp is distributed in the hope that it will be useful, but 00013 # WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU General Public License 00018 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00019 00020 suppressMessages(library(Rcpp)) 00021 suppressMessages(library(inline)) 00022 00023 firstExample <- function() { 00024 ## a really simple C program calling three functions from the GSL 00025 gslrng <- ' 00026 gsl_rng *r; 00027 gsl_rng_env_setup(); 00028 double v; 00029 00030 r = gsl_rng_alloc (gsl_rng_default); 00031 00032 printf("generator type: %s\\n", gsl_rng_name (r)); 00033 printf("seed = %lu\\n", gsl_rng_default_seed); 00034 v = gsl_rng_get (r); 00035 printf("first value = %.0f\\n", v); 00036 00037 gsl_rng_free(r); 00038 return R_NilValue; 00039 ' 00040 00041 ## turn into a function that R can call 00042 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway 00043 funx <- cfunction(signature(), gslrng, 00044 includes="#include <gsl/gsl_rng.h>", 00045 Rcpp=FALSE, 00046 cppargs="-I/usr/include", 00047 libargs="-lgsl -lgslcblas") 00048 00049 cat("Calling first example\n") 00050 funx() 00051 invisible(NULL) 00052 } 00053 00054 secondExample <- function() { 00055 00056 ## now use Rcpp to pass down a parameter for the seed 00057 gslrng <- ' 00058 int seed = Rcpp::as<int>(par) ; 00059 00060 gsl_rng *r; 00061 gsl_rng_env_setup(); 00062 double v; 00063 00064 r = gsl_rng_alloc (gsl_rng_default); 00065 00066 gsl_rng_set (r, (unsigned long) seed); 00067 v = gsl_rng_get (r); 00068 00069 #ifndef BeSilent 00070 printf("generator type: %s\\n", gsl_rng_name (r)); 00071 printf("seed = %d\\n", seed); 00072 printf("first value = %.0f\\n", v); 00073 #endif 00074 00075 gsl_rng_free(r); 00076 return Rcpp::wrap(v) ; 00077 ' 00078 00079 ## turn into a function that R can call 00080 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway 00081 ## use additional define for compile to suppress output 00082 funx <- cfunction(signature(par="numeric"), gslrng, 00083 includes="#include <gsl/gsl_rng.h>", 00084 Rcpp=TRUE, 00085 cppargs="-I/usr/include", 00086 libargs="-lgsl -lgslcblas") 00087 cat("\n\nCalling second example without -DBeSilent set\n") 00088 print(funx(0)) 00089 00090 funx <- cfunction(signature(par="numeric"), gslrng, 00091 includes="#include <gsl/gsl_rng.h>", 00092 Rcpp=TRUE, 00093 cppargs="-I/usr/include -DBeSilent", 00094 libargs="-lgsl -lgslcblas") 00095 cat("\n\nCalling second example with -DBeSilent set\n") 00096 print(funx(0)) 00097 00098 invisible(NULL) 00099 } 00100 00101 thirdExample <- function() { 00102 00103 ## now use Rcpp to pass down a parameter for the seed, and a vector size 00104 gslrng <- ' 00105 int seed = Rcpp::as<int>(s) ; 00106 int len = Rcpp::as<int>(n); 00107 00108 gsl_rng *r; 00109 gsl_rng_env_setup(); 00110 std::vector<double> v(len); 00111 00112 r = gsl_rng_alloc (gsl_rng_default); 00113 00114 gsl_rng_set (r, (unsigned long) seed); 00115 for (int i=0; i<len; i++) { 00116 v[i] = gsl_rng_get (r); 00117 } 00118 gsl_rng_free(r); 00119 00120 return Rcpp::wrap(v) ; 00121 ' 00122 00123 ## turn into a function that R can call 00124 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway 00125 ## use additional define for compile to suppress output 00126 funx <- cfunction(signature(s="numeric", n="numeric"), 00127 gslrng, 00128 includes="#include <gsl/gsl_rng.h>", 00129 Rcpp=TRUE, 00130 cppargs="-I/usr/include", 00131 libargs="-lgsl -lgslcblas") 00132 cat("\n\nCalling third example with seed and length\n") 00133 print(funx(0, 5)) 00134 00135 invisible(NULL) 00136 } 00137 00138 fourthExample <- function() { 00139 00140 ## now use Rcpp to pass down a parameter for the seed, and a vector size 00141 gslrng <- ' 00142 int seed = Rcpp::as<int>(s); 00143 int len = Rcpp::as<int>(n); 00144 00145 gsl_rng *r; 00146 gsl_rng_env_setup(); 00147 std::vector<double> v(len); 00148 00149 r = gsl_rng_alloc (gsl_rng_default); 00150 00151 gsl_rng_set (r, (unsigned long) seed); 00152 for (int i=0; i<len; i++) { 00153 v[i] = gsl_rng_get (r); 00154 } 00155 gsl_rng_free(r); 00156 00157 return wrap(v); 00158 ' 00159 00160 ## turn into a function that R can call 00161 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway 00162 ## use additional define for compile to suppress output 00163 funx <- cfunction(signature(s="numeric", n="numeric"), 00164 gslrng, 00165 includes=c("#include <gsl/gsl_rng.h>", 00166 "using namespace Rcpp;", 00167 "using namespace std;"), 00168 Rcpp=TRUE, 00169 cppargs="-I/usr/include", 00170 libargs="-lgsl -lgslcblas") 00171 cat("\n\nCalling fourth example with seed, length and namespaces\n") 00172 print(funx(0, 5)) 00173 00174 invisible(NULL) 00175 } 00176 00177 firstExample() 00178 secondExample() 00179 thirdExample() 00180 fourthExample()