[R] passing a matrix to C++ with .C()?

Thomas Lumley tlumley at u.washington.edu
Tue May 7 17:19:47 CEST 2002


On Mon, 6 May 2002, Francisco J Molina wrote:

> It seems that it is possible to pass a matrix to C++ by using .C()
>
> The R part of the interface should be something like:
>
> ('C++function', matrix, numberRows, numberColumns )
>
> ...I guess.
>
> The C++ part of the interface could be something like
>
> void C++( double matrix[][], int numberRows, numberColumns )

No, it would be
   void C++(double matrix[], int *numberRows, int *numberColumns

You get passed an R matrix, which (like a Fortran matrix) is just a single
vector formed by concatenating the columns of the matrix.

The simplest way to address the (i,j) element is then
   matrix[i+(*numberRows-1)*j]
or you could go and set up a vector of pointers into the matrix, one for
each row, so you could write
   vector_of_pointers[i][j]
I don't think this is a particularly good idea, but you can see it in
action in the survival package. The file src/dmatrix.c does the setup.


Or you could use .Call, which enables you to handle all sorts of R
objects.

	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list