Skip to content

Commit 4bed99d

Browse files
committed
'matrix problems'
1 parent 95be7ad commit 4bed99d

File tree

1 file changed

+134
-14
lines changed

1 file changed

+134
-14
lines changed

ch03/demo_operators.ipynb

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": null,
12+
"execution_count": 7,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
1616
"import numpy as np\n",
1717
"import matplotlib.pyplot as plt\n",
18+
"from ipywidgets import widgets\n",
1819
"from ipywidgets.widgets import interact, interactive\n",
1920
"\n",
2021
"%matplotlib inline\n",
@@ -30,7 +31,7 @@
3031
},
3132
{
3233
"cell_type": "code",
33-
"execution_count": null,
34+
"execution_count": 2,
3435
"metadata": {},
3536
"outputs": [],
3637
"source": [
@@ -61,9 +62,34 @@
6162
},
6263
{
6364
"cell_type": "code",
64-
"execution_count": null,
65+
"execution_count": 3,
6566
"metadata": {},
66-
"outputs": [],
67+
"outputs": [
68+
{
69+
"data": {
70+
"application/vnd.jupyter.widget-view+json": {
71+
"model_id": "39110dcd05c146da84f09e1e72557dc1",
72+
"version_major": 2,
73+
"version_minor": 0
74+
},
75+
"text/plain": [
76+
"interactive(children=(IntSlider(value=0, description='x', max=10, min=-10), IntSlider(value=0, description='y'…"
77+
]
78+
},
79+
"metadata": {},
80+
"output_type": "display_data"
81+
},
82+
{
83+
"data": {
84+
"text/plain": [
85+
"<function __main__.plot_vector(x, y)>"
86+
]
87+
},
88+
"execution_count": 3,
89+
"metadata": {},
90+
"output_type": "execute_result"
91+
}
92+
],
6793
"source": [
6894
"def plot_vector(x, y):\n",
6995
"\n",
@@ -91,7 +117,7 @@
91117
},
92118
{
93119
"cell_type": "code",
94-
"execution_count": null,
120+
"execution_count": 4,
95121
"metadata": {},
96122
"outputs": [],
97123
"source": [
@@ -112,16 +138,34 @@
112138
},
113139
{
114140
"cell_type": "code",
115-
"execution_count": null,
116-
"metadata": {},
117-
"outputs": [],
118-
"source": []
119-
},
120-
{
121-
"cell_type": "code",
122-
"execution_count": 3,
141+
"execution_count": 8,
123142
"metadata": {},
124-
"outputs": [],
143+
"outputs": [
144+
{
145+
"data": {
146+
"application/vnd.jupyter.widget-view+json": {
147+
"model_id": "175112b533e3462e956b0409b0cc3002",
148+
"version_major": 2,
149+
"version_minor": 0
150+
},
151+
"text/plain": [
152+
"interactive(children=(IntSlider(value=1, description='a', max=5, min=-5), IntSlider(value=0, description='b', …"
153+
]
154+
},
155+
"metadata": {},
156+
"output_type": "display_data"
157+
},
158+
{
159+
"data": {
160+
"text/plain": [
161+
"<function __main__.matrix_transform(a=1, b=0, c=0, d=1)>"
162+
]
163+
},
164+
"execution_count": 8,
165+
"metadata": {},
166+
"output_type": "execute_result"
167+
}
168+
],
125169
"source": [
126170
"def matrix_transform(a=1, b=0, c=0, d=1):\n",
127171
" matrix = np.array([[a, b], [c, d]])\n",
@@ -152,6 +196,82 @@
152196
" d=widgets.IntSlider(min=-5, max=5, value=1, description=\"d\"))\n"
153197
]
154198
},
199+
{
200+
"cell_type": "markdown",
201+
"metadata": {},
202+
"source": [
203+
"### Solve linear euqation by matrix inversion"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 9,
209+
"metadata": {},
210+
"outputs": [
211+
{
212+
"name": "stdout",
213+
"output_type": "stream",
214+
"text": [
215+
"Solution using matrix inversion:\n",
216+
"x = [2.42857143 0.71428571]\n"
217+
]
218+
}
219+
],
220+
"source": [
221+
"# Define the coefficients matrix A and the right-hand side vector b\n",
222+
"A = np.array([[2, 3],\n",
223+
" [1, -2]])\n",
224+
"\n",
225+
"b = np.array([7, 1])\n",
226+
"\n",
227+
"# Calculate the inverse of matrix A\n",
228+
"A_inv = np.linalg.inv(A)\n",
229+
"\n",
230+
"# Solve for the unknown vector x using matrix inversion: x = A_inv * b\n",
231+
"x = np.dot(A_inv, b)\n",
232+
"\n",
233+
"print(\"Solution using matrix inversion:\")\n",
234+
"print(\"x =\", x)"
235+
]
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"metadata": {},
240+
"source": [
241+
"### Compute eigenvalues and eigenvectors"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 21,
247+
"metadata": {},
248+
"outputs": [
249+
{
250+
"name": "stdout",
251+
"output_type": "stream",
252+
"text": [
253+
"eigvals:\n",
254+
"[-0.5+0.8660254j -0.5-0.8660254j]\n",
255+
"eigvecs:\n",
256+
"[[0.8660254+0.j 0.8660254-0.j ]\n",
257+
" [0.4330127-0.25j 0.4330127+0.25j]]\n"
258+
]
259+
}
260+
],
261+
"source": [
262+
"# Define the coefficients matrix A and the right-hand side vector b\n",
263+
"# vary coeficients\n",
264+
"A = np.array([[1, -3],\n",
265+
" [1, -2]])\n",
266+
"\n",
267+
"# Calculate the eigenvalues and eigenvectors of A\n",
268+
"eigenvalues, eigenvectors = np.linalg.eig(A)\n",
269+
"print('eigvals:')\n",
270+
"print(eigenvalues)\n",
271+
"print('eigvecs:')\n",
272+
"print(eigenvectors)"
273+
]
274+
},
155275
{
156276
"cell_type": "code",
157277
"execution_count": null,

0 commit comments

Comments
 (0)