Amirhossein
Amirhossein

Reputation: 37

Converting a cpp function to use in r

I have following cpp function and want to write it in R

I use rcpp package to compile and use that but some error occurred

Indeed I have problem in using pointer in R

      void creationPI(double *distC, int mC, int tailleC, double *PIC,int aC)
    //distC: distribution de X ; mC= smax-smin+1 ie u+v+1; tailleC=a+1; PIC la matrice PI comme resultat
    {

    double *f;//=NULL;  /*f(k)=P[X<=k]*/
    int t1,k,b,c,l;
    int top_un,top_deux;
      //FILE *matrix;

    t1=2*(aC-1)+1;   // taille du vecteur des f ca va de 1-a à a-1 ; k va de [0 à 2*(a-1)]


    /* ALLOCATION DES MATRICES*/
      //if (!(f = (double *)calloc(t1, sizeof(double))))
         //exit(ALLOC_ERROR);
    f = (double *)calloc(t1, sizeof(double));


    /* CREATION DES f */
      if ((aC-1)<=u) top_un=aC-1; else top_un=u;    //top_un=min
      if ((aC-1)>=u) top_deux=aC-1; else top_deux=u;//top_deux=max
     /*On a 0<->1-a donc f[k]=P[X<=k+1-a] ou encore P[X<=l]=f[l-1+a]*/
      if (aC>v+1) 
                { //remplir le premier f
          for (k=1-aC; k<smin; k++) {b=k-1+aC; *(f+b)=0;} // on passe dans cette boucle si a> v+1
                  //on remplit la suite jusqu'a min(u,a-1): P[X<=k+1-a]=P[X<=k-a]+P[X=k+1-a]
                  //on remplit donc soit jusqu'a la fin de f soit jusqu'au premier 1 car on utiliser tous les distC
                 for (k=smin; k<top_un+1 ; k++) {b=k-1+aC; *(f+b)=*(f+b-1)+*(distC +k-smin);}
                 //On gere le bout droit, quand il reste des f non remplis : 
                if (aC-1>u) {for (k=top_un+1; k<aC-1+1 ; k++) {b=k-1+aC; *(f+b)=1;}}
                }
       else // on a aC<=v+1
                { // on remplit le premier f
                *f=0;
                for (k=smin; k<1-aC+1 ;k++) 
                {b=k-smin; *f=*f+ *(distC+b);}
                // la suite : P[X<=k+1-a]=P[X<=k-a]+P[X=k+1-a], remarque identique a la precedente
                for (k=1-aC+1;k<top_un+1; k++)
                {b=k+aC-1; *(f+b)=*(f+b-1)+ *(distC+v+k);}//dernier +1 cf <
                //On gere le bout droit, quand il reste des f non remplis : 
                if (aC-1>u) {for (k=top_un+1; k<aC-1+1 ; k++) {b=k-1+aC; *(f+b)=1;}}
                }

 /*Creation de la matrice PI*/
 /*PIC[a][a]=1*/
 *(PIC + (tailleC * aC) + aC)=1;
 for (k=0; k<aC; k++) 
    {
    b=aC-k-1;
    *(PIC +(tailleC * k))=*(f+b); 
    c=(2*aC)-k-2;                                   /* Pi[0,k]=f(-k)*/
    *(PIC +(tailleC *k) +aC)=1- *(f+c);          /*Pi[a,l]=1-f(a-k-1)*/
    for (l=1; l<aC; l++)
      {
       b=l-k-smin;
       if(b>=0 && b<mC) *(PIC+(tailleC *k)+l)=*(distC + b);
                         /*Pi[k,l]=P[X=l-k]*/
        else  *(PIC+(tailleC *k)+l)=0.0;
       }
    *(PIC+(tailleC *aC)+k)=0.0;
     }

 free(f);

}//fin proc creationPI

Can I use Rcpp to run this function in R?

How to encountered with pointer in R?

Thanks

Upvotes: 0

Views: 89

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

You are writing 1990s style C code, with explicit pointer arithmetic and what not, and hoping that by just mentioning Rcpp things will magically work.

Sorry to disappoint you on that front. However, Rcpp will allow you to

  • declare an n by k matrix using the dimensions: Rcpp::NumericMatrix M(n, k); is one way
  • access elements of that matrix at elements i and j just as you should M(i, j) = 42.0;
  • return it to R as a matrix with preserved dimensions

We now have well over 1000 questions on Rcpp here, including many on matrices. We have the Rcpp Gallery. We have nine vignettes. We have two packages focused on matrices. We have my book. We have Hadley's writeups. Do yourself a favour and go read some of them.

Upvotes: 5

Related Questions