Movatterモバイル変換


[0]ホーム

URL:


Dirk Eddelbuettel

RcppGSL: Rcpp Integration for GNU GSL Vectors and Matrices

Build StatusLicenseCRANDebian packageDownloadsLast Commit

RcppGSL provides an interface from R to the vector and matrix classes of theGNU GSL, a collection of numerical routines for scientifc computing.

It is particularly useful for C and C++ programs as it provides a standard C interface to a wide range of mathematical routines such as special functions, permutations, combinations, fast fourier transforms, eigensystems, random numbers, quadrature, random distributions, quasi-random sequences, Monte Carlo integration, N-tuples, differential equations, simulated annealing, numerical differentiation, interpolation, series acceleration, Chebyshev approximations, root-finding, discrete Hankel transforms physical constants, basis splines and wavelets. There are over 1000 functions in total with an extensive test suite.

The RcppGSL package provides an easy-to-use interface between GSL data structures andGNU R using concepts fromRcpp which is itself a package that eases the interfaces between R and C++.

What can RcppGSL do?

The RcppGSL package does a few things, in particular for vectors and matrices:

  • templated vector and matrix classes: these are similar to Rcpp’s own vector and matrix classes, but really are just smart pointers around the C structure expected by the library
  • this means you can transfer data from R to your GSL-using programs in pretty much the same way you would in other C++ programs using Rcpp—by relying on theRcpp::as() andRcpp::wrap() converterrs which are most often invoked automatically for you anyway
  • at the C++ level you can use these GSL vectors in a more C++-alike way (using egfoo[i] to access an element at index i)
  • yet at the same time you can just pass these vector and matrix objects to the GSL functions expecting its C objects: thanks to some cleverness in these classes they pass the right object on (see the example below)
  • we also provide the lightweightviews for vectors and matrices as the GSL API uses these in many places.

In sum, these features aim to make it a lot easier to use functionality from the (excellent)GNU GSL in a way that is more natural to a user of C++ and R (and of courseRcpp) Thepackage vignette. has a more details.

So give me an example!

Here is a simple implementation of a column norm (which we could easily compute directly in R, but we are simply re-using an example fromSection 8.4.14 of the GSL manual).

#include<RcppGSL.h>#include<gsl/gsl_matrix.h>#include<gsl/gsl_blas.h>// [[Rcpp::export]]Rcpp::NumericVector colNorm(const RcppGSL::Matrix & G) {int k = G.ncol();    Rcpp::NumericVector n(k);// to store resultsfor (int j =0; j < k; j++) {        RcppGSL::VectorView colview = gsl_matrix_const_column (G, j);        n[j] = gsl_blas_dnrm2(colview);    }return n;// return vector}

This example shows how aRcppGSL::Matrix object (atypedef shortcut to the templateddouble representation) gets passed down from R; we can use standardconst & interface as well. Inside the function we then read the number of columns from a member functionncol() (using the same name as in R). We assign a standardRcpp vector of the corresponding sizek to hold the result which is returned to R.

To compute the column norms, we loop over all columns. The norm is computed after we extract a column as aconst view (see the GSL manual for details: this is essentially a lightweight slice referring back to the original object). This column can be passed directly to a GSL function expecting a vector – very nice. The actual computation is done by a standard GSL function, heregsl_blas_dnrm2, which expects a standard GSL vector – which our classes transparently provide in this context.

Previous versions then had to free the memory of the matrix object; this is now done automagically. By using Rcpp Attributes, all this is wrapped in atry/catch block as is standard withRcpp.

Another example function for a faster compiled implementation oflm() using the model fitting code in the GSL is also included in the package.

Where do I get it

RcppGSL is aCRAN package, lives otherwise in its own habitat atGitHub and can also be downloaded from the localRcpp archive which also has a copy of thevignette describing the package.

Authors

RcppGSL is being written by Dirk Eddelbuettel and Romain Francois usingRcpp.

License

RcppGSL is licensed under the GNU GPL version 2 or later.

Initially created: Thu Mar 11 11:14:31 CST 2010
Last modified: Fri Jul 31 16:12:54 CDT 2020


[8]
ページ先頭

©2009-2025 Movatter.jp