0

Say you have n. vectors of arbitrary (but equal) length m each. Is there a (numpy?) function, or a quick way, of calculating all pairwise dot products between these n. vectors?

My initial thought was that you could calculate ATA and take the upper triangular portion, but I'm not sure if that matrix multiplication is slow/computationally intensive. Is there a quicker/efficient way? Or should I just define a function using a for loop for all combinations of pairs?

questionmark
  • 143
  • 1
  • 9

1 Answers1

1

As per @Brenila’s comment, use tensordot:

np.tensordot(arr, arr, axes=(0,0))

Result shape is (n, n) for n = arr.shape[-1]

Cimbali
  • 11,012
  • 1
  • 39
  • 68