Skip to content

Commit 79f58b5

Browse files
authored
Add files via upload
1 parent 7db4c85 commit 79f58b5

13 files changed

+3854
-0
lines changed

LA_02_01_01.ipynb

+677
Large diffs are not rendered by default.

LA_02_01_02.ipynb

+493
Large diffs are not rendered by default.

LA_02_01_矩阵.pdf

872 KB
Binary file not shown.

LA_02_02_01.ipynb

+869
Large diffs are not rendered by default.

LA_02_02_02.ipynb

+590
Large diffs are not rendered by default.

LA_02_02_矩阵转置.pdf

680 KB
Binary file not shown.

LA_02_03_矩阵的形状.pdf

694 KB
Binary file not shown.

LA_02_04_01.ipynb

+406
Large diffs are not rendered by default.

LA_02_04_02.ipynb

+480
Large diffs are not rendered by default.

LA_02_04_03.ipynb

+339
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "0f84611f-5f01-4dc6-ba6b-abefedaeaa61",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 02\n",
9+
"\n",
10+
"# 自定义函数计算矩阵乘法\n",
11+
"《线性代数》 | 鸢尾花书:数学不难"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "9bed0075-4769-46aa-8ee6-c6c449b83284",
17+
"metadata": {},
18+
"source": [
19+
"这段代码的主要任务是从数学角度实现**矩阵乘法**(Matrix Multiplication)的过程,并验证其在不同矩阵顺序下的结果。以下是对代码的详细数学解释:\n",
20+
"\n",
21+
"---\n",
22+
"\n",
23+
"### 1. **矩阵乘法的基本原理**\n",
24+
"\n",
25+
"设有两个矩阵 $A \\in \\mathbb{R}^{m \\times p}$ 和 $B \\in \\mathbb{R}^{p \\times n}$,它们的乘积定义为一个新的矩阵 $C \\in \\mathbb{R}^{m \\times n}$,满足:\n",
26+
"\n",
27+
"$$\n",
28+
"C_{ij} = \\sum_{k=1}^{p} A_{ik} \\cdot B_{kj}, \\quad \\text{其中 } 1 \\leq i \\leq m,\\ 1 \\leq j \\leq n\n",
29+
"$$\n",
30+
"\n",
31+
"这意味着,$C$ 的第 $i$ 行第 $j$ 列的元素是 $A$ 的第 $i$ 行和 $B$ 的第 $j$ 列的**点积**。\n",
32+
"\n",
33+
"---\n",
34+
"\n",
35+
"### 2. **代码实现的数学对应**\n",
36+
"\n",
37+
"定义的函数 `matrix_multiplication(A, B)` 实际上就是实现上述公式的计算方式:\n",
38+
"\n",
39+
"- 首先获取矩阵 $A$ 和 $B$ 的形状分别为 $(m, p_A)$ 和 $(p_B, n)$。\n",
40+
"- 然后检查是否满足矩阵乘法的条件,即 $p_A = p_B$,对应数学上的 $A \\in \\mathbb{R}^{m \\times p}$ 和 $B \\in \\mathbb{R}^{p \\times n}$。\n",
41+
"- 接着创建零矩阵 $C \\in \\mathbb{R}^{m \\times n}$。\n",
42+
"- 使用三重循环实现:\n",
43+
"\n",
44+
"$$\n",
45+
"\\text{for } i = 0 \\text{ to } m-1:\\\\\n",
46+
"\\quad \\text{for } j = 0 \\text{ to } n-1:\\\\\n",
47+
"\\quad\\quad \\text{for } k = 0 \\text{ to } p-1:\\\\\n",
48+
"\\quad\\quad\\quad C[i,j] \\mathrel{+}= A[i,k] \\cdot B[k,j]\n",
49+
"$$\n",
50+
"\n",
51+
"该循环逐元素计算并累加 $C_{ij}$ 的值,和上述数学定义完全一致。\n",
52+
"\n",
53+
"---\n",
54+
"\n",
55+
"### 3. **具体矩阵的构造与运算**\n",
56+
"\n",
57+
"矩阵 $A$ 被定义为:\n",
58+
"\n",
59+
"$$\n",
60+
"A = \\begin{bmatrix}\n",
61+
"1 & 2 & 3 \\\\\n",
62+
"4 & 5 & 6\n",
63+
"\\end{bmatrix} \\in \\mathbb{R}^{2 \\times 3}\n",
64+
"$$\n",
65+
"\n",
66+
"然后取其转置得到:\n",
67+
"\n",
68+
"$$\n",
69+
"B = A^\\top = \\begin{bmatrix}\n",
70+
"1 & 4 \\\\\n",
71+
"2 & 5 \\\\\n",
72+
"3 & 6\n",
73+
"\\end{bmatrix} \\in \\mathbb{R}^{3 \\times 2}\n",
74+
"$$\n",
75+
"\n",
76+
"再进行两次矩阵乘法:\n",
77+
"\n",
78+
"#### (1) $A \\cdot B$\n",
79+
"\n",
80+
"$$\n",
81+
"A \\cdot B = \\begin{bmatrix}\n",
82+
"1 & 2 & 3 \\\\\n",
83+
"4 & 5 & 6\n",
84+
"\\end{bmatrix}\n",
85+
"\\cdot\n",
86+
"\\begin{bmatrix}\n",
87+
"1 & 4 \\\\\n",
88+
"2 & 5 \\\\\n",
89+
"3 & 6\n",
90+
"\\end{bmatrix}\n",
91+
"= \\begin{bmatrix}\n",
92+
"1\\cdot1 + 2\\cdot2 + 3\\cdot3 & 1\\cdot4 + 2\\cdot5 + 3\\cdot6 \\\\\n",
93+
"4\\cdot1 + 5\\cdot2 + 6\\cdot3 & 4\\cdot4 + 5\\cdot5 + 6\\cdot6\n",
94+
"\\end{bmatrix}\n",
95+
"= \\begin{bmatrix}\n",
96+
"14 & 32 \\\\\n",
97+
"32 & 77\n",
98+
"\\end{bmatrix}\n",
99+
"$$\n",
100+
"\n",
101+
"#### (2) $B \\cdot A$\n",
102+
"\n",
103+
"$$\n",
104+
"B \\cdot A = \\begin{bmatrix}\n",
105+
"1 & 4 \\\\\n",
106+
"2 & 5 \\\\\n",
107+
"3 & 6\n",
108+
"\\end{bmatrix}\n",
109+
"\\cdot\n",
110+
"\\begin{bmatrix}\n",
111+
"1 & 2 & 3 \\\\\n",
112+
"4 & 5 & 6\n",
113+
"\\end{bmatrix}\n",
114+
"= \\begin{bmatrix}\n",
115+
"1\\cdot1 + 4\\cdot4 & 1\\cdot2 + 4\\cdot5 & 1\\cdot3 + 4\\cdot6 \\\\\n",
116+
"2\\cdot1 + 5\\cdot4 & 2\\cdot2 + 5\\cdot5 & 2\\cdot3 + 5\\cdot6 \\\\\n",
117+
"3\\cdot1 + 6\\cdot4 & 3\\cdot2 + 6\\cdot5 & 3\\cdot3 + 6\\cdot6\n",
118+
"\\end{bmatrix}\n",
119+
"= \\begin{bmatrix}\n",
120+
"17 & 22 & 27 \\\\\n",
121+
"22 & 29 & 36 \\\\\n",
122+
"27 & 36 & 45\n",
123+
"\\end{bmatrix}\n",
124+
"$$\n",
125+
"\n",
126+
"---\n",
127+
"\n",
128+
"### 4. **总结**\n",
129+
"\n",
130+
"本代码实现的是基础线性代数中最核心的运算之一:**矩阵乘法**,其数学本质是将一个矩阵的行向量与另一个矩阵的列向量进行点积。由于使用的是纯 Python 的循环方式(而不是 NumPy 的矢量化计算),这也帮助我们直观理解矩阵乘法的底层运算逻辑,尤其对学习者理解以下公式至关重要:\n",
131+
"\n",
132+
"$$\n",
133+
"C_{ij} = \\sum_{k=1}^{p} A_{ik} \\cdot B_{kj}\n",
134+
"$$\n",
135+
"\n",
136+
"这个公式是整个函数 `matrix_multiplication` 的核心。"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"id": "62554c66-72b0-4680-8498-2c1f4b7de75f",
142+
"metadata": {},
143+
"source": [
144+
"## 初始化"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 2,
150+
"id": "4726c405-d63a-42cd-b391-471764ec75b5",
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"import numpy as np"
155+
]
156+
},
157+
{
158+
"cell_type": "markdown",
159+
"id": "25ce6457-d6c3-4c33-93f7-0311dad3d710",
160+
"metadata": {},
161+
"source": [
162+
"## 自定义函数"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 4,
168+
"id": "311e7aee-b415-47ec-9971-a16921c77ce4",
169+
"metadata": {},
170+
"outputs": [],
171+
"source": [
172+
"def matrix_multiplication(A, B):\n",
173+
"\n",
174+
" # 获取矩阵 A 和 B 的形状\n",
175+
" m, p_A = A.shape\n",
176+
" p_B, n = B.shape\n",
177+
"\n",
178+
" # 检测矩阵形状是否符合矩阵乘法规则\n",
179+
" if p_A != p_B:\n",
180+
" raise ValueError('Dimensions do not match')\n",
181+
"\n",
182+
" # 初始化结果矩阵 C,形状 (m, n),初始值设为 0\n",
183+
" C = np.zeros((m, n))\n",
184+
"\n",
185+
" # 进行矩阵乘法计算,使用三层 for 循环\n",
186+
" for i in range(m): # 遍历 A 的行\n",
187+
" for j in range(n): # 遍历 B 的列\n",
188+
" for k in range(p_A): # 遍历 A 的列 / B 的行\n",
189+
" C[i, j] += A[i, k] * B[k, j] # 逐元素累加\n",
190+
"\n",
191+
" return C"
192+
]
193+
},
194+
{
195+
"cell_type": "markdown",
196+
"id": "3d7cf1d6-3406-4c65-8860-eb9c555c2e46",
197+
"metadata": {},
198+
"source": [
199+
"## 定义矩阵"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 15,
205+
"id": "615efd21-d4c7-41b6-9827-481d11fec522",
206+
"metadata": {},
207+
"outputs": [
208+
{
209+
"data": {
210+
"text/plain": [
211+
"array([[1, 2, 3],\n",
212+
" [4, 5, 6]])"
213+
]
214+
},
215+
"execution_count": 15,
216+
"metadata": {},
217+
"output_type": "execute_result"
218+
}
219+
],
220+
"source": [
221+
"A = np.array([[1, 2, 3],\n",
222+
" [4, 5, 6]])\n",
223+
"A"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 17,
229+
"id": "1aa1a8c3-7fae-453c-9016-8ce410ef9f7f",
230+
"metadata": {},
231+
"outputs": [
232+
{
233+
"data": {
234+
"text/plain": [
235+
"array([[1, 4],\n",
236+
" [2, 5],\n",
237+
" [3, 6]])"
238+
]
239+
},
240+
"execution_count": 17,
241+
"metadata": {},
242+
"output_type": "execute_result"
243+
}
244+
],
245+
"source": [
246+
"B = A.T\n",
247+
"B"
248+
]
249+
},
250+
{
251+
"cell_type": "markdown",
252+
"id": "b540f553-e9fe-44bf-88dd-05c8b83efecd",
253+
"metadata": {},
254+
"source": [
255+
"## 矩阵乘法"
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": 20,
261+
"id": "1ce432ee-3cf1-468e-820e-825b581820e1",
262+
"metadata": {},
263+
"outputs": [
264+
{
265+
"data": {
266+
"text/plain": [
267+
"array([[14., 32.],\n",
268+
" [32., 77.]])"
269+
]
270+
},
271+
"execution_count": 20,
272+
"metadata": {},
273+
"output_type": "execute_result"
274+
}
275+
],
276+
"source": [
277+
"matrix_multiplication(A, B)"
278+
]
279+
},
280+
{
281+
"cell_type": "code",
282+
"execution_count": 22,
283+
"id": "513aaf4c-f3a6-4362-a628-3c0bb729faef",
284+
"metadata": {},
285+
"outputs": [
286+
{
287+
"data": {
288+
"text/plain": [
289+
"array([[17., 22., 27.],\n",
290+
" [22., 29., 36.],\n",
291+
" [27., 36., 45.]])"
292+
]
293+
},
294+
"execution_count": 22,
295+
"metadata": {},
296+
"output_type": "execute_result"
297+
}
298+
],
299+
"source": [
300+
"matrix_multiplication(B, A)"
301+
]
302+
},
303+
{
304+
"cell_type": "markdown",
305+
"id": "f8dae950-44ce-4a9e-893f-c6cbd84cc502",
306+
"metadata": {},
307+
"source": [
308+
"作者\t**生姜DrGinger** \n",
309+
"脚本\t**生姜DrGinger** \n",
310+
"视频\t**崔崔CuiCui** \n",
311+
"开源资源\t[**GitHub**](https://github.com/Visualize-ML) \n",
312+
"平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
313+
"\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
314+
"\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466) "
315+
]
316+
}
317+
],
318+
"metadata": {
319+
"kernelspec": {
320+
"display_name": "Python [conda env:base] *",
321+
"language": "python",
322+
"name": "conda-base-py"
323+
},
324+
"language_info": {
325+
"codemirror_mode": {
326+
"name": "ipython",
327+
"version": 3
328+
},
329+
"file_extension": ".py",
330+
"mimetype": "text/x-python",
331+
"name": "python",
332+
"nbconvert_exporter": "python",
333+
"pygments_lexer": "ipython3",
334+
"version": "3.12.7"
335+
}
336+
},
337+
"nbformat": 4,
338+
"nbformat_minor": 5
339+
}

LA_02_04_矩阵乘法.pdf

688 KB
Binary file not shown.

LA_02_05_矩阵乘法几何视角.pdf

1.27 MB
Binary file not shown.

LA_02_06_矩阵乘法性质.pdf

1.59 MB
Binary file not shown.

0 commit comments

Comments
 (0)