s-news
[Top] [All Lists]

Re: call c++ from splus

To: <s-news@lists.biostat.wustl.edu>, "Tony Smith" <quickcur@yahoo.com>
Subject: Re: call c++ from splus
From: "Horace Tso" <Horace.Tso@pgn.com>
Date: Tue, 13 Dec 2005 15:16:03 -0800
Tony,
 
I'm not sure how efficient it is, but here is a way of passing a matrix/dataframe. First, you create a list in Splus with as many elements as no. of columns in your dataframe/matrix. Then you read back the list in C by first getting the length of the list, which tells you the no. of pointers you need, then read off the size of the first vector in the list to get the no. of rows.
 
Hope this helps.
 
Horace W. Tso
PGE
Portland, Oregon
 
=======================================================
s_object *ReadDF(s_object *sDF ) {
 
 S_EVALUATOR
 s_object **sLptr;
 s_object *sL;
 double **cData;
 long i=0, j=0, n=0, n_col=0, n_row=0;
 
 //Cast input and find length of the list
 sLptr = LIST_POINTER(sDF);
 n_col = GET_LENGTH(sDF);
 
 // Just get the row number from the first column of the list
 n_row = GET_LENGTH(sLptr[0]);
 
 // Allocate with Salloc (for some reason S_alloc gives type cast error)
 cData = Salloc(n_col, double*);
 for(i=0; i < n_col; i++) {
  cData[i] = NUMERIC_POINTER(sLptr[i]);
 }
 
// Now you have your matrix......
}

======================================================
 

>>> Tony Smith <quickcur@yahoo.com> 12/13/05 2:48 PM >>>
I was trying to call c++ function from splus. My goal
is to pass a two dimensional array to c++ code, do
some computation and return. But I am sure how to pass
two dimensional array to splus. The example shown
bellow is for one dimensional data. I do not want to
use CSPmatrix class since it will be slower to access
the data in c++.


c++
void spC(double* pdX, double* pdY, long* plLen)
{
        S_EVALUATOR


  // TODO: Replace the example code below with your
own code.


        //Validate the input arguments
        if((pdX == NULL) || (pdY == NULL) || plLen ==
NULL)
                PROBLEM "Invalid input" ERROR;


        //Perform element by element operation
        for(long n=0; n< *plLen; n++)
                pdY[n] = pdX[n][n] * pdX[n][n]; //The
output = the input squared


        return;



}


splus
"spC" <-
## TODO: Help documentation
function(x)
{
        ## TODO: Modify the codes below
        ## Example of .C() with two arguments
        ## See sp.cxx for the C/C++ (VC++)
implementation of spC()

        len = length(x)
        y = .C("spC",
                                as.double(x),
                                y=double(len),
                                len
                )$y
        y




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news
<Prev in Thread] Current Thread [Next in Thread>