Skip to content

Commit 68c8505

Browse files
authored
Add files via upload
1 parent d924aa2 commit 68c8505

9 files changed

+1836
-0
lines changed

LA_07_01_01.ipynb

+318
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 07\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+
"该代码使用线性代数的方法解决经典的“鸡兔同笼”问题。本质上,该问题可以被建模为一个 $2 \\times 2$ 线性方程组,并通过矩阵求逆的方法进行求解。\n",
20+
"\n",
21+
"---\n",
22+
"\n",
23+
"### **1. 设定线性方程组**\n",
24+
"设鸡的数量为 $x_1$,兔的数量为 $x_2$,我们得到如下两个方程:\n",
25+
"$$\n",
26+
"x_1 + x_2 = 35\n",
27+
"$$\n",
28+
"$$\n",
29+
"2x_1 + 4x_2 = 94\n",
30+
"$$\n",
31+
"这个方程组可以用矩阵的形式表示为:\n",
32+
"$$\n",
33+
"A \\mathbf{x} = \\mathbf{b}\n",
34+
"$$\n",
35+
"其中:\n",
36+
"$$\n",
37+
"A = \\begin{bmatrix} 1 & 1 \\\\ 2 & 4 \\end{bmatrix}, \\quad\n",
38+
"\\mathbf{x} = \\begin{bmatrix} x_1 \\\\ x_2 \\end{bmatrix}, \\quad\n",
39+
"\\mathbf{b} = \\begin{bmatrix} 35 \\\\ 94 \\end{bmatrix}\n",
40+
"$$\n",
41+
"矩阵 $A$ 是系数矩阵,向量 $\\mathbf{x}$ 是未知数,向量 $\\mathbf{b}$ 是右侧的常数列向量。\n",
42+
"\n",
43+
"---\n",
44+
"\n",
45+
"### **2. 计算矩阵 $A$ 的逆**\n",
46+
"解方程 $\\mathbf{x} = A^{-1} \\mathbf{b}$ 需要先计算矩阵 $A$ 的逆:\n",
47+
"$$\n",
48+
"A^{-1} = \\frac{1}{\\det(A)} \\operatorname{adj}(A)\n",
49+
"$$\n",
50+
"行列式 $\\det(A)$ 计算如下:\n",
51+
"$$\n",
52+
"\\det(A) = 1 \\cdot 4 - 1 \\cdot 2 = 2\n",
53+
"$$\n",
54+
"伴随矩阵(余子式矩阵的转置):\n",
55+
"$$\n",
56+
"\\operatorname{adj}(A) = \\begin{bmatrix} 4 & -1 \\\\ -2 & 1 \\end{bmatrix}\n",
57+
"$$\n",
58+
"因此,矩阵 $A$ 的逆为:\n",
59+
"$$\n",
60+
"A^{-1} = \\frac{1}{2} \\begin{bmatrix} 4 & -1 \\\\ -2 & 1 \\end{bmatrix}\n",
61+
"= \\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix}\n",
62+
"$$\n",
63+
"\n",
64+
"---\n",
65+
"\n",
66+
"### **3. 验证逆矩阵**\n",
67+
"代码通过计算 $A^{-1} A$ 和 $A A^{-1}$ 来验证矩阵 $A$ 的逆是否正确:\n",
68+
"$$\n",
69+
"A^{-1} A = \\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix}\n",
70+
"\\begin{bmatrix} 1 & 1 \\\\ 2 & 4 \\end{bmatrix} = \n",
71+
"\\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
72+
"$$\n",
73+
"$$\n",
74+
"A A^{-1} = \\begin{bmatrix} 1 & 1 \\\\ 2 & 4 \\end{bmatrix}\n",
75+
"\\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix} = \n",
76+
"\\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
77+
"$$\n",
78+
"这表明 $A^{-1}$ 计算正确。\n",
79+
"\n",
80+
"---\n",
81+
"\n",
82+
"### **4. 计算最终解**\n",
83+
"最终,使用 $A^{-1} b$ 计算未知数 $\\mathbf{x}$:\n",
84+
"$$\n",
85+
"\\mathbf{x} = A^{-1} \\mathbf{b} = \n",
86+
"\\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix}\n",
87+
"\\begin{bmatrix} 35 \\\\ 94 \\end{bmatrix}\n",
88+
"$$\n",
89+
"进行矩阵乘法:\n",
90+
"$$\n",
91+
"x_1 = 2 \\times 35 + (-0.5) \\times 94 = 70 - 47 = 23\n",
92+
"$$\n",
93+
"$$\n",
94+
"x_2 = -1 \\times 35 + 0.5 \\times 94 = -35 + 47 = 12\n",
95+
"$$\n",
96+
"因此,鸡的数量是 **23**,兔的数量是 **12**。\n",
97+
"\n",
98+
"---\n",
99+
"\n",
100+
"### **总结**\n",
101+
"本代码基于矩阵运算求解 $2 \\times 2$ 线性方程组,核心步骤如下:\n",
102+
"1. 设定矩阵方程 $A \\mathbf{x} = \\mathbf{b}$。\n",
103+
"2. 计算 $A$ 的逆矩阵 $A^{-1}$。\n",
104+
"3. 验证 $A^{-1}$ 是否正确。\n",
105+
"4. 计算 $A^{-1} b$ 得到未知数的值,即 **鸡 23 只,兔 12 只**。\n",
106+
"\n",
107+
"这个方法是利用矩阵求逆来解线性方程组的一种常见方式,但在高维情况下,通常采用高斯消元法或 LU 分解等更高效的数值方法。"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"id": "f98f12f8-64c9-4907-9910-7534106881b7",
113+
"metadata": {},
114+
"source": [
115+
"## 初始化"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 1,
121+
"id": "d530bc5e-a5b5-41cf-92e4-461541be8fc2",
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"import numpy as np"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"id": "dcea75fb-c19a-4f90-a0e9-b8556344174b",
131+
"metadata": {},
132+
"source": [
133+
"## 鸡兔同笼系数矩阵"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 3,
139+
"id": "f1044cb0-92c2-4e07-8b42-b5571dfc9101",
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"A = np.array([[1, 1], \n",
144+
" [2, 4]])\n",
145+
"# a_1, chicken \n",
146+
"# a_2, rabbit"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"id": "6d759117-5643-4b86-a795-1ebf15aa254b",
152+
"metadata": {},
153+
"source": [
154+
"## 常数列向量"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 5,
160+
"id": "954b7525-dcef-47cb-af13-40c741ca3891",
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"b = np.array([[35],\n",
165+
" [94]])"
166+
]
167+
},
168+
{
169+
"cell_type": "markdown",
170+
"id": "7799aec4-4734-49f7-b0c4-2d34344aa7be",
171+
"metadata": {},
172+
"source": [
173+
"## 矩阵A的逆"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 9,
179+
"id": "aa3dd5d6-6437-4775-9611-64e4fbb5ef65",
180+
"metadata": {},
181+
"outputs": [
182+
{
183+
"data": {
184+
"text/plain": [
185+
"array([[ 2. , -0.5],\n",
186+
" [-1. , 0.5]])"
187+
]
188+
},
189+
"execution_count": 9,
190+
"metadata": {},
191+
"output_type": "execute_result"
192+
}
193+
],
194+
"source": [
195+
"A_inv = np.linalg.inv(A)\n",
196+
"A_inv"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 11,
202+
"id": "dd3bd71d-93b6-480f-bdbd-2b5bc85c8143",
203+
"metadata": {},
204+
"outputs": [
205+
{
206+
"data": {
207+
"text/plain": [
208+
"array([[1., 0.],\n",
209+
" [0., 1.]])"
210+
]
211+
},
212+
"execution_count": 11,
213+
"metadata": {},
214+
"output_type": "execute_result"
215+
}
216+
],
217+
"source": [
218+
"# 验证\n",
219+
"A_inv @ A"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 13,
225+
"id": "c6cdbfed-8757-4050-b184-844f094ccb31",
226+
"metadata": {},
227+
"outputs": [
228+
{
229+
"data": {
230+
"text/plain": [
231+
"array([[1., 0.],\n",
232+
" [0., 1.]])"
233+
]
234+
},
235+
"execution_count": 13,
236+
"metadata": {},
237+
"output_type": "execute_result"
238+
}
239+
],
240+
"source": [
241+
"A @ A_inv"
242+
]
243+
},
244+
{
245+
"cell_type": "markdown",
246+
"id": "eb3be1b7-aad6-4a90-b890-06d7213c4edb",
247+
"metadata": {},
248+
"source": [
249+
"## 求解"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": 15,
255+
"id": "ef4cdee3-8577-47e4-af0f-a2a78aec7401",
256+
"metadata": {},
257+
"outputs": [
258+
{
259+
"data": {
260+
"text/plain": [
261+
"array([[23.],\n",
262+
" [12.]])"
263+
]
264+
},
265+
"execution_count": 15,
266+
"metadata": {},
267+
"output_type": "execute_result"
268+
}
269+
],
270+
"source": [
271+
"A_inv @ b"
272+
]
273+
},
274+
{
275+
"cell_type": "code",
276+
"execution_count": null,
277+
"id": "03232b1c-9ff6-41a6-8eb9-b5f98c289f77",
278+
"metadata": {},
279+
"outputs": [],
280+
"source": []
281+
},
282+
{
283+
"cell_type": "markdown",
284+
"id": "070c3389-8048-43a3-baa7-6666009bce96",
285+
"metadata": {},
286+
"source": [
287+
"作者\t**生姜DrGinger** \n",
288+
"脚本\t**生姜DrGinger** \n",
289+
"视频\t**崔崔CuiCui** \n",
290+
"开源资源\t[**GitHub**](https://github.com/Visualize-ML) \n",
291+
"平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
292+
"\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
293+
"\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466) "
294+
]
295+
}
296+
],
297+
"metadata": {
298+
"kernelspec": {
299+
"display_name": "Python [conda env:base] *",
300+
"language": "python",
301+
"name": "conda-base-py"
302+
},
303+
"language_info": {
304+
"codemirror_mode": {
305+
"name": "ipython",
306+
"version": 3
307+
},
308+
"file_extension": ".py",
309+
"mimetype": "text/x-python",
310+
"name": "python",
311+
"nbconvert_exporter": "python",
312+
"pygments_lexer": "ipython3",
313+
"version": "3.12.7"
314+
}
315+
},
316+
"nbformat": 4,
317+
"nbformat_minor": 5
318+
}

0 commit comments

Comments
 (0)