I want to calculate N (N is big) quadratic forms. I am using the command 'quad.form' from the R package 'emulator'. How can I implement this without using a for loop?
So far, I am using
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])
}
Is there a more direct and efficient way to calculate these quadratic forms?
Something intriguing is that
quad.form(A,x)
is (10 times) faster than the for loop, but I only need the diagonal of this outcome. So, it would still be an inefficient way of calculating the N quadratic forms of interest.
How about
? Gets the right answer for this example at least ... and should be much faster!