clayton.springer@pharma.novartis.com
2004-Jun-17 17:45 UTC
[Rd] using "= matrix (...)" in .C calls
Dear R-devel, I am trying to alter rpart so that it makes additional calculations when growing the tree. In the "rpart.s" there is a call to the C routine: rp <- .C("s_to_rp2", as.integer(nobs), as.integer(nsplit), as.integer(nodes), as.integer(ncat), as.integer(cats *!isord), as.integer(max(cats)), as.integer(xval), which = integer(nobs), cptable = matrix(double(numcp*cpcol), nrow=cpcol), dsplit = matrix(double(1), nsplit,3), isplit = matrix(integer(1), nsplit,3), csplit = catmat, dnode = matrix(double(1), nodes, 3+numresp), inode = matrix(integer(1), nodes, 6), PACKAGE = "rpart") Which in communicates with the C code in "s_to_rp.c" void s_to_rp2(Sint *n, Sint *nsplit, Sint *nnode, Sint *ncat, Sint *numcat, Sint *maxcat, Sint *xvals, Sint *which, double *cptable, double *dsplit, Sint *isplit, Sint *csplit, double *dnode, Sint *inode) Apparently the lines like: dsplit = matrix(double(1), nsplit,3), Cause C arrays to be pulled over into an R matrix. However I can't figure out the syntax from context nor can I find documentation. I have an array which was created and exists in the "C" part of the code, but I can not figure out how to pull it over to the "R" side. The array was ALLOCed as 1-D array (of size nodes * variables), and ultimately I would like to get into matrix of nodes * variables. Any help or advice would be appreciated. thanks in advance, Clayton [[alternative HTML version deleted]]
Try looking at subsection "4.2 Interface functions .C and .Fortran" in the manual "Writing R Extensions". (hint: it's done in a simple way that should be obvious after you've read this.) You're far more likely to get a helpful answer if you indicate in your posting that you've read this and indicate how it did not answer the questions you have. Also, this type of basic question is more appropriate for R-help, not R-devel. hope this helps, Tony Plate At Thursday 09:43 AM 6/17/2004, you wrote:>Dear R-devel, > >I am trying to alter rpart so that it makes additional calculations when >growing the tree. > >In the "rpart.s" there is a call to the C routine: > > rp <- .C("s_to_rp2", > as.integer(nobs), > as.integer(nsplit), > as.integer(nodes), > as.integer(ncat), > as.integer(cats *!isord), > as.integer(max(cats)), > as.integer(xval), > which = integer(nobs), > cptable = matrix(double(numcp*cpcol), nrow=cpcol), > dsplit = matrix(double(1), nsplit,3), > isplit = matrix(integer(1), nsplit,3), > csplit = catmat, > dnode = matrix(double(1), nodes, 3+numresp), > inode = matrix(integer(1), nodes, 6), > PACKAGE = "rpart") > >Which in communicates with the C code in "s_to_rp.c" > >void s_to_rp2(Sint *n, Sint *nsplit, Sint *nnode, Sint >*ncat, > Sint *numcat, Sint *maxcat, Sint *xvals, Sint >*which, > double *cptable, double *dsplit, Sint *isplit, Sint >*csplit, > double *dnode, Sint *inode) > > >Apparently the lines like: > > dsplit = matrix(double(1), nsplit,3), > >Cause C arrays to be pulled over into an R matrix. However I can't figure >out the syntax from context nor can I find documentation. > >I have an array which was created and exists in the "C" part of the code, >but I can not figure out how to pull it over to the "R" side. > >The array was ALLOCed as 1-D array (of size nodes * variables), and >ultimately I would like to get into matrix of nodes * variables. > >Any help or advice would be appreciated. > >thanks in advance, > >Clayton > > [[alternative HTML version deleted]] > >______________________________________________ >R-devel@stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
clayton.springer@pharma.novartis.com writes:> Apparently the lines like: > > dsplit = matrix(double(1), nsplit,3), > > Cause C arrays to be pulled over into an R matrix. However I can't figure > out the syntax from context nor can I find documentation.Actually no. It *creates* an R matrix (nsplit x 3) and then passes the block of numeric data as a 1d array of nsplit. Coming back from C this will still be an R matrix but possibly with new values inside. help(matrix) should tell you the details. The double(1) is really just a silly way of writing 0.0 (it specifies a double precision vector of length 1, and the value will default to 0); matrix() will automagically replicate it to fill the matrix.> I have an array which was created and exists in the "C" part of the code, > but I can not figure out how to pull it over to the "R" side. > > The array was ALLOCed as 1-D array (of size nodes * variables), and > ultimately I would like to get into matrix of nodes * variables. > > Any help or advice would be appreciated.You cannot pull, only push, when dealing with .C (I suspect that's not quite true but it's a close approximation). The canonical way is to dimension the array on the R side and pass it as an argument to the C side. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907