I'm currently computing a quadratic form by taking a known vector and using the element wise multiplication of two outer products as the input matrix. To be specific, my code looks something like this
set.seed(42) # for sake of reproducibility
library(emulator)
Fun <- function(a,b) sqrt((1/(2*pi)))*exp(-0.5*(a-b)^2)
n <- 5000
x <- rnorm(n)
y <- rnorm(n)
u <- rnorm(n)
I <- quad.form(outer(x,x,Fun)*outer(y,y,Fun),u)
This is quite slow and the problem gets significantly worse as n increases. As far as I can make out, the part that causes the problem is the outer(x,x,Fun)*outer(y,y,Fun) term inside the quadratic form.
Is there any way to speed this up?