我对C很新,但知道我的方式[R]。这个错误可能是C中一个非常愚蠢的错误。我的C代码确实内核平滑。在[R]函数中捕获了段错误
* 当我注释掉代码的最后一行时,我的函数有效:results [i] = v; *
此呼叫杀死R:
new.y < -zsmooth2(X = C(0:80000),xpts = DAT $ V2,ypts = DAT $ V4,H = 10000)
*捉住段错误* 地址0x1184f8000,原因 '存储器未被映射'
回溯: 1:.C( “kernel_smooth”,as.double(X),as.double(YP ts),as.double(xpts),as.integer(n),as.integer(nxpts),as.double(h),result = double(length(xpts))) 2:zsmooth2(x = c 0:80000),xpts = DAT $ V2,ypts = DAT $ V4,H = 10000)
C代码:
#include <R.h>
#include <Rmath.h>
#include <stdio.h>
void kernel_smooth(double *x, double *ypts, double *xpts, int *n, int *nxpts, double *h, double *results){
int i, j;
for(i = 0; i < *n; i++){
double nsum = 0;
double dsum = 0;
double z = x[i] + *h;
double y = x[i] - *h;
for(j = 0; j < *nxpts; j++){
if(xpts[j] < y){
continue;
}
if(xpts[j] > z){
break;
}
double d = (xpts[j] - i)/*h;
double r = dnorm(d, 0, 1, 0);
nsum += r * ypts[j];
dsum += r;
}
Rprintf("test:i %d\n", i);
double v = nsum/dsum;
Rprintf("test:v %f\n", v);
results[i] = v;
}
}
R-代码:
dyn.load("~/github/ZevRTricks/smoother1.so")
zsmooth2<-function(x, ypts, xpts, h){
n <- length(x)
nxpts <- length(xpts)
dens <- .C("kernel_smooth", as.double(x), as.double(ypts),
as.double(xpts), as.integer(n), as.integer(nxpts),
as.double(h), result = double(length(xpts)))
dens[["result"]]
}
你有C代码张贴在C#论坛中.. – MethodMan 2012-02-28 18:00:59
C和C#是两种完全不同的语言。你不只是交替使用他们的名字。 – BoltClock 2012-02-28 18:04:55
据我所知,你不能轻易地使用C#与R所以代码真的C? – 2012-02-28 18:22:00