Skip to content

Commit

Permalink
'matrix problems'
Browse files Browse the repository at this point in the history
  • Loading branch information
DPotoyan committed Nov 10, 2023
1 parent 95be7ad commit 4bed99d
Showing 1 changed file with 134 additions and 14 deletions.
148 changes: 134 additions & 14 deletions ch03/demo_operators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from ipywidgets import widgets\n",
"from ipywidgets.widgets import interact, interactive\n",
"\n",
"%matplotlib inline\n",
Expand All @@ -30,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -61,9 +62,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "39110dcd05c146da84f09e1e72557dc1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=0, description='x', max=10, min=-10), IntSlider(value=0, description='y'…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.plot_vector(x, y)>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def plot_vector(x, y):\n",
"\n",
Expand Down Expand Up @@ -91,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -112,16 +138,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "175112b533e3462e956b0409b0cc3002",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=1, description='a', max=5, min=-5), IntSlider(value=0, description='b', …"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.matrix_transform(a=1, b=0, c=0, d=1)>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def matrix_transform(a=1, b=0, c=0, d=1):\n",
" matrix = np.array([[a, b], [c, d]])\n",
Expand Down Expand Up @@ -152,6 +196,82 @@
" d=widgets.IntSlider(min=-5, max=5, value=1, description=\"d\"))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solve linear euqation by matrix inversion"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution using matrix inversion:\n",
"x = [2.42857143 0.71428571]\n"
]
}
],
"source": [
"# Define the coefficients matrix A and the right-hand side vector b\n",
"A = np.array([[2, 3],\n",
" [1, -2]])\n",
"\n",
"b = np.array([7, 1])\n",
"\n",
"# Calculate the inverse of matrix A\n",
"A_inv = np.linalg.inv(A)\n",
"\n",
"# Solve for the unknown vector x using matrix inversion: x = A_inv * b\n",
"x = np.dot(A_inv, b)\n",
"\n",
"print(\"Solution using matrix inversion:\")\n",
"print(\"x =\", x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute eigenvalues and eigenvectors"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"eigvals:\n",
"[-0.5+0.8660254j -0.5-0.8660254j]\n",
"eigvecs:\n",
"[[0.8660254+0.j 0.8660254-0.j ]\n",
" [0.4330127-0.25j 0.4330127+0.25j]]\n"
]
}
],
"source": [
"# Define the coefficients matrix A and the right-hand side vector b\n",
"# vary coeficients\n",
"A = np.array([[1, -3],\n",
" [1, -2]])\n",
"\n",
"# Calculate the eigenvalues and eigenvectors of A\n",
"eigenvalues, eigenvectors = np.linalg.eig(A)\n",
"print('eigvals:')\n",
"print(eigenvalues)\n",
"print('eigvecs:')\n",
"print(eigenvectors)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 4bed99d

Please sign in to comment.