有关详细信息,请参阅“编写 R 扩展”手册,您可以轻松地编译和链接 Rmath.h 和独立的 R Math 库 - 但不是 R.h。 (您可以通过 Rcpp / RInside 使用,但这是一个不同的故事。)
有许多使用 libRmath 的示例,其中之一就在手册本身中。这是我在 Debian 软件包中提供的一个r-mathlib
包含这个独立的数学库:
/* copyright header omitted here for brevity */
#define MATHLIB_STANDALONE 1
#include <Rmath.h>
#include <stdio.h>
typedef enum {
BUGGY_KINDERMAN_RAMAGE,
AHRENS_DIETER,
BOX_MULLER,
USER_NORM,
INVERSION,
KINDERMAN_RAMAGE
} N01type;
int
main(int argc, char** argv)
{
/* something to force the library to be included */
qnorm(0.7, 0.0, 1.0, 0, 0);
printf("*** loaded '%s'\n", argv[0]);
set_seed(123, 456);
N01_kind = AHRENS_DIETER;
printf("one normal %f\n", norm_rand());
set_seed(123, 456);
N01_kind = BOX_MULLER;
printf("normal via BM %f\n", norm_rand());
return 0;
}
在 Linux 上,您只需像这样构建(因为我将库和标头放置在包中的标准位置;在 OS X 上根据需要添加 -I 和 -L)
/tmp $ cp -vax /usr/share/doc/r-mathlib/examples/test.c mathlibtest.c
`/usr/share/doc/r-mathlib/examples/test.c' -> `mathlibtest.c'
/tmp $ gcc -o mathlibtest mathlibtest.c -lRmath -lm
/tmp $ ./mathlibtest
*** loaded '/tmp/mathlibtest'
one normal 1.119638
normal via BM -1.734578
/tmp $