Skip to content

Commit 29fcf74

Browse files
committed
'eig vs eigh done'
1 parent 9abbc2d commit 29fcf74

File tree

1 file changed

+9
-53
lines changed

1 file changed

+9
-53
lines changed

ch06/lin_var.ipynb

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
},
203203
{
204204
"cell_type": "code",
205-
"execution_count": 1,
205+
"execution_count": 17,
206206
"metadata": {
207207
"tags": [
208208
"hide-input"
@@ -213,73 +213,29 @@
213213
"name": "stdout",
214214
"output_type": "stream",
215215
"text": [
216-
"Eigenvalues: [ 4.93487481 51.06512519]\n",
217-
"First eigenvector: [[-0.66168489]\n",
218-
" [-0.74978204]]\n"
216+
"Eigenvalues: [ 4.93487481+0.j 51.06512519+0.j]\n",
217+
"First eigenvector: [-0.66168489 -0.74978204]\n"
219218
]
220219
}
221220
],
222221
"source": [
223-
"# to code this up we are going to set a=1\n",
224222
"import numpy as np\n",
223+
"from scipy.linalg import eig, inv, eigh\n",
224+
"\n",
225+
"# Lets adopt simpler units\n",
225226
"a = 1.0\n",
226227
"hbar = 1.0\n",
227228
"m = 1.0\n",
228229
"\n",
229-
"S = np.matrix([[1.0/30.0, 1.0/140.0],[1.0/140.0,1.0/630.0]])\n",
230-
"H = np.matrix([[1.0/6.0,1.0/30.0],[1.0/30.0,1.0/105.0]])\n",
230+
"S = np.array([[1.0/30.0, 1.0/140.0],[1.0/140.0,1.0/630.0]])\n",
231+
"H = np.array([[1.0/6.0,1.0/30.0],[1.0/30.0,1.0/105.0]])\n",
231232
"\n",
232-
"e,v = np.linalg.eig(np.dot(np.linalg.inv(S),H))\n",
233+
"e, v = eig(inv(S) @ H) #e, v = eigh(H, S) #Same values eigh is for hermitian matrices\n",
233234
"\n",
234235
"print(\"Eigenvalues:\", e)\n",
235236
"print(\"First eigenvector:\", v[:,0])"
236237
]
237238
},
238-
{
239-
"cell_type": "code",
240-
"execution_count": 2,
241-
"metadata": {
242-
"tags": [
243-
"hide-input"
244-
]
245-
},
246-
"outputs": [
247-
{
248-
"name": "stdout",
249-
"output_type": "stream",
250-
"text": [
251-
"S^{-1} = [[ 840. -3780.]\n",
252-
" [-3780. 17640.]]\n",
253-
"S^{-1}H = [[ 14. -8.]\n",
254-
" [-42. 42.]]\n"
255-
]
256-
}
257-
],
258-
"source": [
259-
"print(\"S^{-1} = \", np.linalg.inv(S))\n",
260-
"print(\"S^{-1}H = \", np.dot(np.linalg.inv(S),H))"
261-
]
262-
},
263-
{
264-
"cell_type": "code",
265-
"execution_count": 3,
266-
"metadata": {},
267-
"outputs": [
268-
{
269-
"data": {
270-
"text/plain": [
271-
"array([51.06512519, 4.93487481])"
272-
]
273-
},
274-
"execution_count": 3,
275-
"metadata": {},
276-
"output_type": "execute_result"
277-
}
278-
],
279-
"source": [
280-
"np.roots([1,-56,14*42-42*8])"
281-
]
282-
},
283239
{
284240
"cell_type": "markdown",
285241
"metadata": {},

0 commit comments

Comments
 (0)