Python, Pairwise 'distance', need a fast way to do it

前端 未结 3 1447
渐次进展
渐次进展 2021-01-12 16:27

For a side project in my PhD, I engaged in the task of modelling some system in Python. Efficiency wise, my program hits a bottleneck in the following problem, which I\'ll e

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 16:51

    You can use it something like this:

    def distance3d (p, q):
        if (p == q).all ():
            return 0
    
        p0 = p[0:3]
        p1 = p[3:6]
        q0 = q[0:3]
        q1 = q[3:6]
    
        ...  # Distance computation using the formula above.
    
    print (distance.cdist (List_of_segments, List_of_segments, distance3d))
    

    It doesn't seem to be any faster, though, since it executes the same loop internally.

提交回复
热议问题