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!