From ripley at stats.ox.ac.uk  Thu Jun 17 19:18:17 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu Jun 17 19:18:26 2004
Subject: [Rd] using "= matrix (...)" in .C calls
In-Reply-To: <x2r7sew345.fsf@biostat.ku.dk>
Message-ID: <Pine.LNX.4.44.0406171814310.20880-100000@gannet.stats>

It is worth noting that anyone writing rpart today (or in the last 5 
years) would have used .Call and allocated in C code.  But rpart goes back 
much further than that.  Further, had it been written for S-PLUS's .Call, 
it might never have got ported to R (and certainly not when it was).

On 17 Jun 2004, Peter Dalgaard wrote:

> 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.

And with DUP=TRUE I don't think there is much choice.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] using "= matrix (...)" in .C calls
Prof Brian Ripley