简体   繁体   English

Rcpp:将lfactorial应用于NumericVector

[英]Rcpp: Apply lfactorial to NumericVector

What is the nicest/easiest way to apply the lfactorial function to a NumericVector in Rcpp. lfactorial函数应用于NumericVector中的NumericVector最好/最简单的方法是什么? In RI would have something like this: RI会有类似的东西:

> vec <- c(1,2,3,4)
> lfactorial(vec)
[1] 0.0000000 0.6931472 1.7917595 3.1780538

In Rcpp, suppose I have: 在Rcpp中,假设我有:

NumericVector vec = NumericVector::create(1,2,3,4);

What is the nicest way to apply Rcpp::internal::lfactorial to each element of vec ? Rcpp::internal::lfactorial应用于vec每个元素的最好方法是什么?

Thanks for any help. 谢谢你的帮助。

Rcpp, just like R, is vectorized: 与R一样,Rcpp被矢量化:

R> cppFunction('NumericVector lfac(NumericVector x) { \
                                        return lfactorial(x); }')
R> lfac(1:4)
[1] 0.000000 0.693147 1.791759 3.178054
R> 

That is one line of code I simply broke for nicer exposition here. 这是一行代码我只是为了更好的阐述而破解。 Remove the backslash before copying and pasting. 在复制和粘贴之前删除反斜杠。 You can write it either as a NumericVector or IntegerVector signature. 您可以将其写为NumericVectorIntegerVector签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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