scipy.linalg.svd - Singular Value Decomposition using SciPycf. scip.linalg.svd
Factorizes the matrix a into 2 unitary matrices U and Vh, and a 1-d. array s of singular values (real, non-negative) such that a == U*S*Vh, where S is a suitably shaped matrix of zeros with main diagonal s.
In [1]:
import numpy as np
In [2]:
from scipy import linalg
In [4]:
# Create an array of the given shape and populate it with
# random samples from a uniform distribution
# over ``[0, 1)``.
a = np.random.randn(9,6) + 1.j * np.random.randn(9,6)
In [5]:
a
Out[5]:
In [6]:
U, s, Vh = linalg.svd(a)
In [7]:
U.shape, Vh.shape, s.shape
Out[7]:
In [8]:
U
Out[8]:
In [9]:
Vh
Out[9]:
In [10]:
s
Out[10]:
full_matrices : bool, optional - if True, U and Vh are of shape (M,M), (N,N). If False, the shapes are (M,K) and (K,N) where K = min(M,N)
In [11]:
U,s,Vh=linalg.svd(a,full_matrices=False)
In [12]:
U.shape, Vh.shape, s.shape
Out[12]:
In [13]:
S=linalg.diagsvd(s,6,6) # Construct the Sigma matrix S, given the vector s, of a == U*S*Vh
In [14]:
S
Out[14]:
In [15]:
np.allclose(a,np.dot(U,np.dot(S,Vh)))
Out[15]:
compute_uv : bool, optional Whether to compute also U and Vh in addition to s. Default is True.
In [16]:
s2 = linalg.svd(a,compute_uv=False)
In [17]:
np.allclose(s,s2)
Out[17]:
cuSOLVER in CUDA Toolkit Documentation
In [3]:
A_gold = np.array( [[1.0,2.0], [4.0,5.0],[2.,1.0]])
In [5]:
UsVh_gold = linalg.svd(A_gold)
In [6]:
UsVh_gold[0]
Out[6]:
In [7]:
linalg.diagsvd(UsVh_gold[1],2,2)
Out[7]:
In [8]:
UsVh_gold[2]
Out[8]:
In [5]:
A_6_241_04 = np.array([[100,100],[100.2,100]])
A_6_241_04
Out[5]:
In [7]:
UsVh_6_241_04 = linalg.svd(A_6_241_04)
In [8]:
UsVh_6_241_04[0]
Out[8]:
In [9]:
linalg.diagsvd( UsVh_6_241_04[1], 2,2)
Out[9]:
In [10]:
UsVh_6_241_04[2]
Out[10]:
In [11]:
A33 = np.array([[-149 ,-50, -154 ],[537 ,180, 546 ],[-27 ,-9, -25 ]])
In [13]:
UsVh_33 = linalg.svd(A33)
In [14]:
UsVh_33[0]
Out[14]:
In [15]:
linalg.diagsvd( UsVh_33[1], 3,3)
Out[15]:
In [16]:
UsVh_33[2]
Out[16]:
Let $d=2$ (dimension of the state space, for each, say, spin system, $L=2$ (number of sites; should be an even number)
In [52]:
d=2
L=2
Psi_d2_L2 = np.random.randn(d**(L-1),d) + 1.j * np.random.randn(d**(L-1),d)
In [53]:
print(Psi_d2_L2.shape)
Psi_d2_L2
Out[53]:
In [54]:
UsVh_d2_L2 = linalg.svd(Psi_d2_L2)
In [55]:
print(UsVh_d2_L2[0].shape)
UsVh_d2_L2[0]
Out[55]:
In [56]:
UsVh_d2_L2[1]
Out[56]:
In [57]:
linalg.diagsvd(UsVh_d2_L2[1],2,2)
Out[57]:
In [58]:
print(UsVh_d2_L2[2].shape)
UsVh_d2_L2[2]
Out[58]:
Calculate US and reshape
In [59]:
US_d2_L2 = np.dot( UsVh_d2_L2[0] , linalg.diagsvd(UsVh_d2_L2[1],2,2) )
print(US_d2_L2.shape)
US_d2_L2
Out[59]:
In [63]:
# new matrix after 1 iteration, l=1
Psi_d2_L2_l1 = US_d2_L2.reshape(d**(L-(1+1)),d*2)
print(Psi_d2_L2_l1.shape)
Psi_d2_L2_l1
Out[63]:
Calculate the right normalized matrices $B$'s which are only columns of Vh in this case:
In [64]:
B0_d2_L2=UsVh_d2_L2[2][:,0]
B1_d2_L2=UsVh_d2_L2[2][:,1]
print(B0_d2_L2)
print(B1_d2_L2)
In [33]:
M=4
N=2
ind_RR = 1
ind_CC = 0.1
A_CC=[]
for row in range(M):
A_row =[]
for col in range(N):
A_val = ind_RR * (row+1 + M*col) + ind_CC*(row+1 + M*col)*1j
A_row.append(A_val)
A_CC.append(A_row)
In [34]:
A_CC = np.array(A_CC)
A_CC
Out[34]:
In [35]:
UsVh_CC = linalg.svd(A_CC)
In [36]:
print(UsVh_CC[0].shape)
UsVh_CC[0]
Out[36]:
In [37]:
print(UsVh_CC[1].shape)
linalg.diagsvd(UsVh_CC[1],2,2)
Out[37]:
In [38]:
print(UsVh_CC[2].shape)
UsVh_CC[2]
Out[38]:
In [82]:
d=2
L=4
Psi_d2_L4 = np.random.randn(d**(L-1),d) + 1.j * np.random.randn(d**(L-1),d)
In [83]:
print(Psi_d2_L4.shape)
Psi_d2_L4
Out[83]:
In [84]:
UsVh_d2_L4 = linalg.svd(Psi_d2_L4)
In [85]:
print(UsVh_d2_L4[0].shape)
UsVh_d2_L4[0]
Out[85]:
In [86]:
UsVh_d2_L4[1]
Out[86]:
In [87]:
print(UsVh_d2_L4[2].shape)
UsVh_d2_L4[2]
Out[87]:
In [88]:
US_d2_L4 = np.dot( UsVh_d2_L4[0] , linalg.diagsvd(UsVh_d2_L4[1], UsVh_d2_L4[0].shape[0],d) )
print(US_d2_L4.shape)
US_d2_L4
Out[88]:
In [89]:
# after iteration l = 1, we obtain a new matrix \Psi to apply SVD on
l=1
Psi_d2_L4_l1 = US_d2_L4.reshape(d**(L-(l+1)), d*2 )
print(Psi_d2_L4_l1.shape )
In [90]:
UsVh_d2_L4_l2 = linalg.svd(Psi_d2_L4_l1)
In [91]:
print(UsVh_d2_L4_l2[0].shape)
UsVh_d2_L4_l2[0]
Out[91]:
In [92]:
print(UsVh_d2_L4_l2[1].shape)
UsVh_d2_L4[1]
Out[92]:
In [93]:
print(UsVh_d2_L4_l2[2].shape)
UsVh_d2_L4_l2[2]
Out[93]:
In [94]:
US_d2_L4_l2 = np.dot( UsVh_d2_L4_l2[0] , linalg.diagsvd(UsVh_d2_L4_l2[1], UsVh_d2_L4_l2[0].shape[1],UsVh_d2_L4_l2[2].shape[0]) )
print(US_d2_L4_l2.shape)
US_d2_L4_l2
Out[94]:
In [97]:
# after iteration l = 2, we obtain a new matrix \Psi to apply SVD on
l=2
Psi_d2_L4_l2 = US_d2_L4_l2.reshape(d**(L-(l+1)), d* US_d2_L4_l2.shape[1] )
print(Psi_d2_L4_l2.shape )
In [98]:
UsVh_d2_L4_l3 = linalg.svd(Psi_d2_L4_l2)
In [99]:
print(UsVh_d2_L4_l3[0].shape)
UsVh_d2_L4_l3[0]
Out[99]:
In [96]:
L-(l+1)
Out[96]:
In [60]:
d=2
L=2
In [3]:
def create_fixed_CC_mat(d,L):
totalsysstates = d**(L-1)
A = []
for i in range(totalsysstates):
ithstate = []
f = i*(0.9/totalsysstates)+0.1
theta_f = 2.*np.arccos(-1.)*f
d0 = f*(np.cos( theta_f) + np.sin(theta_f)*1j)
d1 = (1.-f)*(np.sin( theta_f) + np.cos(theta_f)*1j)
ithstate=[d0,d1]
A.append(ithstate)
return np.array(A)
In [66]:
A_CC_d2L2=create_fixed_CC_mat(d,L)
print(A_CC_d2L2.shape)
print(A_CC_d2L2)
In [67]:
UsVh_CC_d2L2 = linalg.svd(A_CC_d2L2)
In [68]:
print(UsVh_CC_d2L2[0].shape)
print(UsVh_CC_d2L2[0])
In [69]:
print(UsVh_CC_d2L2[1].shape)
print(linalg.diagsvd(UsVh_CC_d2L2[1],2,2 ) )
In [70]:
print(UsVh_CC_d2L2[2].shape)
print(UsVh_CC_d2L2[2])
In [71]:
Psi_new_CC_d2L2 = np.dot( UsVh_CC_d2L2[0], linalg.diagsvd(UsVh_CC_d2L2[1],2,2 ) )
print(Psi_new_CC_d2L2)
Psi_new_CC_d2L2 = Psi_new_CC_d2L2.reshape(1,4)
print(Psi_new_CC_d2L2)
In [4]:
d=2
L=4
A_CC_d2L4=create_fixed_CC_mat(d,L)
print(A_CC_d2L4.shape)
print(A_CC_d2L4)
In [5]:
UsVh_CC_d2L4 = linalg.svd(A_CC_d2L4)
In [6]:
print(UsVh_CC_d2L4[0].shape)
print(UsVh_CC_d2L4[0])
In [7]:
print(UsVh_CC_d2L4[1].shape)
print(linalg.diagsvd(UsVh_CC_d2L4[1],2,2 ) )
In [8]:
print(UsVh_CC_d2L4[2].shape)
print(UsVh_CC_d2L4[2])
In [18]:
Psi_new_CC_d2L4 = np.dot( UsVh_CC_d2L4[0], linalg.diagsvd(UsVh_CC_d2L4[1],8,2 ) )
print(Psi_new_CC_d2L4)
Psi_new_CC_d2L4 = Psi_new_CC_d2L4.reshape( (2**(L-2),d*d),order='F')
print(Psi_new_CC_d2L4)
In [19]:
UsVh_CC_d2L4l01 = linalg.svd(Psi_new_CC_d2L4)
In [20]:
print(UsVh_CC_d2L4l01[0].shape)
print(UsVh_CC_d2L4l01[0])
In [21]:
print(UsVh_CC_d2L4l01[1].shape)
print(linalg.diagsvd(UsVh_CC_d2L4l01[1],4,4 ) )
In [22]:
print(UsVh_CC_d2L4l01[2].shape)
print(UsVh_CC_d2L4l01[2])
In [26]:
np.dot( UsVh_CC_d2L4l01[0], linalg.diagsvd(UsVh_CC_d2L4l01[1],4,4 ) )
Out[26]:
In [24]:
Psi_new_CC_d2L4l01 = np.dot( UsVh_CC_d2L4l01[0], linalg.diagsvd(UsVh_CC_d2L4l01[1],4,4 ) )
print(Psi_new_CC_d2L4l01)
Psi_new_CC_d2L4l01 = Psi_new_CC_d2L4l01.reshape( (2**(L-3),d*d*d),order='F')
print(Psi_new_CC_d2L4l01)
In [100]:
d=2
L=2
In [101]:
d**L
Out[101]:
In [102]:
d**(L-1)
Out[102]:
In [103]:
d**(L/2)
Out[103]:
In [104]:
L/2
Out[104]:
In [107]:
[l+1 for l in range(L/2)]
Out[107]:
In [108]:
range(1,2)
Out[108]:
In [109]:
def calculate_dims(d,L):
results = []
result_1 = []
result_1.append( (d**(L-1),d) )
r_1 = min( result_1[0][0], result_1[0][1])
newPsidim = ( result_1[0][0]/d, d*r_1)
result_1.append( newPsidim )
newBdim = ( r_1, 1)
result_1.append( newBdim )
results.append( result_1 )
for l in range(2,L+1):
result_l = []
r_previous = results[l-2][2][0]
result_l.append( (d**(L-l) , d*r_previous))
r_l = min( result_l[0][0], result_l[0][1])
newPsidim = ( result_l[0][0]/d , d*r_l)
result_l.append( newPsidim )
newBdim = ( r_l, r_previous)
result_l.append( newBdim)
results.append( result_l )
return results
In [110]:
results_d2_L2 = calculate_dims(2,2)
In [111]:
results_d2_L2
Out[111]:
In [112]:
results_d2_L4 = calculate_dims(2,4)
In [113]:
results_d2_L4
Out[113]:
In [64]:
d**L
Out[64]:
In [ ]: