Skip to content

Commit d1d0db9

Browse files
authored
Add files via upload
1 parent ecdb32b commit d1d0db9

File tree

68 files changed

+18837
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+18837
-0
lines changed

Book3_Ch13_Python_Codes/Bk3_Ch13_01.ipynb

+351
Large diffs are not rendered by default.

Book3_Ch13_Python_Codes/Bk3_Ch13_02.ipynb

+163
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
###############
2+
# Authored by Weisheng Jiang
3+
# Book 3 | From Basic Arithmetic to Machine Learning
4+
# Published and copyrighted by Tsinghua University Press
5+
# Beijing, China, 2025
6+
###############
7+
8+
import math # 导入数学模块
9+
import numpy as np # 导入 numpy 模块,用于数值计算
10+
import matplotlib.pyplot as plt # 导入 matplotlib 模块,用于绘图
11+
from matplotlib import cm # 导入 matplotlib 的 colormap,用于颜色映射
12+
import streamlit as st # 导入 streamlit 模块,用于创建交互式界面
13+
from sympy import symbols, lambdify # 导入 sympy 模块,用于符号运算
14+
import plotly.graph_objects as go # 导入 plotly 的 graph_objects,用于交互式绘图
15+
16+
## 定义生成网格的函数
17+
def mesh_square(x1_0, x2_0, r, num):
18+
rr = np.linspace(-r, r, num) # 在指定范围生成均匀分布的点
19+
xx1, xx2 = np.meshgrid(rr, rr) # 创建二维网格
20+
xx1 = xx1 + x1_0 # 平移网格中心到 x1_0
21+
xx2 = xx2 + x2_0 # 平移网格中心到 x2_0
22+
return xx1, xx2, rr # 返回生成的网格和坐标数组
23+
24+
## 定义绘制 3D 曲面的函数
25+
def plot_surf(xx1, xx2, ff):
26+
norm_plt = plt.Normalize(ff.min(), ff.max()) # 归一化颜色映射范围
27+
colors = cm.coolwarm(norm_plt(ff)) # 使用 coolwarm 颜色映射
28+
fig = plt.figure() # 创建图形
29+
ax = fig.add_subplot(projection='3d') # 添加 3D 坐标轴
30+
surf = ax.plot_surface(xx1, xx2, ff, facecolors=colors, shade=False) # 绘制曲面
31+
surf.set_facecolor((0, 0, 0, 0)) # 设置背景为透明
32+
ax.set_xlabel('$\it{x_1}$') # 设置 x 轴标签
33+
ax.set_ylabel('$\it{x_2}$') # 设置 y 轴标签
34+
ax.set_zlabel('$\it{f}$($\it{x_1}$,$\it{x_2}$)') # 设置 z 轴标签
35+
return fig # 返回绘制的图形
36+
37+
## 定义绘制等高线的函数
38+
def plot_contourf(xx1, xx2, ff):
39+
fig, ax = plt.subplots() # 创建图形和坐标轴
40+
cntr2 = ax.contourf(xx1, xx2, ff, levels=15, cmap="RdBu_r") # 绘制填充等高线
41+
fig.colorbar(cntr2, ax=ax) # 添加颜色条
42+
ax.set_xlabel('$\it{x_1}$') # 设置 x 轴标签
43+
ax.set_ylabel('$\it{x_2}$') # 设置 y 轴标签
44+
ax.grid(linestyle='--', linewidth=0.25, color=[0.5, 0.5, 0.5]) # 设置网格样式
45+
return fig # 返回绘制的图形
46+
47+
## 设置 Streamlit 侧边栏交互控件
48+
with st.sidebar:
49+
st.latex(r'f(x_1,x_2) = ax_1^2 + bx_1x_2 + cx_2^2 + dx_1 + ex_2 + f') # 显示数学公式
50+
a = st.slider('a', min_value=-2.0, max_value=2.0, step=0.1) # 滑动条选择参数 a
51+
b = st.slider('b', min_value=-2.0, max_value=2.0, step=0.1) # 滑动条选择参数 b
52+
c = st.slider('c', min_value=-2.0, max_value=2.0, step=0.1) # 滑动条选择参数 c
53+
d = st.slider('d', min_value=-2.0, max_value=2.0, step=0.1) # 滑动条选择参数 d
54+
e = st.slider('e', min_value=-2.0, max_value=2.0, step=0.1) # 滑动条选择参数 e
55+
f = st.slider('f', min_value=-2.0, max_value=2.0, step=0.1) # 滑动条选择参数 f
56+
57+
## 初始化网格和符号表达式
58+
x1, x2 = symbols('x1 x2') # 定义符号变量
59+
x1_0, x2_0 = 0, 0 # 设置网格中心
60+
r, num = 2, 30 # 设置网格半径和点数
61+
xx1, xx2, x1_array = mesh_square(x1_0, x2_0, r, num) # 生成网格
62+
x2_array = x1_array # y 轴坐标与 x 轴一致
63+
64+
## 使用 matplotlib 可视化
65+
f_sym = a * x1**2 + b * x1 * x2 + c * x2**2 + d * x1 + e * x2 + f # 定义目标函数
66+
f_fcn = lambdify([x1, x2], f_sym) # 将符号函数转换为数值函数
67+
ff = f_fcn(xx1, xx2) # 计算目标函数值
68+
fig_1 = plot_surf(xx1, xx2, ff) # 绘制 3D 曲面
69+
fig_2 = plot_contourf(xx1, xx2, ff) # 绘制等高线
70+
71+
## 使用 Plotly 可视化
72+
fig_surface = go.Figure(go.Surface(
73+
x=x1_array, y=x2_array, z=ff, showscale=False, colorscale='RdYlBu_r')) # 绘制交互式 3D 曲面
74+
fig_surface.update_layout(autosize=True, width=800, height=600) # 设置布局大小
75+
st.plotly_chart(fig_surface) # 在 Streamlit 中显示 3D 曲面图
76+
77+
fig_contour = go.Figure(data=go.Contour(
78+
z=ff, x=x1_array, y=x2_array, colorscale='RdYlBu_r')) # 绘制交互式等高线
79+
fig_contour.update_layout(autosize=True, width=600, height=600) # 设置布局大小
80+
st.plotly_chart(fig_contour) # 在 Streamlit 中显示等高线图

Book3_Ch14_Python_Codes/Bk3_Ch14_01.ipynb

+230
Large diffs are not rendered by default.

Book3_Ch14_Python_Codes/Bk3_Ch14_02.ipynb

+152
Large diffs are not rendered by default.
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 14\n",
9+
"\n",
10+
"# 斐波那契数列\n",
11+
"Book_3《数学要素》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "9518a62e-6d72-4d77-b6c7-f210be3b5aff",
17+
"metadata": {},
18+
"source": [
19+
"这段代码的核心功能是生成并显示斐波那契数列的前 $n$ 项。斐波那契数列的定义是:\n",
20+
"\n",
21+
"$$\n",
22+
"F(0) = 0, \\quad F(1) = 1\n",
23+
"$$\n",
24+
"\n",
25+
"对于 $n \\geq 2$ 的情况,数列的递推关系为:\n",
26+
"\n",
27+
"$$\n",
28+
"F(n) = F(n-1) + F(n-2)\n",
29+
"$$\n",
30+
"\n",
31+
"每一项等于前两项之和,从而形成一系列逐步递增的值。代码中的函数 `fib(n)` 是递归地定义的,用于计算第 $n$ 项的值。递归关系表示,如果 $n \\leq 1$,函数直接返回 $n$;否则,它会调用自身来计算前两项 $F(n-1)$ 和 $F(n-2)$,并返回它们的和。这种递归调用直到到达基准条件,即 $F(0)$ 或 $F(1)$,从而逐步回溯并计算最终结果。\n",
32+
"\n",
33+
"在主程序中,定义了变量 `n = 10`,表示我们希望显示数列的前 $10$ 项。然后,代码通过 `for` 循环依次计算每一项 $F(i)$,从 $i = 0$ 到 $i = 9$,并打印输出。这段代码的结果是输出斐波那契数列的前 $10$ 项,通过逐项调用 `fib(i)` 函数并显示计算结果。"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"id": "85c1aae2-7f61-4f09-b193-8567ae2946ce",
39+
"metadata": {},
40+
"source": [
41+
"## 定义斐波那契数列生成函数"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 1,
47+
"id": "d15d8d04-e0ee-48d9-85f0-d910263dd962",
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"def fib(n):\n",
52+
" # 计算斐波那契数列第n项\n",
53+
" if n <= 1:\n",
54+
" return (n) # 如果n小于等于1,返回n\n",
55+
" else:\n",
56+
" return (fib(n-1) + fib(n-2)) # 否则递归计算前两项之和"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"id": "cda79a3c-5f7a-4fe8-9c7a-85b488586c85",
62+
"metadata": {},
63+
"source": [
64+
"## 输出斐波那契数列的前n项"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 2,
70+
"id": "13119ff3-f4ae-4846-94b1-674f97bbeaba",
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"0\n",
78+
"1\n",
79+
"1\n",
80+
"2\n",
81+
"3\n",
82+
"5\n",
83+
"8\n",
84+
"13\n",
85+
"21\n",
86+
"34\n"
87+
]
88+
}
89+
],
90+
"source": [
91+
"n = 10 # 设定要显示的项数\n",
92+
"for i in range(n):\n",
93+
" print(fib(i)) # 打印斐波那契数列的第i项"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": []
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
108+
"metadata": {},
109+
"outputs": [],
110+
"source": []
111+
}
112+
],
113+
"metadata": {
114+
"kernelspec": {
115+
"display_name": "Python 3 (ipykernel)",
116+
"language": "python",
117+
"name": "python3"
118+
},
119+
"language_info": {
120+
"codemirror_mode": {
121+
"name": "ipython",
122+
"version": 3
123+
},
124+
"file_extension": ".py",
125+
"mimetype": "text/x-python",
126+
"name": "python",
127+
"nbconvert_exporter": "python",
128+
"pygments_lexer": "ipython3",
129+
"version": "3.12.7"
130+
}
131+
},
132+
"nbformat": 4,
133+
"nbformat_minor": 5
134+
}

Book3_Ch14_Python_Codes/Bk3_Ch14_04.ipynb

+246
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)