I am doing a cross-product of contiguous path segments (xy coordinates) using the following script:
In [129]: def func1(xy, s): size = xy.shape[0]-2*s out = np.zeros(size) for i in range(size): p1, p2 = xy[i], xy[i+s] #segment 1 p3, p4 = xy[i+s], xy[i+2*s] #segment 2 out[i] = np.cross(p1-p2, p4-p3) return out def func2(xy, s): size = xy.shape[0]-2*s p1 = xy[0:size] p2 = xy[s:size+s] p3 = p2 p4 = xy[2*s:size+2*s] tmp1 = p1-p2 tmp2 = p4-p3 return tmp1[:, 0] * tmp2[:, 1] - tmp2[:, 0] * tmp1[:, 1] In [136]: xy = np.array([[1,2],[2,3],[3,4],[5,6],[7,8],[2,4],[5,2],[9,9],[1,1]]) func2(xy, 2) Out[136]: array([ 0, -3, 16, 1, 22])
func1 is especially slow due to the inner loop, so I rewrote the cross product myself (func2), which is orders of magnitude faster.
Is it possible to use the numpy einsum function to perform the same calculation?