Rcpp和RcppArmadillo都有谓词来测试NA
,NaN
(R扩展)和Inf
。
这里是一个短RcppArmadillo例如:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat foo(int n, double threshold=NA_REAL) {
arma::mat M = arma::zeros<arma::mat>(n,n);
if (arma::is_finite(threshold)) M = M + threshold;
return M;
}
/*** R
foo(2)
foo(2, 3.1415)
***/
我们初始化为零的对于参数的矩阵,并且测试。如果它是有限的(即不是NA
或Inf
或NaN
),那么我们添加该值。如果你想,你也可以单独测试可能性。
这产生所需的结果:没有第二个参数,默认值NA
适用,我们得到一个零矩阵。
R> Rcpp::sourceCpp("/tmp/giorgio.cpp")
R> foo(2)
[,1] [,2]
[1,] 0 0
[2,] 0 0
R> foo(2, 3.1415)
[,1] [,2]
[1,] 3.1415 3.1415
[2,] 3.1415 3.1415
R>
当你说失踪时,你的意思是“不适用”还是你的意思是你没有给它任何价值? – stanekam 2014-10-07 17:22:12