Skip to content

Commit d8ca39c

Browse files
authored
Add files via upload
1 parent 95c9b1c commit d8ca39c

7 files changed

+1055
-0
lines changed

LA_05_01_01.ipynb

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 05\n",
9+
"\n",
10+
"# 2 x 2矩阵求逆\n",
11+
"《线性代数》 | 鸢尾花书:数学不难"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
17+
"metadata": {},
18+
"source": [
19+
"这段代码的主要目标是计算一个 $2 \\times 2$ 矩阵的逆矩阵。假设我们有矩阵:\n",
20+
"\n",
21+
"$$\n",
22+
"A = \\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix}\n",
23+
"$$\n",
24+
"\n",
25+
"要计算 $A$ 的逆矩阵 $A^{-1}$,首先需要计算矩阵的 **行列式**(determinant)。行列式是一个标量值,它用于判断矩阵是否可逆,计算公式如下:\n",
26+
"\n",
27+
"$$\n",
28+
"\\det(A) = ad - bc\n",
29+
"$$\n",
30+
"\n",
31+
"如果 $\\det(A) = 0$,那么矩阵是**不可逆的**,即不存在 $A^{-1}$。在代码中,这一步通过 `det = a * d - b * c` 计算,并用 `if det == 0:` 判断是否可逆。如果行列式为零,函数会打印 \"矩阵不可逆,行列式为零\",并返回 `None`,终止计算。\n",
32+
"\n",
33+
"如果矩阵是可逆的,即 $\\det(A) \\neq 0$,则可以按照标准公式计算逆矩阵:\n",
34+
"\n",
35+
"$$\n",
36+
"A^{-1} = \\frac{1}{\\det(A)} \\begin{bmatrix} d & -b \\\\ -c & a \\end{bmatrix}\n",
37+
"$$\n",
38+
"\n",
39+
"在代码中,首先创建矩阵:\n",
40+
"\n",
41+
"$$\n",
42+
"\\begin{bmatrix} d & -b \\\\ -c & a \\end{bmatrix}\n",
43+
"$$\n",
44+
"\n",
45+
"然后,将其除以 $\\det(A)$ 来得到最终的逆矩阵。这一步在代码里是 `inv_A = np.array([[d, -b], [-c, a]]) / det`。\n",
46+
"\n",
47+
"代码示例中,矩阵 $A$ 被定义为:\n",
48+
"\n",
49+
"$$\n",
50+
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
51+
"$$\n",
52+
"\n",
53+
"其行列式计算如下:\n",
54+
"\n",
55+
"$$\n",
56+
"\\det(A) = (1 \\times 4) - (2 \\times 3) = 4 - 6 = -2\n",
57+
"$$\n",
58+
"\n",
59+
"因此,$A$ 是可逆的,按照逆矩阵公式计算:\n",
60+
"\n",
61+
"$$\n",
62+
"A^{-1} = \\frac{1}{-2} \\begin{bmatrix} 4 & -2 \\\\ -3 & 1 \\end{bmatrix}\n",
63+
"= \\begin{bmatrix} -2 & 1 \\\\ 1.5 & -0.5 \\end{bmatrix}\n",
64+
"$$\n",
65+
"\n",
66+
"最终,`inverse_2x2_A(A)` 计算并返回了这个逆矩阵。该方法利用 `numpy` 进行矩阵运算,并确保只对可逆矩阵进行计算,从而避免错误。"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"id": "b7998c9a-044c-4308-b809-c7458b33a48b",
72+
"metadata": {},
73+
"source": [
74+
"## 初始化"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 4,
80+
"id": "d530bc5e-a5b5-41cf-92e4-461541be8fc2",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"import numpy as np"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"id": "fd8fe07d-9741-4d49-bc75-4292daf3790b",
90+
"metadata": {},
91+
"source": [
92+
"## 自定义函数"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 6,
98+
"id": "ad0dd3b8-1858-4362-82f0-c17ff891d911",
99+
"metadata": {},
100+
"outputs": [],
101+
"source": [
102+
"def inverse_2x2_A(A):\n",
103+
" \"\"\"\n",
104+
" A = [[a, b],\n",
105+
" [c, d]]\n",
106+
" \"\"\"\n",
107+
" # 提取矩阵元素\n",
108+
" a, b = A[0]\n",
109+
" c, d = A[1]\n",
110+
" \n",
111+
" # 计算行列式 det(A) = ad - bc\n",
112+
" det = a * d - b * c\n",
113+
" \n",
114+
" # 判断行列式是否为零\n",
115+
" if det == 0:\n",
116+
" print(\"矩阵不可逆,行列式为零\")\n",
117+
" return None\n",
118+
" \n",
119+
" # 计算逆矩阵\n",
120+
" inv_A = np.array([[d, -b], [-c, a]]) / det\n",
121+
" return inv_A"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"id": "5905d558-ffac-4737-868f-7c9b665aeab0",
127+
"metadata": {},
128+
"source": [
129+
"## 定义矩阵"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 8,
135+
"id": "1e029681-047b-42b1-9f57-6476396e95a8",
136+
"metadata": {},
137+
"outputs": [
138+
{
139+
"data": {
140+
"text/plain": [
141+
"array([[1, 2],\n",
142+
" [3, 4]])"
143+
]
144+
},
145+
"execution_count": 8,
146+
"metadata": {},
147+
"output_type": "execute_result"
148+
}
149+
],
150+
"source": [
151+
"A = [[1, 2], \n",
152+
" [3, 4]] # 示例矩阵\n",
153+
"A = np.array(A)\n",
154+
"A"
155+
]
156+
},
157+
{
158+
"cell_type": "markdown",
159+
"id": "50736aaa-bc64-46c9-9527-8dce6b0ba101",
160+
"metadata": {},
161+
"source": [
162+
"## 计算逆矩阵"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 9,
168+
"id": "72553dc9-98d5-4509-ab4e-679c3668f3b3",
169+
"metadata": {},
170+
"outputs": [
171+
{
172+
"data": {
173+
"text/plain": [
174+
"array([[-2. , 1. ],\n",
175+
" [ 1.5, -0.5]])"
176+
]
177+
},
178+
"execution_count": 9,
179+
"metadata": {},
180+
"output_type": "execute_result"
181+
}
182+
],
183+
"source": [
184+
"inverse_2x2_A(A)"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"id": "d86b9119-2028-4797-8c3e-6c7af8fd2995",
191+
"metadata": {},
192+
"outputs": [],
193+
"source": []
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"id": "9eac7716-a90e-4c76-ac0f-c81c864dd073",
199+
"metadata": {},
200+
"outputs": [],
201+
"source": []
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"id": "3886cb7b-972c-4347-9b6b-f66c8de25cbb",
207+
"metadata": {},
208+
"outputs": [],
209+
"source": []
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"id": "070c3389-8048-43a3-baa7-6666009bce96",
214+
"metadata": {},
215+
"source": [
216+
"作者\t**生姜DrGinger** \n",
217+
"脚本\t**生姜DrGinger** \n",
218+
"视频\t**崔崔CuiCui** \n",
219+
"开源资源\t[**GitHub**](https://github.com/Visualize-ML) \n",
220+
"平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
221+
"\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
222+
"\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466) "
223+
]
224+
}
225+
],
226+
"metadata": {
227+
"kernelspec": {
228+
"display_name": "Python [conda env:base] *",
229+
"language": "python",
230+
"name": "conda-base-py"
231+
},
232+
"language_info": {
233+
"codemirror_mode": {
234+
"name": "ipython",
235+
"version": 3
236+
},
237+
"file_extension": ".py",
238+
"mimetype": "text/x-python",
239+
"name": "python",
240+
"nbconvert_exporter": "python",
241+
"pygments_lexer": "ipython3",
242+
"version": "3.12.7"
243+
}
244+
},
245+
"nbformat": 4,
246+
"nbformat_minor": 5
247+
}

0 commit comments

Comments
 (0)