[Rd] lbfgsb from C/C++

Dirk Eddelbuettel edd at debian.org
Sun Sep 7 16:46:07 CEST 2014


On 7 September 2014 at 12:30, axionator wrote:
| I would like to call R's lbfgsb function from my C/C++ code by including
| R_ext/Applic.h and linking against libR.
| Currently, I am allocating memory for x (and the other input arrays for
| lbfgsb) in my C/C++ code via malloc/new. However, this gives a segmentation
| fault when executing the program.
| I tried to allocate x via PROTECT(x = NEW_NUMERIC(n)); x_p =
| NUMERIC_POINTER(x);.
| This compiles but also gives a segmentation fault.
| Is there a way to use lbfgsb from C/C++ directly (without an intermediate
| call of R)? Did I miss any compile flags?

R is built to provide a 'language and environment', not a callable library.

There is a however an optional callable library with a (much smaller) subset
of functionality, see the section '6.16 Using these functions in your own C
code' in the 'Writing R Extension' manual.  However, the library does /not/
contain the bounded BFGS implementation you are seeking.

So you may want to look at another (open source) optmization library. NLopt
by Steven Johnson (http://ab-initio.mit.edu/wiki/index.php/NLopt) is decent
and easy to use; if you need LBFGSB there is also Jorge Nocedal's site
(http://www.ece.northwestern.edu/~nocedal/lbfgsb.html) as well as much more.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org



More information about the R-devel mailing list