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 ) but C++ requires to indicate the number of columns, that is, instead of matrix[][], it should be double matrix[][ numberColumns ]... This does not look like the right thing to do though. Can anyone help me with this? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Take a look at the ".Call" documentation in the "writing R extensions" manual. If you mean passing an R matrix to C++, then you will have to pass the SEXP structure containing the matrix into your C++ code and manipulate it with internal R functions. If you mean passing a C++ matrix into R, you will have to wrap the pointer and return it from your C++ code. Note that R will have no idea what to do with the pointer unless you provide appropriate functions/methods to manipulate it in R. (I believe the "Matrix" package does just that.) Tim On Tue, 2002-05-07 at 01:05, 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 ) > > but C++ requires to indicate the number of columns, that is, instead of > matrix[][], it should be double matrix[][ numberColumns ]... > > This does not look like the right thing to do though. > > Can anyone help me with this? > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._