简体   繁体   中英

Unable to call Rcpp's factorial Function with a Scalar

I define a c++ function in R, it is:

library(Rcpp)
cppFunction(
'double foo(double t, int k) {
    double x = t/factorial(k);
}')

When I run this function in R, I receive an error:

file59b051c6b334.cpp:7:25: error: no matching function for call to 'factorial'

 NumericVector x = t/factorial(k); ^~~~~~~~~ 

/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/sugar/functions/math.h:59:19:

note: candidate function not viable: no known conversion from 'int' to 'SEXP' (aka 'SEXPREC *') for 1st argument VECTORIZED_MATH_1(factorial , ::Rcpp::internal::factorial )

/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/sugar/block/Vectorized_Math.h:91:9:

note: expanded from macro 'VECTORIZED_MATH_1'

  __NAME__( SEXP x){ return __NAME__( NumericVector( x ) ) ; } 

Could anybody please help me to solve this problem? Thanks!

The issue is two fold:

  1. The factorial function is part of VECTORIZED_MATH_1 that requires a Rcpp::NumericVector parameter.
  2. You are missing a return statement.

Use:

Rcpp::cppFunction(
    'Rcpp::NumericVector foo(double t, Rcpp::NumericVector k) {
      Rcpp::NumericVector x = t/factorial(k);
      return x;
    }')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM