0

I have a bunch of 4D points, and I'm looking to get their perpendicular distance from the line w=x=y=z (assuming w,x,y,z to be the 4D axes). It seems like there should be an easy way to do this in python, but I can't seem to figure it out.

Aside: This is basically a 4D scatter plot, and I'm trying to see how far each point is from the ideal scenario (w=x=y=z). Is there a mathematical term for this metric?

Kinshuk
  • 3
  • 2
  • You could try implementing a 4D version of [this](http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html). – Antimony Oct 12 '17 at 00:37
  • [simply calculate it](https://stackoverflow.com/questions/23353977/calculate-euclidean-distance-between-4-dimensional-vectors) or [use a module](https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy) – ivan7707 Oct 12 '17 at 00:41
  • What have you tried, and what is it doing differently from what you expect? If you don't show code, you are unlikely to get a quality answer. – Chris Johnson Oct 12 '17 at 00:46
  • @Antimony I don't think 4D spaces have cross products, so I'm not sure how to directly extend that into 4D – Kinshuk Oct 12 '17 at 01:02
  • @ivan7707 Those are pointing towards distance between two points. Sure, I could find the point of projection and then calculate, but just wondering if there's a different approach. – Kinshuk Oct 12 '17 at 01:04
  • @ChrisJohnson I don't know how to calculate that - hence no code. Appreciate your suggestion though (this is my first question ever) – Kinshuk Oct 12 '17 at 01:05
  • How would you do it without programming at all? Start withdraw formula then start translating into code. – Chris Johnson Oct 12 '17 at 01:15

2 Answers2

2

Any line has parametric equation with some base point and unit direction vector. Here P0=(0,0,0,0) and u=(1/2, 1/2, 1/2, 1/2).
Vector from base point to every point P is equal to their coordinates, and distance from P to the line is:

 D = (P - (P.dot.u) * u).length

enter image description here

Scalar product approach works for any number of dimensions.

MBo
  • 77,366
  • 5
  • 53
  • 86
0

This is an implementation of euclidian distance that works for any number of dimensions

def distance_to_line(line, pts, l0=None, p0=None):
    """
    line defined between l0 and line 
    points defined between p0 and pts
    """
    # line origin other than (0,0,0,..)
    if l0 is not None:
        line = line-l0
    # points origin other than (0,0,0,..)
    if p0 is not None:
        pts = pts-p0
    dp = np.dot(pts,line)
    pp = dp/np.linalg.norm(line)
    pn = np.linalg.norm(pts, axis=1)
    return np.sqrt(pn**2 - pp**2)



distance_to_line(l0=np.transpose((0,0,0,0,0)),
                 line=np.transpose((1,1,0,0,0)),
                 pts=np.array([(2,0,0,0,1),
                               (1,1,0,0,1),
                               (0,1,0,0,1),
                               (1,2,0,0,1)]))

>>> array([ 1.73205081,  1.        ,  1.22474487,  1.22474487])
Cobry
  • 4,348
  • 8
  • 33
  • 49