pythonarraysnumpynorm

Find L3 norm of two arrays efficiently in Python


Suppose I have two arrays. A has size n by d, and B has size t by d. Suppose I want to output an array C, where C[i, j] gives the cubed L3 norm between A[i] and B[j] (both of these have size d). i.e.

C[i, j] = |A[i, 0]-B[j, 0]|**3 + |A[i, 1]-B[j, 1]|**3 + ... + |A[i, d-1]-B[j, d-1]|**3

Can anyone redirect me to a really efficient way to do this? I have tried using a double for loop and something with array operations but the runtime is incredibly slow.

Edit: I know TensorFlow.norm works, but how could I implement this myself?


Solution

  • In accordance with @gph answer, an explicit example of the application of Numpy's np.linalg.norm, using broadcasting to avoid any loops:

    import numpy as np
    
    n, m, d = 200, 300, 3
    A = np.random.random((n,d))
    B = np.random.random((m,d))
        
    C = np.linalg.norm(A[:,None,:] - B[None,...], ord=3, axis = -1)
    # The 'raw' option would be:
    C2 = (np.sum(np.abs(A[:,None,:] - B[None,...])**3, axis = -1))**(1/3)
    

    Both ways seems to take around the same time to execute. I've tried to use np.einsum but let's just say my algebra skills have seen better days. Perhaps you have better luck (a nice resource is this wiki by Divakar).