I have searched for a solution to determine distances using einsum for numpy arrays that are not equal in their number of rows, but equal in columns. I have tried various combinations but the only way I can do it successful is using the following code. I am obviously missing something and the literature and numerous threads haven't lead me any closer to a solution. I would appreciate finding a generality such that the origins could be of any number for a destination array of any number. I am only working with 2D arrays and have no intention of extending this to other dimensions. I am also familiar with pdist and cdist and other ways of reaching my desired solution, however, I am only interested in einsum since I want to round out my arsenal of examples. Any help would be appreciated.
import numpy as np
origs = np.array([[0.,0.],[1.,0.],[0.,1.],[1.,1.]])
dests = np.asarray([[4.,0.],[1.,1.],[2.,2.],[2.,3.],[0.,5.]])
for i in origs:
d =np.sqrt(np.einsum("ij,ij->i", i-dests, i-dests))
print("orig {}...dist: {}".format(i,d))
The following results are what I am looking for...
orig [ 0. 0.]...dist: [ 4. 1.41421356 2.82842712 3.60555128 5. ]
orig [ 1. 0.]...dist: [ 3. 1. 2.23606798 3.16227766 5.09901951]
orig [ 0. 1.]...dist: [ 4.12310563 1. 2.23606798 2.82842712 4. ]
orig [ 1. 1.]...dist: [ 3.16227766 0. 1.41421356 2.23606798 4.12310563]