Skip to content

Commit 95c9b1c

Browse files
authored
Add files via upload
1 parent 5cec660 commit 95c9b1c

7 files changed

+811
-0
lines changed

LA_04_01_01.ipynb

+260
Large diffs are not rendered by default.

LA_04_01_2x2行列式.pdf

663 KB
Binary file not shown.

LA_04_02_01.ipynb

+291
Large diffs are not rendered by default.

LA_04_02_3x3矩阵行列式.pdf

610 KB
Binary file not shown.

LA_04_03_行列式的性质.pdf

854 KB
Binary file not shown.

LA_04_04_01.ipynb

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 04\n",
9+
"\n",
10+
"# 伴随矩阵法求解逆矩阵\n",
11+
"《线性代数》 | 鸢尾花书:数学不难"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
17+
"metadata": {},
18+
"source": [
19+
"该代码实现了计算矩阵的**行列式(determinant)**的方法,使用的是**Laplace展开(Laplace Expansion)**,即按某一行或某一列展开计算行列式。\n",
20+
"\n",
21+
"---\n",
22+
"\n",
23+
"### **1. 余子矩阵(Minor Matrix)**\n",
24+
"给定一个$n \\times n$矩阵 $A$,其某个元素 $A_{ij}$ 的**余子矩阵(minor matrix)** $M_{ij}$ 是删除该元素所在的第 $i$ 行和第 $j$ 列后得到的 $(n-1) \\times (n-1)$ 矩阵。代码中的 `get_minor(matrix, row, col)` 函数通过 `np.delete` 删除指定行和列来获取余子矩阵:\n",
25+
"\n",
26+
"$$\n",
27+
"M_{ij} = \\text{Minor}(A, i, j)\n",
28+
"$$\n",
29+
"\n",
30+
"---\n",
31+
"\n",
32+
"### **2. Laplace 展开计算行列式**\n",
33+
"对于 $n \\times n$ 矩阵 $A$,行列式 $\\det(A)$ 递归地按第一行展开(也可以按任意一行或一列展开,但本代码按第一行展开):\n",
34+
"\n",
35+
"$$\n",
36+
"\\det(A) = \\sum_{j=0}^{n-1} (-1)^j A_{0j} \\det(M_{0j})\n",
37+
"$$\n",
38+
"\n",
39+
"其中:\n",
40+
"- $A_{0j}$ 是矩阵 $A$ 第一行的第 $j$ 个元素。\n",
41+
"- $M_{0j}$ 是 $A_{0j}$ 对应的余子矩阵。\n",
42+
"- $(-1)^j$ 是交替的符号,用于计算**代数余子式(cofactor)**。\n",
43+
"\n",
44+
"代码中 `determinant(matrix)` 递归地调用自己来计算 $\\det(M_{0j})$,最终得到 $\\det(A)$。\n",
45+
"\n",
46+
"---\n",
47+
"\n",
48+
"### **3. 递归终止条件**\n",
49+
"在 `determinant(matrix)` 中:\n",
50+
"- 如果 $n=1$,即 $A$ 仅为 $1 \\times 1$ 矩阵,则 $\\det(A) = A_{00}$。\n",
51+
"- 如果 $n=2$,即 $A$ 为 $2 \\times 2$ 矩阵,则直接使用公式:\n",
52+
"\n",
53+
"$$\n",
54+
"\\det \\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix} = ad - bc\n",
55+
"$$\n",
56+
"\n",
57+
"- 对于 $n \\geq 3$,使用 Laplace 展开进行递归计算。\n",
58+
"\n",
59+
"---\n",
60+
"\n",
61+
"### **4. 计算示例**\n",
62+
"给定矩阵:\n",
63+
"$$\n",
64+
"A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 3 & 0 & 1 \\\\ 1 & 2 & 1 \\end{bmatrix}\n",
65+
"$$\n",
66+
"\n",
67+
"展开计算:\n",
68+
"1. 按第一行展开:\n",
69+
"\n",
70+
"$$\n",
71+
"\\det(A) = 1 \\cdot \\det \\begin{bmatrix} 0 & 1 \\\\ 2 & 1 \\end{bmatrix} - 2 \\cdot \\det \\begin{bmatrix} 3 & 1 \\\\ 1 & 1 \\end{bmatrix} + 3 \\cdot \\det \\begin{bmatrix} 3 & 0 \\\\ 1 & 2 \\end{bmatrix}\n",
72+
"$$\n",
73+
"\n",
74+
"2. 计算 $2 \\times 2$ 子矩阵的行列式:\n",
75+
"\n",
76+
"$$\n",
77+
"\\det \\begin{bmatrix} 0 & 1 \\\\ 2 & 1 \\end{bmatrix} = 0 \\cdot 1 - 1 \\cdot 2 = -2\n",
78+
"$$\n",
79+
"\n",
80+
"$$\n",
81+
"\\det \\begin{bmatrix} 3 & 1 \\\\ 1 & 1 \\end{bmatrix} = 3 \\cdot 1 - 1 \\cdot 1 = 2\n",
82+
"$$\n",
83+
"\n",
84+
"$$\n",
85+
"\\det \\begin{bmatrix} 3 & 0 \\\\ 1 & 2 \\end{bmatrix} = 3 \\cdot 2 - 0 \\cdot 1 = 6\n",
86+
"$$\n",
87+
"\n",
88+
"3. 代入计算:\n",
89+
"\n",
90+
"$$\n",
91+
"\\det(A) = 1 \\cdot (-2) - 2 \\cdot (2) + 3 \\cdot (6)\n",
92+
"$$\n",
93+
"\n",
94+
"$$\n",
95+
"\\det(A) = -2 - 4 + 18 = 12\n",
96+
"$$\n",
97+
"\n",
98+
"因此,最终的行列式结果为 **12**。\n",
99+
"\n",
100+
"---\n",
101+
"\n",
102+
"### **总结**\n",
103+
"- 代码实现了 **递归计算行列式**,基于 Laplace 展开,选择第一行进行展开计算。\n",
104+
"- 通过 `get_minor()` 计算余子矩阵,再通过递归调用 `determinant()` 计算行列式。\n",
105+
"- 终止条件:$1 \\times 1$ 或 $2 \\times 2$ 矩阵直接计算。\n",
106+
"- 计算复杂度为 $O(n!)$,随着矩阵大小增长,计算量呈指数增长,因此适用于小矩阵。\n",
107+
"\n",
108+
"最终,代码计算出矩阵 $A$ 的行列式值为 $12$。"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"id": "47bc7070-fd14-4a88-936f-85385266166e",
114+
"metadata": {},
115+
"source": [
116+
"## 初始化"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 5,
122+
"id": "d7296ce6-de4d-4b77-b55d-e8e601f5e615",
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"import numpy as np"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"id": "4cd29e1b-f98f-4088-b9ec-a50b5a406b4d",
132+
"metadata": {},
133+
"source": [
134+
"## 自定义函数,提取余子矩阵"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 7,
140+
"id": "d29ff57c-b087-49b1-b8d9-fe561c5a4ac1",
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"def cal_minor(A, row_idx, col_idx):\n",
145+
" A_ij = np.delete(np.delete(A, row_idx, axis=0), col_idx, axis=1)\n",
146+
" # 去掉指定的行、列\n",
147+
" return A_ij"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"id": "0cc8393f-b473-4da4-8a4c-7ddcd17166d7",
153+
"metadata": {},
154+
"source": [
155+
"## 用Laplace展开递归计算行列式"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 9,
161+
"id": "bcc5a135-f965-4792-9fcd-6f8d1fa68132",
162+
"metadata": {},
163+
"outputs": [],
164+
"source": [
165+
"def determinant(matrix):\n",
166+
"\n",
167+
" n = matrix.shape[0]\n",
168+
" if n == 1:\n",
169+
" return matrix[0, 0]\n",
170+
" if n == 2:\n",
171+
" return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]\n",
172+
" \n",
173+
" det = 0\n",
174+
" for col_idx in range(n):\n",
175+
" minor = cal_minor(matrix, 0, col_idx) # 沿第一行展开\n",
176+
" cofactor = ((-1) ** col_idx) * determinant(minor) # 计算代数余子式\n",
177+
" # 相当于 (-1) ** ((col_idx + 1) + 1) = (-1) ** col_idx\n",
178+
" det += matrix[0, col] * cofactor # 计算行列式\n",
179+
" \n",
180+
" return det"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"id": "e9771c69-eb08-4500-8184-fe90ab522720",
186+
"metadata": {},
187+
"source": [
188+
"## 测试"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": null,
194+
"id": "4d49d283-744e-4704-a121-243f5fc6c8c9",
195+
"metadata": {},
196+
"outputs": [
197+
{
198+
"data": {
199+
"text/plain": [
200+
"12"
201+
]
202+
},
203+
"execution_count": 10,
204+
"metadata": {},
205+
"output_type": "execute_result"
206+
}
207+
],
208+
"source": [
209+
"A = np.array([[1, 2, 3], \n",
210+
" [3, 0, 1], \n",
211+
" [1, 2, 1]])\n",
212+
"A_det = determinant(A)\n",
213+
"A_det"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"id": "954b7525-dcef-47cb-af13-40c741ca3891",
220+
"metadata": {},
221+
"outputs": [],
222+
"source": []
223+
},
224+
{
225+
"cell_type": "markdown",
226+
"id": "070c3389-8048-43a3-baa7-6666009bce96",
227+
"metadata": {},
228+
"source": [
229+
"作者\t**生姜DrGinger** \n",
230+
"脚本\t**生姜DrGinger** \n",
231+
"视频\t**崔崔CuiCui** \n",
232+
"开源资源\t[**GitHub**](https://github.com/Visualize-ML) \n",
233+
"平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
234+
"\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
235+
"\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466) "
236+
]
237+
}
238+
],
239+
"metadata": {
240+
"kernelspec": {
241+
"display_name": "Python [conda env:base] *",
242+
"language": "python",
243+
"name": "conda-base-py"
244+
},
245+
"language_info": {
246+
"codemirror_mode": {
247+
"name": "ipython",
248+
"version": 3
249+
},
250+
"file_extension": ".py",
251+
"mimetype": "text/x-python",
252+
"name": "python",
253+
"nbconvert_exporter": "python",
254+
"pygments_lexer": "ipython3",
255+
"version": "3.12.7"
256+
}
257+
},
258+
"nbformat": 4,
259+
"nbformat_minor": 5
260+
}

LA_04_04_手算行列式.pdf

664 KB
Binary file not shown.

0 commit comments

Comments
 (0)