RcppArmadillo中的负和正无穷大

时间:2014-05-09 04:59:43

标签: r rcpp

我试图访问在0.2.16(http://cran.r-project.org/web/packages/RcppArmadillo/news.html)中引入RcppArmadillo的math :: inf()函数。符号与犰狳不同。犰狳(http://arma.sourceforge.net/docs.html#constants)将无穷大称为:

datum::inf();

我收到的错误是:"'数学'尚未宣布。"

我试图将数学声明为标题无效。

如果我将其他值替换为无穷大(即2或1),则代码可以正常工作。但是,我需要能够代表无限。

以下是我的代码:

#include <RcppArmadillo.h>
#include <math.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
// [[Rcpp::export]]
void lovetest(arma::mat Y,arma::mat thetatilde){
  int n = Y.n_rows;
  arma::vec lb(n);
  arma::vec ub(n);
  for(int i = 0; i<n;i++){
    #equivalent to R command: subset(thetatilde[i,],Y[i,]==1)
    arma::vec Y1 = thetatilde.elem(find(Y.row(i) == 1));
    arma::vec Y0 = thetatilde.elem(find(Y.row(i) == 0));
    Rcpp::Rcout << "max(Y1) = " << max(Y1) << std::endl;
    Rcpp::Rcout << "min(Y1) = " << min(Y0) << std::endl;
    if(Y1.n_elem >0){
      lb(i) = max(Y1);
    }else{
      lb(i) = -1*math::inf(); //problem line due to math
    }
    if(Y0.n_elem >0){
      ub(i) = min(Y0);
    }else{
      ub(i) = math::inf(); //problem line due to math
    } 
  }
}

1 个答案:

答案 0 :(得分:3)

Clang很明显:

test.cpp:20:18: error: use of undeclared identifier 'math'; did you mean 'arma::math'?
      lb(i) = -1*math::inf(); //problem line due to math
                 ^~~~
                 arma::math
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/include/armadillo_bits/constants_compat.hpp:158:22: note: 'arma::math' declared here
typedef Math<double> math;
                     ^

您需要arma::math::inf(),而不是math::inf()