L = k(X, .7)
D = diag(L)
M = inv(D).dot(L)
# Mi,j denotes the transition probability
# from the point xi to the point xj in one time step
print M

"""
Explanation: $
M = D^{-1}L
$
End of explanation
"""


L/L.sum(axis=1).reshape(-1,1)

"""
Explanation: <p>Equivalente a</p>
$
M_{i,j}=\frac{k_\epsilon(x_i, x_j)}{p_\epsilon(x_j)}
$
<p>onde</p>
$
p_\epsilon(x_j) = \sum_i k_\epsilon(x_i, x_j)
$
End of explanation
"""


Ms = (diag(D,.5)).dot(M).dot(diag(D,-.5))
Ms

"""
Explanation: $
Ms = D^{1/2}LD^{-1/2}
$
End of explanation
"""


p = L.sum(axis=1)
for i in range(0,3):
    a = []
    for j in range(0,3):
        a.append(L[i,j]/(p[i]*p[j])**.5)
    print a

"""
Explanation: Equivalente a
$
Ms = \frac{L_{i,j}}{(d(x_i) \times d(x_j))^{1/2}}
$
End of explanation
"""


w, v0, v1 = eig(Ms, left=True)
w = w.real
print '%s\n%s' % (w, v0)

"""
Explanation: 
End of explanation
"""


Ms.dot(v0)

w * v0

"""
Explanation: $
P \times \psi_l = \lambda_l \times \psi_l
$
End of explanation
"""


w = w[::-1]

phi = v0.T
phi = phi[::-1]
#print w, '\n', phi
print w, '\n', phi

psi = []
for i in range(3):
    psi.append([])
    for j in range(2):
        psi[i].append(phi[j+1,i]/M[i])
psi

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for p in psi:
    ax.scatter(p[0][0],p[0][1],p[0][2])
    ax.scatter(p[1][0],p[1][1],p[1][2])
    #print p[0][0], p[0][1], p[0][2]

"""
Explanation: Implementação
* https://github.com/petermuehlbacher/diffusion-maps-algorithm/blob/b4e91352459b2c4e6b0d3358b5b3e4040762d9c5/diffusion%20maps.py
End of explanation
"""


l = w.real[::-1]
print l
psi = v0.T[::-1]
print psi

phi = []
for i in range(3):
    phi.append(l[i] * psi[i])
phi

pairwise_distances(phi[1:])**2

"""
Explanation: $$
D_t(x,y) = (\sum_{l \geq 1} \lambda_t^{2t} (\psi_l(x) - \psi_l(y))^2)^{1/2}
$$
End of explanation
"""


from sklearn.preprocessing import normalize

X = np.array([[ 1., -1.,  2.]])
normalize(X, norm='l2')

np.sqrt((X * X).sum())

"""
Explanation: 
End of explanation
"""


