# SVD decomposition and original matrix are not equal [closed]

When l compute the SVD of my matrix x as defined in kernel_hist_to_SVD(). The resulted decomposition is not equal (approximatively) to the original matrix k. Here is my code.

def histogramIntersection(M, N):
m = M.shape[0]
n = N.shape[0]
result = np.zeros((m,n))
for i in range(m):
for j in range(n):
temp = np.sum(np.minimum(M[i], N[j]))
result[i][j] = temp
return result

def kernel_hist_to_SVD():
n=200
d=2000
x = np.random.rand(n, d)
x=np.matrix(x)
K=histogramIntersection(x,x)
Phi, Lambda, PhiT=linalg.svd(K)
ILambda=np.zeros(x.shape[0],x.shape[0])
for i in np.arange(x.shape[0]):
ILambda[i,i]= np.sqrt(Lambda[i])
X2=Phi*ILambda
X2=np.dot(X2, X2)
y=linalg.norm(K-X2) # It is supposed to tend to 0. k approximately equals to X2


In y=linalg.norm(K-X2) l'm supposed to get a number which tends to 0, meaning that K approximately equal X2, however y is a very large number.

What's wrong around ?

EDIT-1

def kernel_hist_to_SVD():
n=200
d=2000
x = np.random.rand(n, d)
x=np.matrix(x)
K=histogramIntersection(x,x)
U, Sigma, V=linalg.svd(K)
# Testing
X2=np.dot(U, np.dot(Sigma, V))
np.allclose(K,X2)

• do you want us to debug your code? Commented Dec 18, 2017 at 16:52
• You might do better on a Python specific site. Commented Dec 18, 2017 at 16:56
• It is neither a debugging code nor a python problem Commented Dec 18, 2017 at 16:59
• If it's not a debugging nor python problem, would you mind explaining how your code works for those of us that don't use python? Otherwise, please submit it to a python-specific site as @mdewey suggested. Commented Dec 18, 2017 at 17:25

In your code you are assuming that the unitary matrices $U,V$ in the decomposition $K=U \Sigma V^{*}$ are equal. You should be testing with

X2 = np.dot(U, np.dot(Sigma, V))


(I used U,V instead of the misleading Phi, PhiT).

SVD is not Jordan decomposition. In particular, the dimensions of the square matrices $U,V$ can be different.

• Thank you @Miguel. Please see my update. Not sure to understand the way of testing. Look at my updated function Commented Dec 18, 2017 at 18:13
• Ooops, sorry, I meant to use Sigma, not K for testing! Commented Dec 18, 2017 at 18:14
• Sigma is the lambda in my case. Let me update that Commented Dec 18, 2017 at 18:17
• So we compare np.dot(U, np.dot(Sigma, V)) with K (original matrix) using np.allclose(K,np.dot(U, np.dot(Sigma, V)) ) ? Commented Dec 18, 2017 at 18:19