Skip to content

Instantly share code, notes, and snippets.

@KhyatiMahendru
Last active July 23, 2019 05:16
Show Gist options
  • Save KhyatiMahendru/7bb8c7f9ffdc37a664d5e64789b36f45 to your computer and use it in GitHub Desktop.
Save KhyatiMahendru/7bb8c7f9ffdc37a664d5e64789b36f45 to your computer and use it in GitHub Desktop.
import numpy as np
from numpy.linalg import svd
# define your matrix as a 2D numpy array
A = np.array([[4, 0], [3, -5]])
U, S, VT = svd(A)
print("Left Singular Vectors:")
print(U)
print("Singular Values:")
print(np.diag(S))
print("Right Singular Vectors:")
print(VT)
# check that this is an exact decomposition
# @ is used for matrix multiplication in Py3, use np.matmul with Py2
print(U @ np.diag(S) @ VT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment