是否可以直接在我的C代码中调用用C编写的R源代码函数?

时间:2015-08-07 07:09:11

标签: c r fortran

我想在我的C程序中使用nlminb包中的函数stats。我知道几种不同的方法:使用Rcpp在C代码中调用R函数。

但是函数nlminb基本上是用Fortran编写的,并且用C代码包装。可以在R-3.2.1/src/library/stats/src个文件portsrc.fport.cport.h中找到此代码。

那么有没有办法直接在我的C代码中调用port.c甚至portsrc.f中的函数?

1 个答案:

答案 0 :(得分:3)

使用nlminb吗?请注意,optim函数与nlminb执行大部分相同的操作,其入口点记录在"写入R扩展"手册。

  

接口(在标题R_ext / Applic.h中定义)是

     

Nelder Mead:

void nmmin(int n, double *xin, double *x, double *Fmin, optimfn fn,
int *fail, double abstol, double intol, void *ex,
double alpha, double beta, double gamma, int trace,
int *fncount, int maxit);
  

BFGS:

void vmmin(int n, double *x, double *Fmin,
optimfn fn, optimgr gr, int maxit, int trace,
int *mask, double abstol, double reltol, int nREPORT,
void *ex, int *fncount, int *grcount, int *fail);
  

共轭渐变:

void cgmin(int n, double *xin, double *x, double *Fmin,
optimfn fn, optimgr gr, int *fail, double abstol,
double intol, void *ex, int type, int trace,
int *fncount, int *grcount, int maxit);
  

带有边界的有限内存BFGS:

void lbfgsb(int n, int lmm, double *x, double *lower,
double *upper, int *nbd, double *Fmin, optimfn fn,
optimgr gr, int *fail, void *ex, double factr,
double pgtol, int *fncount, int *grcount,
int maxit, char *msg, int trace, int nREPORT);
  

模拟退火:

void samin(int n, double *x, double *Fmin, optimfn fn, int maxit,
int tmax, double temp, int trace, void *ex);