|
楼主

楼主 |
发表于 2013-1-19 11:35:40
|
只看该作者
如何在SAS里调用带函数指针(function pointer)的C函数
各位高人,很抱歉向你们提一个算不得有趣或有益的问题。
想从SAS里面使用一些用C写的算法,但由于本人见识浅薄,不知道如何引用带函数指针的C函数,感觉处处受限。
比如很简单的定积分,在C里可以声明成
[code:50dadx8r]
double integral(double f(double x), double lower, double upper);
[/code:50dadx8r]
这样可以适用用于一般的一元函数。
但假如要在SAS里使用C来积分,我只能假设被积分的函数是某种特殊类型,然后还要把被积分函数在C里面定义好。
就比如下面的代码里我假设被积分函数是不超过2次的多项式。
我还查阅了早期用MODULEN/MODULEC/CALL MODULE和SASCBTBL Attribute Table来调用外部函数的例子,也不得其解,故此相求!
[code:50dadx8r]
proc proto package=work.funcs.test;
double fn(double x, const double *A);
double integral(const double *A, double lower, double upper);
externc fn;
double fn(double x, const double *A){
return (A[0]*x*x + A[1]*x + A[2]);
}
externcend;
externc integral;
double integral(const double *A, double lower, double upper){
int i;
double sigma;
double delta;
delta = (upper-lower)/10000;
sigma = 0;
for (i=0;i<10000;i++){
sigma += delta*(fn(lower+i*delta, A) + fn(lower+(i+1)*delta, A))/2;
}
return sigma;
}
externcend;
run;
proc fcmp inlib=work.funcs outlib=work.funcs.test;
function sas_integral(A[*], lower, upper);
return (integral(A, lower, upper));
endsub;
run;
options cmplib=work.funcs;
data _null_;
array _A[3] _temporary_ (1, 0, 0);
y = sas_integral(_A, 0, 1);
put 'the integral of x^2 over [0, 1] = ' y:best6.;
array _B[3] _temporary_ (0, 2, 0);
y = sas_integral(_B, 0, 2);
put 'the integral of 2x over [0, 2] = ' y:best6.;
run;
[/code:50dadx8r] |
|