-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmatcplx.py
239 lines (213 loc) · 5.57 KB
/
libmatcplx.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from math import *
def len_test(u,v):#compare the size of u and v
sizeu=len(u)
sizev=len(v)
if sizeu!=sizev:
raise Exception("Incompatible sizes, both vectors must be the same sizes")
return sizeu
def square_test(u):
rows=len(u)
columns=len(u[0])
if rows!=columns:
raise Exception("Unacceptable dimension, matrix must be square")
return rows
def dim_test(u,v):
rowsu=len(u)
columnsu=len(u[0])
rowsv=len(v)
columnsv=len(v[0])
if (rowsu-rowsv)+(columnsu-columnsv)!=0:
raise Exception("Incompatible dimensions, both matrices must be the same dimensions")
return rowsu,columnsu
##vector functions
def vsum(u,v):## u plus v
size=len_test(u,v)
sum_=size*[0+0j]
i=0
while i<size:
sum_[i]=u[i]+v[i]
i=i+1
return sum_
def vinv(u):## u additive inverse
size=len(u)
inv_=size*[0+0j]
i=0;
while i<size:
inv_[i]=-u[i]
i=i+1
return inv_
def vscp(c,u):## c*u, scalar vector product
size=len(u)
prod_=size*[0+0j]
i=0
while i<size:
prod_[i]=c*u[i]
i=i+1
return prod_
def vtrans(u):## transpose of vector u
return u
def vconj(u):## conjugate of vector u
size=len(u)
vconj_=size*[0+0j]
i=0
while i<size:
vconj_[i]=u[i].conjugate()
i=i+1
return vconj_
def vdagger(u):## adjoint of vector u
u_t=vtrans(u)
u_T=vconj(u_t)
return u_T
def vinnp(u,v):## inner product of vectors u and v
size=len_test(u,v)
conjv=vconj(v)
innp=0
i=0
while i<size:
innp=u[i]*conjv[i]+innp
i=i+1
return innp
def vnorm(u):##norm of u
vnorm_=sqrt(vinnp(u,u))
return vnorm_
def vdist(u,v):## distance of u and v
diff=vsus(u,v)
dist_=vnorm(diff)
return dist_
def vtprod(u,v):## vectorial tensor product
lenu=len(u)
lenv=len(v)
result=(lenu*lenv)*[0+0j]
i=0
k=0
l=0
while i<lenu:
while k<lenv:
result[l]=u[i]*v[k]
l=1+l
k=k+1
i=i+1
return result
## matrix functions
def msum(u,v):## adition of matrices u and v
size=dim_test(u, v)##########columns###################rows#####
msum_=[[0+0j for i in range (size[1])] for i in range (size[0])]
i=0
k=0
while i<size[0]:
while k<size[1]:
msum_[i][k]=u[i][k]+v[i][k]
k=k+1
i=i+1
return msum_
def minv(u):## additive inverse of matrix u
size=dim_test(u, u)##########columns###################rows#####
minv_=[[0+0j for i in range (size[1])] for i in range (size[0])]
i=0
k=0
while i<size[0]:
while k<size[1]:
minv_[i][k]=-u[i][k]
k=k+1
i=i+1
return minv_
def mscp(c,u):## matrix scalar product of c and u
size=dim_test(u, u)
mscp_=[[0+0j for i in range (size[1])] for i in range (size[0])]
i=0
k=0
while i<size[0]:
while k<size[1]:
mscp_[i][k]=c*u[i][k]
k=k+1
i=i+1
return mscp_
def mtrans(u):## transpose of matrix u
size=dim_test(u, u)
mtrans_==[[0+0j for i in range (size[0])] for i in range (size[1])]
i=0
k=0
while i<size[1]:
while k<size[0]:
mtrans_[i][k]=u[k][i]
k=k+1
i=i+1
return mtrans_
def mconj(u):## conjugate of matrix u
size=dim_test(u, u)
mconj_=[[0+0j for i in range (size[1])] for i in range (size[0])]
i=0
k=0
while i<size[0]:
while k<size[1]:
mconj_[i][k]=u[i][k].conjugate()
k=k+1
i=i+1
return mconj_
def mdagger(u):## adjoint of matrix u
size=dim_test(u, u)
mconju_=[[0+0j for i in range (size[1])] for i in range (size[0])]
mconju_=mconj(u)
mdagger_=[[0+0j for i in range (size[0])] for i in range (size[1])]
mdagger_=mtrans(mconju_)
return mdagger_
def mprod(u,v):## matrix product between u and v
sizeu=dim_test(u, u)
sizev=dim_test(v, v)
if sizeu[1]!=sizev[0]:
raise Exception("Incompatible dimensions, number of columns of u must be the same as the number of rows of v")
size=(sizeu[0],sizev[1])
result=[[0+0j for i in range (size[1])] for i in range (size[0])]
v_t=mtrans(v)
i=0
k=0
while i<size[0]:
while k<size[1]:
result[i][k]=vinnp(u[i][:], v_t[k][:])
k=k+1
i=i+1
return result
def actmov(u,v):## action of matrix u on vector v
(m,n)=dim_test(u, u)
n_=len(v)
if n!=n_:
raise Exception("Incompatible dimensions, number of columns of u must be the same as the number of elements of v")
result=m*[0+0j]
i=0
while i<m:
result[i]=vinnp(u[i][:], v[i])
i=i+1
return result
def unitm(u):## returns 1 if u is an unitary matrix, else 0
size=square_test(u)######columns#################rows#####
mI=[[0+0j for i in range (size)] for i in range (size)]
while i<size:
mI[i][i]=1
i=i+1
andjoint=mdagger(u)
uut=mprod(u, u)
if uut==mI:
return 1
else:
return 0
def hertm(u):## returns 1 if u is an hermitian matrix, else 0
size=square_test(u)
andjoint=mdagger(u)
if u==andjoint:
return 1
else:
return 0
def mtprod(u,v):## matricial tensor product
sizeu=dim_test(u, u)
sizev=dim_test(v, v)
m=sizeu[0]*sizev[0]##rows
n=sizeu[1]*sizev[1]##columns
result_=[[0+0j for i in range (n)] for i in range (m)]
i=0
k=0
while i<m:
while k<n:
result_[i][k]=u[j//sizev[0]][k//sizev[1]]*v[i%sizev[0]][k%sizev[1]]
k=k+1
i=i+1
return result_