This is an optimization problem. Given matrices E, H, Q, F and the logic in method my_func_basic (see code block), populate matrix V. Any potential ways, such as through vectorization, to speed up the computation? Thanks.
import timeit
import numpy as np
n = 20
m = 90
# E: m x n
E = np.random.randn(m,n)
# H, Q: m x m
H = np.random.randn(m,m)
Q = np.random.randn(m,m)
# F: n x n
F = np.random.randn(n,n)
# V: m x m
V = np.zeros(shape=(m,m))
def my_func_basic():
for x in range(n):
for y in range(n):
if x == y:
V[x][y] = np.nan
continue
h = H[x][y]
e = np.array([E[x,:]+h*E[y,:]])
v1 = np.dot(np.dot(e,F),np.transpose(e))[0][0]
v2 = Q[x][x]+h**2*Q[y][y]
V[x][y] = v1/np.sqrt(v2)
print(timeit.timeit(my_func_basic,number=1000),'(sec), too slow...')
E[x,:] + h*E[y,:]should already be an array. You can probably get away with just reshaping it to add the extra dimension. Otherwise, it might be a little easier to tell what's going on if you explain mathematically what it is that you're trying to accomplish. There might be some simplifications on that side that could help...