/
exercise_1.py
executable file
·134 lines (92 loc) · 3.09 KB
/
exercise_1.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
#!/usr/bin/python3
# coding: utf-8
# In[1]:
import numpy as np
import networkx as nx
# In[2]:
def linear(n):
# returns the matrix for linear polyene of length n, with alpha = 0 and beta = -1
n_int = int(n)
M = np.array([[-1 if i == j - 1 or i == j + 1 else 0 for i in range(n_int)] for j in range(n_int)])
return M
# In[3]:
def cyclic(n):
# returns the matrix for linear polyene of length n, with alpha = 0 and beta = -1
n_int = int(n)
M = np.array([[-1 if i == j - 1 or i == j + 1 else 0 for i in range(n_int)] for j in range(n_int)])
M[0,(n_int-1)] = -1
M[(n_int-1),0] = -1
return M
# In[4]:
def platonic(n): # n must be 4, 6, 8, 12 or 20
# returns the matrix for sp2 platonic solid, with alpha = 0 and beta = -1
n_int = int(n)
if n_int == 4:
s = nx.tetrahedral_graph()
elif n_int == 6:
s = nx.cubical_graph()
elif n_int == 8:
s = nx.octahedral_graph()
elif n_int == 12:
s = nx.dodecahedral_graph()
elif n_int == 20:
s = nx.icosahedral_graph()
else:
print("n must be equal to 4, 6, 8, 12 or 20")
M = - nx.adjacency_matrix(s)
return M.todense()
# In[5]:
def get_evals(Ma):
# calculates the eigenvalues for a symmetric matrix
evalss, evecs = np.linalg.eigh(Ma)
return evalss
# In[6]:
def degen(evals):
# calculates the degeneracies of each eigenvalue
test = 0
count = 0
result = []
for e in evals:
if abs(e - test) < 0.001: # threshold can be varied for accuracy
count += 1
else:
if test != 0:
result.append((round(test,3), count))
test = e
count = 1
if count > 0:
result.append((round(test,3), count))
return result
# In[ ]:
def huckel():
# this function gathers information from the user and executes the chosen tasks
repeat = "yes"
while repeat == "yes":
print("")
print("For the eigenvalues and degeneracies of a length n poly-ene, input p")
print("For the eigenvalues and degeneracies of a length n cyclic poly-ene, input c")
print("For the eigenvalues and degeneracies of a length n platonic solid, input s")
print("")
ans = input("Enter here:")
if ans == "p":
ans1 = input("Please specify n:")
lin = linear(ans1)
evals = sorted(get_evals(lin))
print(degen(evals))
elif ans == "c":
print("Please specify n")
ans1 = input("n:")
cyc = cyclic(ans1)
evals = sorted(get_evals(cyc))
print(degen(evals))
elif ans == "s":
print("Please specify n where n = 4, 6, 8, 12 or 20")
ans1 = input("n:")
pla = platonic(ans1)
evals = sorted(get_evals(pla))
print(degen(evals))
else:
print("Make sure you have used lowercase to specify your category")
print("")
repeat = input("Do you want to repeat for another molecule?")
huckel()