0

Hello everyone i am trying to compute the SVD of a matrix with numpy but i get wrong result.

I have this matrix

X = [[ 0  0  8]
    [ 0 23 25]
    [ 0  0  0]]

I am going through this code:

U, D, VT = np.linalg.svd(X, full_matrices=False)
X_remake = (U @ np.diag(D ) @ VT)

But the result that i get is this which is obviously wrong:

X_remake = ([[0.00000000e+00, 1.13509861e-15, 8.00000000e+00],
            [0.00000000e+00, 2.30000000e+01, 2.50000000e+01],
            [0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
Lefteris Kyprianou
  • 219
  • 1
  • 3
  • 14

1 Answers1

0

You can use the below snippet code to rebuild the input array as follow:

X = [[ 0 , 0 , 8]   , [ 0,23, 25]  ,  [ 0,  0,  0]]
U, D, VT = np.linalg.svd(X, full_matrices=False)

D=np.diag(D)    
X_remake=np.dot(U, np.dot(D, VT))    
print(X_remake)

# array([[0.00000000e+00, 1.19457349e-15, 8.00000000e+00],
#        [0.00000000e+00, 2.30000000e+01, 2.50000000e+01],
#        [0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
  • Users find it difficult to understand code only answers with no explanation. Please add some description explaining what is does and how it solves the problem or add comments in the source code at appropriate places. – Azhar Khan Jan 27 '23 at 05:17