if (FALSE) {
nx <- 100
ny <- 150
x <- matrix(runif(nx*3), nrow=nx, ncol=3) # matrix 100 x 3
y <- matrix(runif(ny*3), nrow=ny, ncol=3) # matrix 150 x 3
eta <- matrix(runif(nx*1), nrow=nx, ncol=1) # matrix 100 x 1
# nrow(x) == nrow(eta)
x_i <- LazyTensor(x, index = 'i') # LazyTensor from matrix x, indexed by 'i'
y_j <- LazyTensor(y, index = 'j') # LazyTensor from matrix y, indexed by 'j'
eta_i <- LazyTensor(eta, index = 'i') # LazyTensor from matrix eta,
# indexed by 'i' (like x_i)
# gradient with the formula from position
grad_xy <- grad(sqnorm2(x_i-y_j), eta_i, "Sum", var = y_j$formula, "j")
# gradient with the formula from index
grad_xy <- grad(sqnorm2(x_i-y_j), eta_i, "Sum", var = 0, "j")
}
Run the code above in your browser using DataLab