2014-07-17 2 views
4

저는 R CRAN의 큰 배열에 대해 방정식 (x % * % res = y 형식)을 빨리 푸는 데 노력하고 있습니다.
데이터 x와 y를 가지고 있고 res를 계산하려고합니다. 어떻게이 작업을 가장 신속하게 수행 할 수 있습니까? 고마워요! 여기R에서 여러 방정식을 빨리 해결하는 방법은 무엇입니까?

는 예를 몇 가지 방법입니다 :

# 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,])}}) 

배열을 통해 루프 좋은 해결책인가 (? 가장 빠른 "해결"처럼 보인다)?

답변

0

스파 스 매트릭스를 사용 하시겠습니까? :

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)}) 
관련 문제