Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
molecular-simulation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nguyed99
molecular-simulation
Commits
21d38a97
Commit
21d38a97
authored
2 years ago
by
jung_42
Browse files
Options
Downloads
Patches
Plain Diff
Update PS7.py
parent
5f6e133b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
PS7_Jung/PS7.py
+113
-23
113 additions, 23 deletions
PS7_Jung/PS7.py
with
113 additions
and
23 deletions
PS7_Jung/PS7.py
+
113
−
23
View file @
21d38a97
...
@@ -12,42 +12,132 @@ import matplotlib.pyplot as plt
...
@@ -12,42 +12,132 @@ import matplotlib.pyplot as plt
T
=
np
.
matrix
([[
0
,
1
/
4
,
0
,
0
,
0
],
[
1
,
0
,
1
/
2
,
1
/
2
,
0
],
[
0
,
1
/
4
,
0
,
0
,
0
],
[
0
,
1
/
4
,
0
,
0
,
0
],
[
0
,
1
/
4
,
1
/
2
,
1
/
2
,
1
]])
T
=
np
.
matrix
([[
0
,
1
/
4
,
0
,
0
,
0
],
[
1
,
0
,
1
/
2
,
1
/
2
,
0
],
[
0
,
1
/
4
,
0
,
0
,
0
],
[
0
,
1
/
4
,
0
,
0
,
0
],
[
0
,
1
/
4
,
1
/
2
,
1
/
2
,
1
]])
eigenvalues
,
eigenvectors
=
np
.
linalg
.
eig
(
T
)
eigenvalues
,
eigenvectors
=
np
.
linalg
.
eig
(
T
)
# plot spectrum of T
#
# plot spectrum of T
real
=
[
eigenvalue
.
real
for
eigenvalue
in
eigenvalues
]
#
real = [eigenvalue.real for eigenvalue in eigenvalues]
imaginary
=
[
eigenvalue
.
imag
for
eigenvalue
in
eigenvalues
]
#
imaginary = [eigenvalue.imag for eigenvalue in eigenvalues]
plt
.
figure
()
#
plt.figure()
plt
.
scatter
(
real
,
imaginary
)
#
plt.scatter(real, imaginary)
plt
.
xlabel
(
'
Real
'
)
#
plt.xlabel('Real')
plt
.
ylabel
(
'
Imaginary
'
)
#
plt.ylabel('Imaginary')
plt
.
tight_layout
()
#
plt.tight_layout()
plt
.
savefig
(
'
Spectrum_cat_mouse.png
'
)
#
plt.savefig('Spectrum_cat_mouse.png')
# stationary distribution = last eigenvector
#
# stationary distribution = last eigenvector
# c)
#
# c)
av_columns
=
np
.
zeros
(
100
)
av_columns
=
np
.
zeros
(
100
)
columns_indep
=
np
.
zeros
((
5
,
100
))
columns_indep
=
np
.
zeros
((
5
,
100
))
E_k
=
np
.
zeros
(
100
)
E_k
=
np
.
zeros
(
100
)
q_1
=
np
.
array
((
1
,
0
,
0
,
0
,
0
))
print
(
f
'
{
eigenvectors
[
:
,
0
]
=
}
'
)
for
k
in
range
(
1
,
101
):
for
k
in
range
(
1
,
101
):
T_k
=
np
.
linalg
.
matrix_power
(
T
,
k
)
T_k
=
np
.
linalg
.
matrix_power
(
T
,
k
)
# L2-Norm
# L2-Norm
for
i
in
range
(
5
):
for
i
in
range
(
5
):
columns_indep
[
i
,
k
-
1
]
=
np
.
linalg
.
norm
(
eigenvectors
[
0
]
-
T_k
[:,
i
])
columns_indep
[
i
,
k
-
1
]
=
np
.
linalg
.
norm
(
eigenvectors
[
:,
0
]
-
T_k
[:,
i
])
av_columns
[
k
-
1
]
=
np
.
average
(
columns_indep
[:,
k
-
1
])
av_columns
[
k
-
1
]
=
np
.
average
(
columns_indep
[:,
k
-
1
])
print
(
f
'
{
(
T_k
.
dot
(
q_1
)
-
eigenvectors
[
:
,
0
]).
shape
=
}
'
)
E_k
[
k
-
1
]
=
np
.
linalg
.
norm
((
T_k
.
dot
(
q_1
)).
T
-
eigenvectors
[:,
0
])
q_1
=
np
.
array
((
1
,
0
,
0
,
0
,
0
))
# plt.figure()
E_k
[
k
-
1
]
=
np
.
linalg
.
norm
(
np
.
dot
(
T_k
,
q_1
)
-
eigenvectors
[
0
])
# for i in range(5):
# plt.plot(columns_indep[i,:], label=f'Column {i+1}')
# plt.plot(av_columns, label='All')
# plt.legend()
# plt.tight_layout()
# plt.savefig('Column_convergence.png')
plt
.
figure
()
# # k =1,..,9
for
i
in
range
(
5
):
plt
.
plot
(
columns_indep
[
i
,:],
label
=
f
'
Column
{
i
+
1
}
'
)
plt
.
plot
(
av_columns
,
label
=
'
All
'
)
plt
.
legend
()
plt
.
show
()
plt
.
figure
()
plt
.
figure
()
plt
.
plot
(
np
.
log
(
E_k
))
plt
.
plot
(
list
(
range
(
1
,
101
)),
np
.
log
(
E_k
),
'
o
'
)
plt
.
show
()
plt
.
xlim
(
1
,
101
)
plt
.
xlabel
(
'
k
'
)
plt
.
ylabel
(
'
$log(E_k)$
'
)
plt
.
tight_layout
()
plt
.
savefig
(
'
For_slope.png
'
)
## Problem 7.2
# a)
# T = np.matrix([[0,0,0,1], [1,0,0,0], [0,1,0,0], [0,0,1,0]])
# eigenvalues, eigenvectors = np.linalg.eig(T)
# real = [eigenvalue.real for eigenvalue in eigenvalues]
# imaginary = [eigenvalue.imag for eigenvalue in eigenvalues]
# plt.figure()
# plt.scatter(real, imaginary)
# plt.xlabel('Real')
# plt.ylabel('Imaginary')
# plt.tight_layout()
# plt.savefig('Spectrum_stoch_mat_A.png')
# b)
# T = np.matrix([[0,0,0,1], [1/3,0,0,0], [1/3,1,0,0], [1/3,0,1,0]])
# eigenvalues, eigenvectors = np.linalg.eig(T)
# real = [eigenvalue.real for eigenvalue in eigenvalues]
# imaginary = [eigenvalue.imag for eigenvalue in eigenvalues]
# plt.figure()
# plt.scatter(real, imaginary)
# plt.xlabel('Real')
# plt.ylabel('Imaginary')
# plt.tight_layout()
# plt.savefig('Spectrum_stoch_mat_B.png')
# c)
# T = np.matrix([[0,0,0,1], [1/2,0,0,0], [0,1,0,0], [1/2,0,1,0]])
# # convergence
# p_0_1 = np.array([1,0,1,0])
# p_0_2 = np.array([1,0,0,0])
# p_0_3 = np.array([0,0,1,0])
# p_0_4 = np.array([0,1,0,0])
# p_0_5 = np.array([0,0,0,1])
# k = 100
# T_k = np.linalg.matrix_power(T, k)
# T_k_1 = np.linalg.matrix_power(T, k+1)
# norm_0_1 = np.zeros(100)
# norm_0_2 = np.zeros(100)
# norm_0_3 = np.zeros(100)
# norm_0_4 = np.zeros(100)
# norm_0_5 = np.zeros(100)
# for k in range(1,101):
# T_k = np.linalg.matrix_power(T, k)
# T_k_1 = np.linalg.matrix_power(T, k+1)
# # norm_0_1[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_1)-np.dot(T_k,p_0_1))
# norm_0_2[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_2)-np.dot(T_k,p_0_2))
# norm_0_3[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_3)-np.dot(T_k,p_0_3))
# norm_0_4[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_4)-np.dot(T_k,p_0_4))
# norm_0_5[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_5)-np.dot(T_k,p_0_5))
# plt.figure()
# # plt.plot(norm_0_1, label='1')
# plt.plot(norm_0_2, label='2')
# plt.plot(norm_0_3, label='3')
# plt.plot(norm_0_4, label='4')
# plt.plot(norm_0_5, label='5')
# plt.legend()
# plt.show()
# # divergence
# p_0_1 = np.array([1,1,0,0])
# p_0_2 = np.array([1,1,1,1])
# k = 100
# T_k = np.linalg.matrix_power(T, k)
# T_k_1 = np.linalg.matrix_power(T, k+1)
# norm_0_1 = np.zeros(100)
# norm_0_2 = np.zeros(100)
# norm_0_3 = np.zeros(100)
# for k in range(1,101):
# T_k = np.linalg.matrix_power(T, k)
# T_k_1 = np.linalg.matrix_power(T, k+1)
# norm_0_1[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_1)-np.dot(T_k,p_0_1))
# norm_0_2[k-1] = np.linalg.norm(np.dot(T_k_1,p_0_2)-np.dot(T_k,p_0_2))
# plt.figure()
# plt.plot(norm_0_1, label='1')
# plt.plot(norm_0_2, label='2')
# plt.legend()
# plt.show()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment