Suppose I have a 2d (square) matrix and a function:
import numpy as np
data = np.random.rand(10000, 10000)
def func(v1, v2):
n1, n2 = np.linalg.norm(v1), np.linalg.norm(v2)
return(np.dot(v1, v2) / (n1 * n2))
I want to calculate 'func' for each pair of rows in 'data' and save it to an output matrix 'out'. So basically the equivalent of:
out = np.ndarray(data.shape)
for i in range(data.shape[0]):
for j in range(data.shape[1]):
out[i,j] = func(data[i, :], data[j, :])
Obviously the above is super slow and inefficient. What is the most optimal and (num)pythonic way of iterating through pairs (tuples in general) of rows in an array like this? Given 'func' is an arbitrary R^n x R^n -> R function.