我正在學習Rcpp並編寫以下兩個函數。第二個函數簡單計算sum(log_gamma(x)) - log_gamma(sum(x)),其中x是一個向量。使用Rcpp時的數據類型轉換錯誤
返回三個數據類型轉換錯誤,我無法解決所有這些錯誤。有人可以幫我糾正這些代碼嗎?謝謝。
file330c72cf6532.cpp:8:37: error: cannot convert 'Rcpp::sugar::Vectorized<Rf_lgammafn, true, Rcpp::Vector<14, Rcpp::PreserveStorage> >' to 'SEXP' in initialization
SEXP lgamma_x = lgamma(x);
^
file330c72cf6532.cpp:10:45: error: cannot convert 'SEXP' to 'double' in initialization
double up = sum_cpp(lgamma_x_vec);
^
file330c72cf6532.cpp:11:44: error: cannot convert 'Rcpp::sugar::Vectorized<Rf_lgammafn, true, Rcpp::Vector<14, Rcpp::PreserveStorage> >' to 'double' in initialization
double down = lgamma(sum_cpp(x));
^
cppFunction(
'double sum_cpp(NumericVector x){
double tmp = 0;
int n = x.size();
for(int j = 0; j<n; j++){
tmp = tmp + x[j];
}
return tmp;
}')
cppFunction('double LogB_cpp(NumericVector x){
Function sum_cpp("sum_cpp") ;
SEXP lgamma_x = lgamma(x);
NumericVector lgamma_x_vec(lgamma_x);
double up = sum_cpp(lgamma_x_vec);
double down = lgamma(sum_cpp(x));
return up - down;}')
謝謝!這很有道理。 –