<font color="red">This page is tested only in R version newer than 2.4</font> --- - <font color="#800000"><b>Reference</b></font> - GSL -- <a href="http://www.gnu.org/software/gsl/" target="_blank">GNU Scientific Library</a>. - For MS Windows, the GSL has binaries and setup for MinGW, and it can be installed as MinGW's library. See <a href="http://gnuwin32.sourceforge.net/packages/gsl.htm" target="_blank"> GnuWin32</a> packages for the detail. (my mirror <a href="./source/gsl/gsl-1.8.exe" target="_blank">here</a> for GSL version 1.8). - <font color="#800000"><b>Call GSL</b></font> First, create a C code file "<a href="./example/R_gsl/gsltools.c" target="_blank">gsltools.c</a>" contains the following, it will list all possible permutations for a given integer n (*nrow) by calling the GSL functions. See the GSL document for the detail. ``` /* File name: gstools.c For Linux, SHELL> gcc -c gsltools.c; gcc -shared -o gsltools.so gsltools.o -lgsl -lgslcblas For MS Windows, SHELL> gcc -c gsltools.c SHELL> gcc -shared -o gsltools.dll gsltools.o -lgsl -lgslcblas */ #include &lt;gsl/gsl_permutation.h&gt; int allpermu(int *nrow, int *all){ gsl_permutation *p; int i, j; p = gsl_permutation_alloc(*nrow); gsl_permutation_init(p); i = 0; do{ for(j = 0; j < *nrow; j++){ *(all + i + j) = (int) gsl_permutation_get(p, (size_t) j); } i += *nrow; } while(gsl_permutation_next(p) == GSL_SUCCESS); gsl_permutation_free (p); return 0; } ``` Create an R code file "<a href="./example/R_gsl/callgsl.r" target="_blank">callgsl.r</a>" contains this ``` # File name: callgsl.r dyn.load("/usr/lib/libgslcblas.so", local = FALSE, now = FALSE) dyn.load("/usr/lib/libgsl.so", local = FALSE, now = FALSE) dyn.load("gsltools.so") ### For MS Windows, they will be like these # dyn.load("C:/PROGRA~1/RTOOLS/MINGW/BIN/LIBGSLCBLAS.DLL") # dyn.load("C:/PROGRA~1/RTOOLS/MINGW/BIN/LIBGSL.DLL") # dyn.load("gsltools.dll") allpermu <- function(n){ # if(n > 10) stop("allpermu: n <= 10") ncol <- as.integer(factorial(n)) nrow <- as.integer(n) all <- vector(mode = "integer", length = nrow * ncol) ret <- .C("allpermu", nrow, all)[[2]] matrix(ret, nrow = nrow) + 1 } allpermu(3) dyn.unload("gsltools.so") dyn.unload("/usr/lib/libgsl.so") dyn.unload("/usr/lib/libgslcblas.so") ### For MS Windows, they will be like these # dyn.unload("gsltools.dll") # dyn.unload("C:/PROGRA~1/RTOOLS/MINGW/BIN/LIBGSL.DLL") # dyn.unload("C:/PROGRA~1/RTOOLS/MINGW/BIN/LIBGSLCBLAS.DLL") ``` For MS Windows, the compiled gstools.dll can be donloaded at <a href="./example/R_gsl/gsltools.dll" target="_blank">here</a>. - <font color="#800000"><b>Output</b></font> Each column contains one possible permutation. For n = 3, there are 3! = 6 possible outcomes. ``` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 2 2 3 3 [2,] 2 3 1 3 1 2 [3,] 3 2 3 1 2 1 ``` - <font color="#800000"><b>Conclusion</b></font> - Call by address in R. - Use a column-wise data structure in R and Fortran. - Use a row-wise data structure in C. - Use pointers in C to catch the objects' address which are passed from R. - Use dyn.load() to load the GSL in R. --- <div w3-include-html="../preamble_tail_date.html"></div>