计算二次形式的有效方法:避免循环?

时间:2014-11-26 19:13:26

标签: r quadratic

我想计算N(N很大)quadratic forms。我正在使用命令' quad.form'来自R包'模拟器'。如何在不使用for循环的情况下实现它?

到目前为止,我正在使用

library(emulator)

A = matrix(1,ncol=5,nrow=5) # A matrix

x = matrix(1:25,ncol=5,nrow=5) # The vectors of interest in the QF

# for loop
QF = vector()
for(i in 1:5){
QF[i] = quad.form(A,x[,i])
}

有没有更直接有效的方法来计算这些二次形式?

有趣的是

quad.form(A,x)

比for循环快10倍,但我只需要这个结果的对角线。因此,它仍然是计算N个二次形式的低效方式。

2 个答案:

答案 0 :(得分:6)

怎么样

colSums(x * (A %*% x))

?至少得到这个例子的正确答案......并且应该快得多!

library("rbenchmark")
A <- matrix(1, ncol=500, nrow=500)
x <- matrix(1:25, ncol=500, nrow=500)

library("emulator")
aa <- function(A,x) apply(x, 2, function (y) quad.form(A,y))
cs <- function(A,x) colSums(x * (A %*% x))
dq <- function(A,x) diag(quad.form(A,x))
all.equal(cs(A,x),dq(A,x))  ## TRUE
all.equal(cs(A,x),aa(A,x))  ## TRUE
benchmark(aa(A,x),
          cs(A,x),
          dq(A,x))
##       test replications elapsed relative user.self sys.self
## 1 aa(A, x)          100  13.121    1.346    13.085    0.024
## 2 cs(A, x)          100   9.746    1.000     9.521    0.224
## 3 dq(A, x)          100  26.369    2.706    25.773    0.592

答案 1 :(得分:2)

使用apply功能:

apply(x, 2, function (y) quad.form(A,y))

如果你使矩阵变大(500x500),那么使用apply的速度大约是使用quad.form(A,x)的两倍:

A <- matrix(1, ncol=500, nrow=500)
x <- matrix(1:25, ncol=500, nrow=500)

system.time(apply(x, 2, function (y) quad.form(A,y)))
# user  system elapsed 
# 0.183   0.000   0.183 

system.time(quad.form(A,x))
# user  system elapsed 
# 0.314   0.000   0.314 

修改

@Ben Bolker的答案比apply快1/3:

system.time(colSums(x * (A %*% x)))
# user  system elapsed 
# 0.123   0.000   0.123 
相关问题