RcppNumerical: Rcpp Integration for Numerical Computing Libraries

Yixuan Qiu



Rcpp is a powerful tool to write fast C++ code to speed up R programs. However, it is not easy, or at least not straightforward, to compute numerical integration or do optimization using pure C++ code inside Rcpp.

RcppNumerical integrates a number of open source numerical computing libraries into Rcpp, so that users can call convenient functions to accomplish such tasks.

  • To use RcppNumerical with Rcpp::sourceCpp(), add

in the C++ source file. - To use RcppNumerical in your package, add Imports: RcppNumerical and LinkingTo: Rcpp, RcppEigen, RcppNumerical to the DESCRIPTION file, and import(RcppNumerical) to the NAMESPACE file.

Numerical Integration


The one-dimensional numerical integration code contained in RcppNumerical is based on the NumericalIntegration library developed by Sreekumar Thaithara Balan, Mark Sauder, and Matt Beall.

To compute integration of a function, first define a functor derived from the Func class (under the namespace Numer):

The first function evaluates one point at a time, and the second version overwrites each point in the array by the corresponding function values. Only the second function will be used by the integration code, but usually it is easier to implement the first one.

RcppNumerical provides a wrapper function for the NumericalIntegration library with the following interface:

  • f: The functor of integrand.
  • lower, upper: Limits of integral.
  • err_est: Estimate of the error (output).
  • err_code: Error code (output). See inst/include/integration/Integrator.h Line 676-704.
  • subdiv: Maximum number of subintervals.
  • eps_abs, eps_rel: Absolute and relative tolerance.
  • rule: Integration rule. Possible values are GaussKronrod{15, 21, 31, 41, 51, 61, 71, 81, 91, 101, 121, 201}. Rules with larger values have better accuracy, but may involve more function calls.
  • Return value: The final estimate of the integral.

See a full example below, which can be compiled using the Rcpp::sourceCpp function in Rcpp.

Runing the integrate_test() function in R gives


Multi-dimensional integration in RcppNumerical is done by the Cuba library developed by Thomas Hahn.

To calculate the integration of a multivariate function, one needs to define a functor that inherits from the MFunc class:

Here Constvec represents a read-only vector with the definition

(Basically you can treat Constvec as a const Eigen::VectorXd. Using Eigen::Ref is mainly to avoid memory copy. See the explanation here.)

The function provided by RcppNumerical for multi-dimensional integration is

  • f: The functor of integrand.
  • lower, upper: Limits of integral. Both are vectors of the same dimension of f.
  • err_est: Estimate of the error (output).
  • err_code: Error code (output). Non-zero values indicate failure of convergence.
  • maxeval: Maximum number of function evaluations.
  • eps_abs, eps_rel: Absolute and relative tolerance.
  • Return value: The final estimate of the integral.

See the example below:

We can test the result in R:

Numerical Optimization

Currently RcppNumerical contains the L-BFGS algorithm for unconstrained minimization problems based on the LBFGS++ library.

Again, one needs to first define a functor to represent the multivariate function to be minimized.

Same as the case in multi-dimensional integration, Constvec represents a read-only vector and Refvec a writable vector. Their definitions are

The f_grad() member function returns the function value on vector x, and overwrites grad by the gradient.

The wrapper function for LBFGS++ is

  • f: The function to be minimized.
  • x: In: the initial guess. Out: best value of variables found.
  • fx_opt: Out: Function value on the output x.
  • maxit: Maximum number of iterations.
  • eps_f: Algorithm stops if |f_{k+1} - f_k| < eps_f * |f_k|.
  • eps_g: Algorithm stops if ||g|| < eps_g * max(1, ||x||).
  • Return value: Error code. Negative values indicate errors.

Below is an example that illustrates the optimization of the Rosenbrock function f(x1, x2) = 100 * (x2 - x1^2)^2 + (1 - x1)^2:

Calling the generated R function optim_test() gives

A More Practical Example

It may be more meaningful to look at a real application of the RcppNumerical package. Below is an example to fit logistic regression using the L-BFGS algorithm. It also demonstrates the performance of the library.

Here is the R code to test the function:

It is much faster than the standard glm.fit() function in R! (Although glm.fit() calculates some other quantities besides beta.)

RcppNumerical also provides the fastLR() function to run fast logistic regression, which is a modified and more stable version of the code above.