Rcpp:如何将复数从R传递到cpp
Rcpp: how to pass complex number from R to cpp
我想使用Rcpp将复数从R传递到我的cpp代码。我尝试像传递双精度和整数一样传递复数:
#include <complex>
#include <Rcpp.h>
using namespace Rcpp;
RcppExport SEXP mandelC(SEXP s_c) {
std::complex<double> c = ComplexVector(s_c)[0];
}
但是,代码无法编译并报错:
g++ -I/usr/share/R/include -DNDEBUG -I/usr/share/R/include -fopenmp -I/home/siim/lib/R/Rcpp/include -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c a.cpp -o a.o
a.cpp: In function ‘SEXPREC* mandelC(SEXP)’:
a.cpp:7:50: error: conversion from ‘Rcpp::traits::storage_type<15>::type {aka Rcomplex}’ to non-scalar type ‘std::complex<double>’ requested
std::complex<double> c = ComplexVector(s_c)[0];
^
显然,我做错了什么,但我一直找不到任何的例子。有人能给我指路吗?
你错过了一些非常简单的东西:
R> cppFunction("ComplexVector doubleMe(ComplexVector x) { return x+x; }")
R> doubleMe(1+1i)
[1] 2+2i
R> doubleMe(c(1+1i, 2+2i))
[1] 2+2i 4+4i
R>
记住所有都是R中的向量,标量并不"真正"存在:它们是长度为1的向量。因此,对于单个complex
数,您(仍然)传递长度为1的ComplexVector
。
看看Baptiste的两个包,它们通过RcppArmadillo进行复杂的数学运算——这"证明"了一些RcppArmadillo接口是按照它们应该的方式工作的。
编辑:如果你真的需要一个标量,你也可以得到它:
R> cppFunction("std::complex<double> doubleMeScalar(std::complex<double> x) {
+ return x+x; }")
R> doubleMeScalar(1+1i)
[1] 2+2i
R>
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何使用Rcpp将R函数转换为C++函数
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- Rcpp - 来源Cpp - 未定义的符号
- Rcpp:如何将复数从R传递到cpp
- 在带有Rcpp的R包中,将头(.h)文件与cpp文件一起使用