在Rcpp(和RcppArmadillo)中,如何检查vec是否包含复数
In Rcpp (and RcppArmadillo), how to check if a vec contains complex numbers?
在R中,我们可以使用is.complex(例如is.complex(vec1)
(来检查向量(例如,vec1=c(1+1i,2)
(是否包含复数。我想知道RcppArmadillo中的等效函数是什么?
如何提取RcppArmadillo中向量中每个元素的实部,就像R中的Re(vec1)
一样?
为了提取实部和虚部,可以使用arma::real()
和arma::imag()
函数。或者,您可以使用糖函数Rcpp::Re()
和Rcpp::Im()
:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::vec getRe(arma::cx_vec x) {
return arma::real(x);
}
// [[Rcpp::export]]
Rcpp::NumericVector getIm(Rcpp::ComplexVector x) {
return Rcpp::Im(x);
}
/*** R
set.seed(42)
N <- 5
vec <- complex(5, rnorm(5), rnorm(5))
t(getRe(vec))
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.9390771 -0.04167943 0.8294135 -0.4393582 -0.3140354
Re(vec)
#> [1] -0.93907708 -0.04167943 0.82941349 -0.43935820 -0.31403543
getIm(vec)
#> [1] -2.1290236 2.5069224 -1.1273128 0.1660827 0.5767232
Im(vec)
#> [1] -2.1290236 2.5069224 -1.1273128 0.1660827 0.5767232
*/
如果你使用上面的getRe(arma::vec x)
,你会得到:
Warning message:
In getRe(vec) : imaginary parts discarded in coercion
你不能把复数放在一个不打算存储它们的对象中。这是C++作为强类型语言的结果。因此不需要is.complex()
的类似物。
请参阅Armadillo文档以获取更多参考。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- C++模板来检查友元函数的存在
- 检查输入是否不是整数或数字
- 试图让变量检查数组中的某些内容
- 检查值是否在集合p1和p2中,但不在p3中
- C++概念:如何使用'concept'检查模板化结构的属性?
- 概念TS检查忽略私有访问修饰符
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- 如何在C++中检查2D数组中负值的输入验证
- C++:正在检查LinkedList中的回文-递归方法-错误
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 如何检查一个c++字符串中有多少相同的字符/数字
- 检查不带转换的扫描格式
- 如何检查线程是否锁定
- 清除前检查矢量
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 在Rcpp(和RcppArmadillo)中,如何检查vec是否包含复数
- 当使用 std::vector::p ush_back 插入未知数量的元素时,是否应该在每次推送时检查 std::vec