[英]Compare vector to double in rcpp armadillo
考虑这个 Rcpp Armadillo function:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
// [[Rcpp::export]]
vec testfun(const vec &x,
const double &y,
const double &z)
{
vec out = ((y < x) - z) % (x - y);
return out;
}
现在运行以下 R 脚本我得到不一致的结果:
Rcpp::sourceCpp("functions/test.cpp")
x <- 1:3
y <- 2
z <- 0.5
out_r <- ((y < x) - z) * (x - y)
out_cpp <- testfun(x, y, z)
print(out_r)
print(out_cpp)
[1] 0.5 0.0 0.5
[,1]
[1,] 0
[2,] 0
[3,] 1
所以不知何故,比较失败了。 对于如何解决这个问题的任何建议,我都会很高兴。 来自 R,我认为循环对于这项任务来说太复杂了。 也许我错了。
几个简短的陈述:
unsigned int
的比较到带有double
问题的减法。第二部分确实是麻烦所在。 为了说明,我们将通过编写调试 function 系统地逐步执行每个操作,如下所示:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
// [[Rcpp::export]]
arma::vec debug_inline_statements(const vec &x, const double &y, const double &z)
{
// Isolate the problem statement:
// Ok
Rcpp::Rcout << "(x - y)" << std::endl << (x - y) << std::endl;
// Ok
Rcpp::Rcout << "1.0*(y < x):" << std::endl << 1.0*(y < x) << std::endl;
// Bad
Rcpp::Rcout << "(1.0*(y < x) - z):" << std::endl << ((1.0*(y < x)) - z) << std::endl;
// What went wrong? Conversion from Unsigned integer to double.
// Solution: Help the template expansion:
vec bool_to_double = arma::conv_to<arma::vec>::from(y < x);
Rcpp::Rcout << "(double(y < x) - z):" << std::endl << (bool_to_double - z) << std::endl;
// Success!
// All together now:
Rcpp::Rcout << "(double(y < x) - z) % (x - y):" << std::endl <<
(arma::conv_to<arma::vec>::from(y < x) - z) % (x - y) << std::endl;
return (arma::conv_to<arma::vec>::from(y < x) - z) % (x - y);
}
运行 function 给出:
x <- 1:3
y <- 2
z <- 0.5
out_cpp <- debug_inline_statements(x, y, z)
# (x - y)
# -1.0000
# 0
# 1.0000
#
# 1.0*(y < x):
# 0
# 0
# 1
#
# (1.0*(y < x) - z):
# 0
# 0
# 1
#
# (double(y < x) - z):
# -0.5000
# -0.5000
# 0.5000
#
# (double(y < x) - z) % (x - y):
# 0.5000
# 0
# 0.5000
output 不符合预期:
(1.0*(y < x) - z)
通过进行显式类型转换,从uvec
到vec
,计算再次可行:
(arma::conv_to<arma::vec>::from(y < x) - z)
请注意,显式转换请求是通过arma::conv_to<>::from()
对计算的向量部分完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.