【发布时间】:2014-09-08 15:47:16
【问题描述】:
我正在尝试为 R CRAN 中的大型数组快速求解方程(形式为 x%*%res = y)。
我有数据 x 和 y 并想计算 res。
怎样才能做到最好,即快速?非常感谢!
这是一个例子和一些方法:(似乎“解决”是最快的?)
# setup:
p = 20 # dimension of matrix to solve
nmkt= 3000 # length of array, i.e., number of equations to solve
res = matrix(0,p,nmkt) # result matrix
x = array(rnorm(p*p*nmkt),c(p,p,nmkt)) # data
# make x symetric and invertible
for(i in 1:nmkt){ x[, , i]= crossprod(x[, , i])+diag(p)*0.01}
y = matrix(rnorm(p*nmkt),nmkt,p) # data
# computation and test:
R=100 # number of replications (actually much larger than 100 in my application R=1e5 or 1e7)
system.time(for(r in 1:R){ for(i in 1:nmkt){res[,i] = qr.solve(x[, , i], y[i,], tol = 1e-7)}})
system.time(for(r in 1:R){ for(i in 1:nmkt){res[,i] = solve(x[, , i], y[i,], tol = 1e-7)}})
system.time(for(r in 1:R){ for(i in 1:nmkt){res[,i] = crossprod( chol2inv(chol( x[, , i] )) , y[i,] )}})
通过数组的循环是一个好的解决方案吗?
【问题讨论】:
标签: arrays r performance