- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我注意到 RcppArmadillo
支持 FFT 和 2-D FFT。不幸的是,我的数据在 ifft2
(RcppArmadillo
) 和 R 的原生 mvfft(..., inverse = TRUE)
之间存在显着差异。这在第 0 个 bin 中特别大(这在我的应用程序中非常重要)。差异不是标量倍数。我找不到任何文档或解释这些偏差,尤其是在第 0 个 bin 中。
我已经专门针对 ifft(arma::cx_mat input)
函数调用调试了这个问题。除非可能存在不可预见的内存管理问题,否则这是罪魁祸首。
示例:ifft2
结果(1 列前 5 个条目):
[1] 0.513297156-0.423498014i -0.129250939+0.300225299i
0.039722228-0.093052563i -0.007956237+0.018643534i 0.001181177-0.002768473i
mvfft
反转结果(1 列前 5 个条目):
[1] 0.278131988-0.633838170i -0.195699114+0.445980950i
0.060070320-0.136894940i -0.011924932+0.027175865i 0.001754788-0.003999007i
问题
RcppArmadillo
FFT 是否仍在开发中? 可重现性 - 下面我尽可能地浓缩了问题并重现了问题。更新为最少的代码Rcpp代码:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
//profile is the dependent variable of a given variable x,
//q is a vector containing complex valued information for a single column after a tcrossprod
//Size is a scalar value which the FFT depends upon.
arma::cx_mat DebugLmnCPP( arma::cx_vec Profile, arma::cx_vec q) {
std::complex<double> oneeye (0,1);//Cmplx number (0 + 1i)
arma::cx_mat qFFT = ifft2( exp( oneeye * (Profile * q.st() ) ) );
return(qFFT );
}
// [[Rcpp::export]]
//For pedagogical purposes
arma::cx_mat DebugIFFTRCPP( arma::cx_mat input) {
arma::cx_mat qFFT = ifft2( input );
return( qFFT );
}
RCode(抱歉,这太草率了)
library(Rcpp)
library(RcppArmadillo)
sourceCpp("/home/FILE.cpp")
#Use C++ function
qt <- c(6.0+0i, 5.95+0i, 0.10+0i)
prof <- 0.25* sin( (1:512)*(2*3.1415)/512 ) + 0.25#Offset Sine wave
Debug1 <- DebugLmnCPP( Profile = prof, q = qt )
#Use R function
FFTSize <- 2^9
DebugLmnR <- function(Profile, q) {
g <- (0+1i)*(as.matrix(Profile ) %*% t(q))
qFFT <- mvfft( exp(g) , inverse = TRUE) / FFTSize
return( qFFT )
}
#Call function
Debug2 <- DebugLmnR( Profile = prof, q = qt )
#Use R and C++
DebugLmnRC <- function(Profile, q) {
g <- (0+1i)*(as.matrix(Profile ) %*% t(q))
qFFT <- DebugIFFTRCPP(exp(g))
return( qFFT )
}
#Call function
Debug3 <- DebugLmnRC( Profile = prof, q = qt )
#Compare Results
Debug1[1:5,1] #CPP
Debug2[1:5,1] #R
Debug3[1:5,1] #R and CPP
产量:
> Debug1[1:5,1]
[1] 0.359632774+0.35083419i -0.037254305-0.36995074i 0.015576046+0.15288379i -0.004552119-0.03992962i
[5] 0.000967252+0.00765564i
> Debug2[1:5,1]
[1] 0.03620451+0.51053116i -0.04624384-0.55604273i 0.02204910+0.23101589i -0.00653108-0.06061692i
[5] 0.00140213+0.01167389i
> Debug3[1:5,1]
[1] 0.359632774+0.35083419i -0.037254305-0.36995074i 0.015576046+0.15288379i -0.004552119-0.03992962i
[5] 0.000967252+0.00765564i
最佳答案
我不是特别喜欢你的例子:
这是一个更简单的例子。 help(fft)
in R leads of this example
fftR> x <- 1:4
fftR> fft(x)
[1] 10+0i -2+2i -2+0i -2-2i
fftR> fft(fft(x), inverse = TRUE)/length(x)
[1] 1+0i 2+0i 3+0i 4+0i
我们可以使用 RcppArmadillo 轻松重现:
R> cppFunction("arma::cx_mat armafft(arma::vec x) { return fft(x); }",
+ depends="RcppArmadillo")
R> armafft(1:4)
[,1]
[1,] 10+0i
[2,] -2+2i
[3,] -2+0i
[4,] -2-2i
R>
并添加逆
R> cppFunction("arma::cx_mat armaifft(arma::cx_mat x) { return ifft(x); }",
+ depends="RcppArmadillo")
R> armaifft(armafft(1:4))
[,1]
[1,] 1+0i
[2,] 2+0i
[3,] 3+0i
[4,] 4+0i
R>
恢复我们在 R 示例中的输入。
据我所知没有错误,而且我没有理由相信这对于 2d 情况有任何不同......
编辑/跟进:错误出在 OP,而不是 Armadillo。这里的主要问题是
这里的主要问题是 Armadillo 的 fft()
可以在 vector 或矩阵上工作,因此(在矩阵情况下)对应于 R 的 mvfft( )
。 Armadillo 的 fft2()
只是其他东西,与此处无关。
让我们继续/扩展我们之前的例子。我们重新定义访问器以使用复杂的矩阵值:
R> cppFunction("arma::cx_mat armafft(arma::cx_mat x) { return fft(x); }",
+ depends="RcppArmadillo")
R>
然后定义一个 5 x 2 的复杂数组,我们将其提供给它:
R> z <- array(1:10 + 1i, dim=c(5,2))
R> z
[,1] [,2]
[1,] 1+1i 6+1i
[2,] 2+1i 7+1i
[3,] 3+1i 8+1i
[4,] 4+1i 9+1i
[5,] 5+1i 10+1i
R>
R> armafft(z)
[,1] [,2]
[1,] 15.0+5.00000i 40.0+5.00000i
[2,] -2.5+3.44095i -2.5+3.44095i
[3,] -2.5+0.81230i -2.5+0.81230i
[4,] -2.5-0.81230i -2.5-0.81230i
[5,] -2.5-3.44095i -2.5-3.44095i
R>
这与我们在每一列上单独运行函数得到的输出相同。这也是 R 为 mvfft()
所做的(cf help(fft)
)
R> mvfft(z)
[,1] [,2]
[1,] 15.0+5.00000i 40.0+5.00000i
[2,] -2.5+3.44095i -2.5+3.44095i
[3,] -2.5+0.81230i -2.5+0.81230i
[4,] -2.5-0.81230i -2.5-0.81230i
[5,] -2.5-3.44095i -2.5-3.44095i
R>
相同的结果,不同的库/包,据我所知没有错误。
关于c++ - RcppArmadillo ifft2 与 R 的原生 mvfft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38942881/
考虑以下 R 代码, ## ----------- R version ----------- caller using namespace arma ; using n
RcppArmadillo 是我尝试安装的一些软件包的依赖项。我在编译 RcppArmadillo 版本 0.10.1.0.0 时收到此错误(这是 R 在发现 RcppArmadillo 是一个 de
我正在研究 a package ,它使用来自 RcppArmadillo 的随机数。该软件包运行 MCMC 算法,为了获得精确的再现性,用户应该能够设置随机数种子。执行此操作时,似乎用于从 Gamma
我是 Rcpp 的新手,正在尝试基于 for() 中的负索引执行计算- 使用 RcppArmadillo 循环。 我已经发现 RcppArmadillo 中的负索引不是那么简单,但是可以通过应该保留的
我正在尝试使用 RcppArmadillo 定义一个可以处理稀疏和密集矩阵输入的模板函数。我得到了一个非常简单的案例,将一个密集或稀疏矩阵发送到 C++,然后返回到 R 以像这样工作: library
我试图找到一个非常大的稀疏矩阵的特征值。我正在使用 RcppArmadillo 的 eig_gen 函数,它不是专门用于稀疏矩阵的,但只要计算是以单精度完成的,我就可以接受。所以我的 cpp 代码是:
我在我的 Rcpp 代码中使用 RcppArmadillo::sample,它在下面有这种奇怪的行为。 fun_good 按预期工作,从 x vector 中采样 1 个元素。然而,fun_bad 不
问题是我有一个变量 arma::mat prob_vec并想要相当于 rmultinom(1, 1, prob_vec) 的东西在 R。 我找到了 rmultinom RcppArmadillo 提供
这是一个用于绘制 N 的 C++ 函数均值为零和标准差的独立正态偏差s // [[Rcpp::depends(RcppArmadillo)]] #include using namespace Rc
我有以下 Rcpp/RcppArmadillo 函数,它计算矩阵中的相关距离 #include using namespace Rcpp; // [[Rcpp::export]] arma::mat
开始有 R 经验,但完全是 C++ 新手,我用 RcppArmadillo 编写了一些函数,并且对它的可用性和速度非常热情。我现在想使用函数 RcppArmadillo.package.skeleto
我在编译这个简单的 c++ 时遇到了一些麻烦代码使用 Rcpp和 RcppArmadillo包裹。以下面的简单示例将矩阵的每一列乘以数值标量: code (m); for(int i = 0; i :
问候和称呼, 我正在尝试使用字段对象类型而不是列表数据类型来避免必须发出复制命令。我试图这样做是为了减少与从列表中删除一个矩阵相关的时间,该矩阵由 Armadillo 的数据结构中已经定义的矩阵进行操
我正在尝试使用 RcppArmadillo 通过完全旋转来实现 LU 分解。幸运的是我有 this可以执行我想要的操作的 Matlab 代码,但我在将其转换为 Armadillo 时遇到了一些挑战。
将 log1p() 应用于整个 arma::vec 的合适方法是什么?似乎有 log() 和 exp() 的矢量化版本,但没有 log1p()。我发现 NumericVector 有语法糖,所以我最终
以下玩具示例为 parallelFor工作正常( f2 是 f1 的并行版本): // [[Rcpp::depends(RcppParallel)]] // [[Rcpp::depends(RcppA
我在编译 RcppArmadillo 时遇到问题。这是我尝试安装软件包时的结果: > install.packages("RcppArmadillo") Installing package(s)
我试图了解用 RcppArmadillo 编写的函数与使用 Armadillo 库在独立 C++ 程序中编写的函数之间的性能差异。例如,考虑以下简单函数,该函数使用传统教科书公式计算线性模型的系数。
真的很困惑为什么使用 RcppArmadillo 的 QR 输出与 R 的 QR 输出不同; Armadillo 文档也没有给出明确的答案。本质上,当我给 R 一个矩阵 Y 是 n * q (比如 1
我需要使用名为 dgebal 的 Fortran 例程(文档 here )在我的 Rcpparmadillo 代码中。我已经包含了以下标题: # include # include 但是,当我尝试
我是一名优秀的程序员,十分优秀!