pythonnumpyscipyscipy-spatial

How to calculate vektor distances fast in python


I would like to calculate the vector between points as fast as possible. My Input array has a list of points in 3D. And looks like this

b = [[0,0,1],  #first point
     [1,0,2],  #second point
     ...    ]

I want to calculate a matrix with the vector as entry (m_ij = b_i - b_j) and it should look like this:

m = [[[0,0,0], [-1,0,-1],...],
     [[1,0,1], [0,0,0],...],
          ...             ]

How do I calculate this with built in functions of numpy and scipy without using any slow loops? I found pdist, but this uses a metric and I don't think it really fits my needs.


Solution

  • Use broadcasting:

    m = b[:,None]-b[None]
    

    Output:

    array([[[ 0,  0,  0],
            [-1,  0, -1]],
    
           [[ 1,  0,  1],
            [ 0,  0,  0]]])
    

    Shapes:

    b[:,None].shape
    (2, 1, 3)
    
    b[None].shape
    (1, 2, 3)
    
    m.shape
    (2, 2, 3)