[Rd] The best way to use R's linear algebra functions from C
Daniel Oberski
daniel.oberski at gmail.com
Sat May 12 20:48:20 CEST 2007
First of all, thanks to everybody for R.
I have written a program in C that requires some basic matrix
operations, namely the following:
- matrix multiplication and addition,
- determinant and transpose,
- inverse of a symmetric matrix.
Since I am a happy R user I thought I could call R from within my
program for this purpose. I read "Writing R Extensions" and "R
Internals" and a bunch of other things from the web, ran the
"Embedded" tests, and grep'ed / looked at the source code.
It is now my understanding that there are several ways I could use R from C:
1. Pass data from C to an R function, loaded during R runtime;
2. Build R statements and use Rparse.h to execute them;
3. Use Rembedded.h to call R's C functions;
4. Directly call R's C functions.
4) would be the fastest (I think?), and speed is some concern.
However, I am finding it very tough going to discover how to implement
this. For example: given two matrices represented as arrays of doubles
and their numbers of rows and columns in C, which function in the R
code should I call to multiply them (matprod and co. are not
exported)? Which headers should I include? Should I link against a
library, and if so, which one?
Currently I am leaning towards a very roundabout solution: copy the
double arrays into SEXPs, use Rembedded.h and load the required
formulas from a .R file. This solution seems rather hacky and slow to
me, but it is the only one I can get to work right now. However, I
would rather not use it.
In short, my question is: what is the proper or preferred method of
solving this problem, and how do I find out how to apply it?
Thank you very much for your attention.
Best regards,
Daniel Oberski
University of Tilburg / European Social Survey
daniel.oberski at gmail.com
More information about the R-devel
mailing list