将cpp函数转换为在r中使用

Converting a cpp function to use in r

本文关键字:cpp 函数 转换      更新时间:2023-10-16

我有以下cpp函数,并想写在R

我使用rcpp包来编译和使用,但是出现了一些错误

在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

我可以使用Rcpp在R中运行这个函数吗?

如何在R中遇到指针?

谢谢

你正在写90年代风格的C代码,带有显式的指针算术等等,并希望只要提到Rcpp事情就会神奇地工作。

很抱歉在这方面让你失望了。然而,Rcpp将允许您
  • 用维度声明一个n × k矩阵:Rcpp::NumericMatrix M(n, k);是单向的
  • 在元素i和j处访问该矩阵的元素,就像你应该做的那样M(i, j) = 42.0;
  • 将其作为保留维度的矩阵返回给R

我们现在有超过1000个关于Rcpp的问题,包括许多关于矩阵的问题。我们有Rcpp画廊。我们有九个小插曲。我们有两个关于矩阵的包。我们有我的书。我们有哈德利的文章。帮自己一个忙,去读一些吧。