All,
I have been playing with the R math standalone library in C++. I quite like being able to generate random numbers and use distribution functions that I am comfortable with from R. My question is: is it possible to use the matrix operations (multiplication, transpose, inverse, Chol, etc) that are available in R in a standalone library? I don't see them in Rmath.h.
If matrix operations are available to use in standalone C++ code, it seems that the R API becomes the perfect open source computational engine. Are people using it in this fashion?
Thanks so much for your guidance and suggestions!
Most, if not all, of the things you mention are provided by LAPACK or BLAS Fortran code that R links to, not something R provides new code for itself.
If you are interested in using C++ with R, look at the Rcpp package by Dirk Eddelbuettel and Romain Francois. Dirk has also written the RcppArmadillo package as an interface to the Armadillo C++ linear algebra library that can do the matrix operations you mention.
Whether this is of use will depend on whether you are wanting to write separate C++ code that is accessed outside R, or interfacing C++ code that you access within R. Rcpp facilitates (greatly) the latter. Take a look at Armadillo directly if it is the former situation.
I think the original questions starts at the wrong end. There is no C++ in R, and no C++ API in R itself.
So if you want to access R matrix functions, you have to go through the R API -- which is a C layer and very much not C++.
Gavin in his answer (and comments) and Josh are spot-on: You probably want something like Armadillo for high-level matrix algebra. Armadillo (just like related libraries) eventually goes to BLAS and LAPACK --- as does R. [ Doug Bates usually reminds us that there is one important difference related to pivoted decompositions; this is touched upon in the fastLm() implementation and example in RcppArmadillo. ]
Lastly, Rcpp can help with both cases:
whether you want to extend R by calling C++ code you wrote, where Rcpp makes it easy to pass objects back and forth, or
whether you want to embed R inside your C++ application using RInside as Rcpp once again provides the glue
The rcpp-devel list is a good place for more in-depth discussions and examples.
Related
I want to perform multinomial logistic regression [using glm()] and do some graphs [gplot()] in C++.
I'm not able to write a function for this kind of regression in C++, but maybe is possible to open R and call glm() function in order to performe it inside of C++, is it? how? I would appreciate any idea.
Thanks in advance!
Yes you can via the RInside project which provides you with a (very simple) class R which instantiates the R interpreter. You can then pass commands as (possibly multi-line) strings and evaluate via the REPL.
There are numerous examples in the package itself, and on different blog posts. As it uses Rcpp extensively, it is also covered in one chapter of the Rcpp book.
I am currently working on a project in C++, and I am actually interested in using Matlab data structures, instead of having to create my own data types (such as matrices, arrays, etc.)
Is there a way to seamlessly use Matlab objects in C++? I don't mind having to run Matlab in the background while my program runs.
EDIT: A starting point is this: http://www.mathworks.co.uk/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html. I will continue reading this.
You can use instead Armadillo C++ maths library; used by NASA, Boeing, Siemens, Deutsche Bank, MIT, CMU, Stanford, etc.
They have good documentation and examples if you are more familiar with MATLAB/OCTAVE
http://arma.sourceforge.net/docs.html#syntax
I would prefer using native C++ library of some sort and not Matlab. This is likely to be faster for both development and execution.
From writing C++ extensions for Matlab I learned one thing: Using Matlab objects in C++ is likely to give you considerable headache.
Matlab data structures are not exposed as C++ classes. Instead, you get pointers that you can manipulate with C-like API functions.
I recommend to use a native C++ library such as Eigen3.
The functionality you are looking at is not really intended to be used as seamless objects. In the past when I have used it I found it much simpler to do the C parts using either native arrays or a third party matrix library and then convert it into a Matlab matrix to return.
Mixing Matlab and C++ is typically done in one of two ways:
Having a C++ program call Matlab to do some specialist processing. This is chiefly useful for rapid development of complex matrix algorithms. You can do this either by calling the full Matlab engine, or by packaging you snippet of Matlab code into a shared library for distribution. (The distributed version packages a distributable copy of the Matlab runtime which is called with your scripts).
Having a Matlab script call a C++ function to do some specialist processing. This is often used to embed C++ implementations of algorithms (such as machine learning models) or to handle specific optimizations.
Both of these use cases have some overhead transferring the data to/from Matlab.
If you are simply looking for some matrix code to use in C++ you would be better off looking into the various C++ matrix libraries, such as the one implemented in Boost.
You can do mixed programming with C++ and Matlab. There are two possible ways:
Call MATLAB Engine directly: Refer to this post for more info. Matlab will run in the background.
Distribute MATLAB into independent shared library: check out here on how to do this (with detail steps and example).
I can't find any documentation about how to use the "forecast" R package from C++. Is anything available online?
I have not looked closely but as it depends on the CRAN packages Rcpp and RcppArmadillo (which I am involved and which facilitate C++ use from R), I'd say yes. You can probably poke around in the sources and find other entry points. It's open source, after all.
But if you asked for a well-defined pre-existing API for forecast, the answer may be no.
Edit: Oh, and the obvious plug for RInside which allows you to embed [a single instance of] R very easily inside your C++ application. It does this by wrapping the existing (but hard to use) embedding API in a much simpler to use C++ class which abstracts away all the handholding.
I would like to port the SpecialK Poker Hand evaluator to R. I think this should be relatively easy using the Rcpp package, but I have no idea where to begin.
The existing tutorials seem to focus on developing new C++ code for use in R. In this case, I have C++ that solves a problem, and I want to use this code in R with minimum hassle. If the code were one, self-contained function, I could compile and link it on the fly with inline, but this doesn't work here.
I guess this question has 2 parts:
Will I need to make any changes to the C++ source to make it compatible with Rcpp?
How do I call this code from R, given that it's not a small,
self-contained function I can compile and link dynamically using
inline?
I am also open to using the Java, python, or objective-C versions of the evaluator, but I don't think those will be easier to link to R.
Have you looked at Rcpp which makes it fairly painfree to combine R and C++?
Lots of packages use it to bring existing C++ code to R. You can also look at questions here under the [rcpp] tag. Fairly extensive documentation in the package, at my site and other places.
I have a certain code written in Matlab. I wanted to obtain similar results using a c++ code. I wanted to know whether a c++ library exists for matrix manipulations? My current Matlab code consists of matrix multiplications,inverse and complements.
There are dozens of linear algebra libraries for C++. Here is another one not mentioned in the other links so far:
http://www.alglib.net/
I have used Newmat in a similar situation.
Not sure if this would help you, or your company/university have licenses for it but matlabcoder can export your matlab code to c++ without much extra work.
http://www.mathworks.com/products/matlab-coder/