-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinalg.py
More file actions
65 lines (64 loc) · 1.2 KB
/
linalg.py
File metadata and controls
65 lines (64 loc) · 1.2 KB
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
# %%
import numpy.linalg as np_linalg
import scipy.linalg as sp_linalg
import numpy as np
# %%
# Ax = b
A = np.array([
[1,0,0],
[0,1,0],
[0,0,2],
])
b= np.array([1,0,1])
# %%
for i in range (A.shape[0]):
terms = []
for j in range(A.shape[1]):
terms.append("{1} x[{0}]".format(j, A[j,i]))
print(" + ".join(terms), "=", b[i])
# %%
x = np_linalg.solve(A,b)
print(x)
# %%
np.dot(A,x) -b
# %%
xnew = sp_linalg.inv(A).dot(b)
print(xnew)
# %%
Ainv = sp_linalg.inv(A)
# %%
A.dot(Ainv)
# %%
Ainv.dot(A)
# %%
A.dot(np.linalg.inv(A))
# %%
# A x_i = lambda_i x_i
# I omega_i = lambda_i omega_i
Isquare = np.array([[2/3, -1/4], [-1/4, 2/3]])
lambdas, omegas = np_linalg.eig(Isquare)
# %%
print(lambdas, omegas)
# %%
# eigenvectors are omegas[:,i]
# transpose
omegas.T
# %%
# test: (I- lambdas*1)omegas = 0 ?
(Isquare - lambdas[0]*np.identity(2)).dot(omegas[:,0])
# %%
(Isquare - lambdas[0]*np.identity(2)).dot(omegas.T[0])
# %%
(Isquare - lambdas[1]*np.identity(2)).dot(omegas.T[1])
# %%
sigma_y = np.array([[0,-1j],[1j,0]])
E, chis = np.linalg.eig(sigma_y)
print(E)
print(chis.T)
# %%
chi1 = chis.T[0]
print(chi1)
norm = np.dot(chi1.conjugate(),chi1)
chi1hat = chi1/np.sqrt(norm)
print(chi1hat)
# %%