0

I am running R 2.15.2 and Rcpp 10.4 (upgraded a few days ago) on RedHat.

When I call the qgamma function in my C++ program compiled via make, it returns 0, but qnorm in the same program returns the correct value (source to follow.) When I call it via a cppFunction compile, I get the right answer.

C++ source:

#include <iostream>
using namespace std;

#include <Rcpp.h>

int main() {
  cout << R::qnorm(0.3, 1.0, 1.0, 1, 0) << endl;
  cout << R::qgamma(0.3, 5.0, 5.0, 1, 0) << endl;
  return 0;
}

and the associated compile messages and execution:

[jbowma1@smartrepl-app00 src]$ make test
g++ -I/usr/include/R -I/usr/lib64/R/library/Rcpp/include -L/usr/lib64/R/lib -lR  -L/usr/lib64/R/library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/lib64/R/library/Rcpp/lib  -o test test.cpp
[jbowma1@smartrepl-app00 src]$ ./test
0.475599
0
[jbowma1@smartrepl-app00 src]$ 

The problem of course being that "0" out in the second line of the output above.

Now for the cppFunction version:

> foosrc <- "double foo() {return R::qgamma(0.3, 5.0, 5.0, 1, 0);}"
> bar <- cppFunction(foosrc)
> bar()
[1] 18.16805

(Same computer, naturally, this one executed in RStudio.)

Note that if just calling qgamma from the R prompt, the third parameter needs to be inverted (=0.2 in this example) to get the same answer.

I'd suspect I'd messed up my makefile, which is actually largely copied from the RcppExamples directory, if it were not for the fact that qnorm works. dgamma also works, but pgamma does not. Other distributions, e.g., the negative binomial, also work.

Any suggestions would be greatly appreciated!

4

1 回答 1

1

你的第一个例子“闻起来很有趣”。您根本无法使用随机构建,main()因为Rcpp.hRcpp.h意味着我们加载到 R 中的附加组件提供了它的 main

现在,您可以使用

  1. RInside 可以让你嵌入 R,然后你就可以使用Rcpp.hRcpp 的其余部分了
  2. 或者,您可以使用RmathR 提供的独立数学库(可r-mathlib通过我的包装作为 Debian/Ubuntu 的软件包使用)。

您的第二个示例有效,因为您在这里使用了正确的上下文。第一个没有。从这个意义上说,您的比较是关闭的。

编辑下面是一个我一直在闲逛的示例程序qbeta。这里没有 Rcpp,只是在编写 R 扩展中记录的 R 的 Mathlib 的外部使用:

// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; 
//           compile-command: "gcc -s -Wall -O3 -I/usr/share/R/include 
//                             -o rmath_qbeta rmath_qbeta.c -lRmath -lm" -*-

#include <stdio.h>

#define MATHLIB_STANDALONE 1
#include <Rmath.h>

int main(void) {

    double x = 0.25, a = 0.8, b = 2.0;

    printf("qbeta: %f %f %f %f\n", 
           qbeta(x, a, b, 0, 0), 
           qbeta(log(x), a, b, 0, 1), 
           qbeta(x, a, b, 1, 0), 
           qbeta(log(x), a, b, 1, 1));

    return 0;
}

包含/链接路径适用于 Debian/Ubuntu;注释的前三行是从单行缩进的(这是供 Emacs 使用的)。

于 2013-09-28T21:39:01.220 回答