简体   繁体   English

R 中的 Toeplitz 矩阵向量乘法

[英]Toeplitz Matrix-Vector multiplication in R

I have anxn symmetrix toeplitz matrix T , a vector v of length n, and I would like to compute the matrix-vector product T%*%v quickly.我有 anxn symmetrix toeplitz 矩阵T ,一个长度为 n 的向量v ,我想快速计算矩阵向量乘积T%*%v Is there a package in R that can use the fast fourier transform method of computing T%*%v (or some other method if one exists)? R 中是否有一个包可以使用快速傅立叶变换方法来计算T%*%v (或其他一些方法,如果存在的话)? For example, Matlab has the Toeplitzmult package.例如,Matlab 有 Toeplitzmult 包。

The function below works.下面的功能有效。 Note that the ifft() function requires the pracma library.请注意, ifft()函数需要pracma库。

toepmult <- function(A,v){
    n <- nrow(A)
    x <- as.matrix(c(A[1,],0,A[1,][n:2]))
    p <- c(v,rep(0,n))
    h <- as.vector(fft(p)*fft(x))
    out <- Re(pracma::ifft(h)[1:n])
    return( matrix(out,n) )
}

For a vector/matrix of size 1000, the toepmult function takes about 18% of the time A%*%v takes.对于大小为 1000 的向量/矩阵, toepmult函数占用的时间约为A%*%v 18%。

A <- toeplitz(runif(1000))
v <- runif(1000)
microb(A%*%v,toepmult(A,v),times=1000)
#Unit: microseconds
#           expr      min       lq      mean   median        uq      max neval
#        A %*% v 1515.858 1597.345 1809.3517 1693.533 1957.4350 3868.788  1000
# toepmult(A, v)  185.901  215.395  331.2928  298.435  347.7335 4611.285  1000
#[[1]]
#           [,1]      [,2]
#median 1693.533   298.435
#ratio     1.000     0.176
#diff      0.000 -1395.098

For a vector/matrix of size 10,000, the toepmult function takes about 2.5% of the time A%*%v takes.对于大小为 10,000 的向量/矩阵, toepmult函数占用的时间约为A%*%v 2.5%。

A <- toeplitz(runif(10000))
v <- runif(10000)
microb(A%*%v,toepmult(A,v),times=1000)
#Unit: milliseconds
#           expr        min         lq       mean     median         uq      max neval
#        A %*% v 145.834304 160.395663 181.842779 170.396014 186.221449 495.2003  1000
# toepmult(A, v)   2.802058   4.077408   4.990894   4.322707   4.911103 180.4926  1000
#[[1]]
#          [,1]     [,2]
#median 170.396    4.323
#ratio    1.000    0.025
#diff     0.000 -166.073

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM