我想在我的C程序中使用stats
包中的nlminb
函数。我知道有几种不同的方法可以做到这一点:在C代码中调用R函数,使用Rcpp
。
但是函数nlminb
基本上是用Fortran写的,用C代码封装。你可以在R-3.2.1/src/library/stats/src
文件portsrc.f
, port.c
和port.h
中找到这些代码。
那么有一种方法可以在port.c
甚至portsrc.f
中直接在我的C代码中调用函数吗?
你有使用nlminb
吗?请注意,optim
函数所做的大部分事情与nlminb
相同,其入口点在"编写R扩展"中有文档说明。手册。
接口(定义在报头R_ext/application .h)是
Nelder米德:
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);
bfg:
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);