将cpp函数转换为在r中使用
Converting a cpp function to use in r
我有以下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画廊。我们有九个小插曲。我们有两个关于矩阵的包。我们有我的书。我们有哈德利的文章。帮自己一个忙,去读一些吧。
相关文章:
- 内置函数可查看CPP中的成员变量
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 我需要知道编译器如何在cpp中使用析构函数
- 使用cpp中的结构和函数的多个学生条目
- 从 c 或 cpp 系列子函数返回到主函数
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- 共享对象中的符号名称与文件中.cpp函数不同
- 从 python ctypes 调用 CPP 函数
- CPP 函数无法获得正确的输出
- 如何从C文件中调用Cpp函数
- cpp函数来检索各种数据类型的值
- 从 Python 调用 cpp 函数时出现分段错误
- RcppEigen - 从内联到包中的.cpp函数和"Map"
- 如何在将函数作为参数传递给文件中.cpp函数模板时声明
- 正则表达式:获取 CPP 函数 (c#) 的单独参数和参数类型
- "not in this scope" cpp 函数调用中的错误
- 是否有任何cpp函数或对象(不包括从c继承的)不是线程安全的,即使每个线程对自己的数据进行操作
- 将cpp函数转换为在r中使用
- 如何操作cpp函数