1

Problem: Following code does not provide given example coordinates as it is expected. (what algoirthm? - Please refer to link below)

I tried to implement algorithm described here: finding the coordinates of points from distance matrix@math.stackexchange.com

Code:

# https://math.stackexchange.com/questions/156161/finding-the-coordinates-of-points-from-distance-matrix

import numpy as np
from scipy import spatial

example = np.array([[0,0],[1,3],[4,4]])
distance_matrix = spatial.distance_matrix(example,example)
 
def get_m_matrix(distance_matrix):
    d = distance_matrix
    shape = d.shape
    out = np.zeros(shape)
    for i in range(shape[0]):
        for j in range(shape[1]):
            out[i,j] = ( d[0,j]**2 + d[i,0]**2 - d[i,j]**2)/2
    return out
 
m_matrix = get_m_matrix(distance_matrix)
eigenvalue, eigenvectors = np.linalg.eig(m_matrix)
eigenvalue_matrix = np.diag(eigenvalue)
assert np.equal(np.round(np.dot(np.dot(eigenvectors, eigenvalue_matrix),np.linalg.inv(eigenvectors))),np.round(m_matrix)).all()
X = np.dot(eigenvectors, eigenvalue_matrix**0.5)
 
print('example',example,sep='\n')
print('distance_matrix',distance_matrix,sep='\n')
print('m_matrix',m_matrix,sep='\n')
print('eigenvalue',eigenvalue,sep='\n')
print('eigenvectors',eigenvectors,sep='\n')
print('eigenvalue_matrix',eigenvalue_matrix,sep='\n')
print('result',X,sep='\n')

And I think I don't get something in its description. I read and watched some videos on eigenvalue decomposition. Check numpy docs on funcitions used there. I am pretty sure M matrix is fine (found some other question, but algo was a bit different, nevertheless M matrix equation was the same). Another thing, I checked np.linalg.eig on example from youtube and eigenvectors did not matched. Assertion worked but It still puzzles me.

One more thing, if you use equations in answers, PLEASE DESCRIBE VARIABLES. You are not talking to an expert, and no expert will read you 'cause they know. Answer like to a child. Thanks!

pael
  • 81
  • 7
  • What exactly is your question here? The code provided works without error. What output did you expect when compared to the output you get? – Grismar Sep 27 '20 at 23:09
  • Grismar, thanks but this code is my own. If you would click a link provided you would get that I expect to get the same coordinates as given in example variable. Also edited question. btw. This should output coordinates as provided in example variable. – pael Sep 27 '20 at 23:17
  • You cannot recover the orientation of a collection of points from distances alone (as mentioned in one of the solutions). My guess is that the solution is "correct", but is rotated relative to your initial `example` points. You can confirm by inspection that `X` has the same distances to the origin as the original points: `np.sum(example**2, axis=1)` <-> `np.sum(X**2, axis=1)` – anon01 Sep 27 '20 at 23:41
  • Good point, but thought about it. The result from above is [[0,0], [2.96, 1.11], [5.62, -0.58]], two first look ok, but third one is off, and I can't see any simple rotation to make it right. I added more points, but results were even more nonsense. On other thread I read that there are some other limitations to this [https://stackoverflow.com/questions/18096783/using-distance-matrix-to-find-coordinate-points-of-set-of-points](https://stackoverflow.com/questions/18096783/using-distance-matrix-to-find-coordinate-points-of-set-of-points) but these should not apply to this example I guess. – pael Sep 27 '20 at 23:47
  • 1
    It looks correct to me. The recovered distances for both `X` and `example` points are: 3.16, 5.65, 3.16. A set of points actually has *two* gauge freedoms (distances are preserved under rotation and translation) – anon01 Sep 27 '20 at 23:55
  • If you really want to find the linear transformation mapping between the point sets, you could start by finding the translation between their respective centroids. – anon01 Sep 27 '20 at 23:57
  • Ok, I see it now. I haven't check it that far, but indeed distance matrix is the same, thou I don't really understand what you wrote ;) (no need to explain, that's homework ;)). Actually this is good enough for my needs, but would you be able to suggest a solution, i.e. to apply proper rotation to obtain input coordinates as an answer? – pael Sep 28 '20 at 00:46

0 Answers0