![](/img/trans.png)
[英]MATLAB : how to solve coupled differential equations dependend on data stored in arrays
[英]How to fast solve multiple equations in R?
我正在嘗試快速解決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,] )}})
通過陣列的循環是一個很好的解決方案嗎?
或者使用稀疏矩陣? :
require(Matrix)
j = c(matrix(1:(p*nmkt),p,p*nmkt,byrow=TRUE))
i = c(aperm( array(j,c(p,p,nmkt)), c(2,1,3)))
system.time(for(r in 1:R){ res= solve(sparseMatrix(i=i, j=j, x = c(x)), c(t(y)), tol = 1e-7)} )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.