-
Notifications
You must be signed in to change notification settings - Fork 0
/
svd_test.py
39 lines (30 loc) · 881 Bytes
/
svd_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding=utf-8
from __future__ import absolute_import, division, print_function
import numpy as np
def svd(A): # 奇异值分解
ATA = np.matmul(A.T, A)
eigen_values, eigen_vector = np.linalg.eig(ATA)
sigma = np.sqrt(eigen_values)
sort_id = np.argsort(-sigma)
sigma = sigma[sort_id]
V = eigen_vector[:, sort_id]
VT = V.T
S = np.diag(sigma)
_S = np.linalg.inv(S)
U = np.matmul(np.matmul(A, V), _S)
result = np.matmul(np.matmul(U, S), VT)
print('U=\n', U)
print('\nS=\n', S)
print('\nV=\n', V)
print('\nA=\n', result)
print('\nVT=\n', VT)
return U, sigma, VT
# A = np.array([[5, 5],[-1, 7]])
A = np.array([[1, 1], [1, 1], [0,0]])
# A = np.array([[4, 0], [3, -5]])
if __name__ == '__main__':
svd(A)
[U, S, V] = np.linalg.svd(A)
print('\n_U=\n', U)
print('\n_S=\n', S)
print('\n_V=\n', V)