KaiYun Sports2026世界杯(中国)IOS/安卓官方下载 让数学公式自动推导

咱们在作念数学公式推导的视频时,比如,念念展示一个因式剖析:x 2 + 2 x + 1 x^2+2x+1 x 2 + 2 x + 1 造成 ( x + 1 ) 2 (x+1)^2 ( x + 1 ) 2 的,或者反过来因式伸开,王人需要手动磋磨剖析或伸开后的为止。
然后再作念一个切换动画。
expr1 = MathTex("x^2 +
expr2 = MathTex("(x +
K体育(中国)官网入口self.play(Transform(expr1, expr2))
苟简的公式不错手动去作念,如若公式稍许复杂呢?比如:
x 3 − 6 x 2 + 11 x − 6 x^3-6x^2+11x-6 x 3 #Python #动效 #数学 #每天一个常识点− 6 x 2 + 11 x − 6 可剖析成: ( x − 1 ) ( x − 2 ) ( x − 3 ) (x-1)(x-2)(x-3) ( x − 1 ) ( x − 2 ) ( x − 3 )2 x 3 + 4 x 2 − 30 x 2x^3 + 4x^2-30x 2 x 3 + 4 x 2 − 30 x 可剖析成: 2 x ( x − 3 ) ( x + 5 ) 2x(x-3)(x+5) 2 x ( x − 3 ) ( x + 5 )... ...这几个公式就不是那么容易剖析了,咱们制作公式变换的数学动画,或者绘制函数图像时,细则不念念把时辰花在这些数学推导上。
那么,把 SymPy 引进了 Manim 的动画制作中—— 一切王人变了。
1. 痛点规复:手动推导假定咱们要展示从 ( x + 2 ) ( x − 3 ) (x+2)(x-3) ( x + 2 ) ( x − 3 ) 伸开到 x 2 − x − 6 x^2-x-6 x 2 − x − 6 的经由。 传统作念法会这么写: # 原始因式
expr_origin = MathTex("(x + 2)(x - 3)")
# 手动磋磨中间能力(默算 or 草稿纸)
# ... ...
# 归拢同类项
expr_result = MathTex("x^2 - x - 6")
# 然后动画切换切换...
self.play(Transform(expr_origin, expr_result))
问题不言而喻:
容易算错(极端是总共复杂时)无法自动化(换个新公式就得重写代码)穷乏动态生成的纯真性咱们信得过需要的 是一个能自动贬责代数变形,况兼把为止无缝喂给 Manim 的管说念。
Python 2. SymPy 登场:代数变形的“发动机”SymPy 是 Python 的标志磋磨库,苟简说便是能让磋磨机帮你 推导数学公式 。
C 中枢火器: expand 和 factorfrom sympy import symbols, expand, factor, latex
x = symbols("x")
# 原始抒发式:因式形状
expr_factored = (x + 2) * (x - 3)
# 一键伸开!
expr_expanded = expand(expr_factored) print(expr_expanded) # 输出:x**2 - x - 6
# 再一键因式剖析!
expr_back = factor(expr_expanded) print(expr_back) # 输出:(x - 3)*(x + 2)
expand 门径不错匡助咱们将因式伸开;
factor 门径令是剖析因式。
SymPy 不仅能复返磋磨为止,还能生成 LaTeX 字符串 ,这恰是 Manim 渲染公式需要的形状:
from sympy import latex
latex_code = latex(expr_expanded) print(latex_code) # 输出:x^{2} - x - 6
有了这个“发动机”,咱们只需要告诉 SymPy 首先和非常,中间的整个代数变形它王人能自动磋磨。
3. Manim 联动实战:让公式自动推导底下通过一个弧线绘制的示例来演示 SymPy 奈何让 Manim 动画愈加苟简。
为了粗略绘制弧线的门径粗略通用,咱们封装几个函数:
from sympy import symbols, sympify, solve, Eq, latex, lambdify
def plot_function_curve(axes, equation_str, x_range, plot_range): """ 绘制函数弧线
Args: axes: 坐标系对象 equation_str: 方程字符串,如 "x**2 - x - 6" x_range: 画图 x 界限,如 [-3.5, 4.5] plot_range: 用于 sympy 求解的 x 界限
Returns: graph: 弧线对象 graph_label: 弧线标签 """ x = symbols("x") expr = sympify(equation_str)
# 创建可调用函数
f = lambdify(x, expr, modules="numpy")
# 绘制弧线
graph = axes.plot(f, x_range=x_range, color=YELLOW, stroke_width=3)
# 创建标签
graph_label = axes.get_graph_label( graph, label=f"f(x)={latex(expr)}",赛马投注中国app官方版下载 x_val=x_range[1], direction=UP * 3 )
return graph, graph_label
def find_roots(equation_str): """ 使用 SymPy 求解方程的根
Args: equation_str: 方程字符串,如 "x**2 - x - 6"
Returns: roots: 实数根列表 """ x = symbols("x") expr = sympify(equation_str)
# 求解方程 f(x) = 0
solutions = solve(Eq(expr, 0), x)
# 只保留实数根
roots = [] for sol in solutions: if sol.is_real: roots.append(float(sol))
return roots
def create_root_markers(axes, roots): """ 创建零点标记和标签
Args: axes: 坐标系对象 roots: 根的列表
Returns: root_dots: 零点标记组 root_labels: 零点标签组 """ root_dots = VGroup root_labels = VGroup
for rx in roots: # 在零点处画点
dot = Dot(axes.c2p(rx, 0), color=RED, radius=0.1) # 添加坐标标签
label = MathTex(f"({rx}, 0)", font_size=30, color=RED).next_to(dot, UP * 0.5) root_dots.add(dot) root_labels.add(label)
return root_dots, root_labels
其中:
plot_function_curve :左证弧线方程的字符串(比如 "x**2 - x - 6" )来绘制弧线find_roots :求解弧线方程的根,开云体育APP也便是和弧线和 X 轴的交点create_root_markers :把弧线方程的根标记出来有了上头封装的函数,绘制一个弧线的动画代码就相当简略了。
class PlotQuadraticFunction(Scene):
def construct(self):
# 1. 创建坐标系
axes = Axes( x_range=[-4, 5, 1], # x 轴界限:-4 到 5,步长 1
y_range=[-8, 10, 2], # y 轴界限:-8 到 10,步长 2
x_length=8, y_length=6, axis_config={"color": BLUE, "include_numbers": True, "font_size": 24}, x_axis_config={"numbers_to_include": range(-4, 6)}, y_axis_config={"numbers_to_include": range(-8, 11, 2)}, ) axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
# 界说方程
equation = "x**2 - x - 6"
# 绘制函数图像
graph, graph_label = plot_function_curve( axes, equation, x_range=[-3.5, 4.5], plot_range=[-4, 5] )
# 使用 SymPy 求解零点
roots = find_roots(equation)
# 创建零点标记
root_dots, root_labels = create_root_markers(axes, roots)
# 动画展示
self.play(Create(axes), Write(axes_labels)) self.wait
self.play(Create(graph), Write(graph_label)) self.wait
self.play( LaggedStart( *[Create(dot) for dot in root_dots], *[Write(label) for label in root_labels], lag_ratio=0.3, ) ) self.wait
上头的代码看着长,其实要道的就一滑:equation = "x**2 - x - 6" 。
其他部分王人是渲染坐标系,创建各式元素的动画等等。
生成的成果如下:
使用 SymPy 中中枢的几个门径如下:
sympify :比如, expr = sympify(equation_str) 将弧线方程的字符串援手成 SymPy 中的标志公式lambdify : SymPy 中的标志公式援手为 Python 函数(供 Manim 绘制弧线用)latex :比如, latex(expr) ,将 SymPy 中的标志公式援手为 Manim 中走漏公式的 Latex 形状solve : solve(Eq(expr, 0), x) ,求解方程的根,也便是动画中标记的与 X 轴的交点只从上头示例中的代码,可能还看不出来上 SymPy 和 Manim 蚁集的威力。
这本领,奈何咱们还念念绘制另一个弧线,x 3 − 6 x 2 + 11 x − 6 x^3-6x^2+11x-6 x 3 − 6 x 2 + 11 x − 6 ,那么,惟有改一滑代码就行了。
equation = "x**2 - x - 6" 改成 "x**3 - 6*(x**2) +11*x - 6"
这里走漏的有点叠加,咱们不错左证弧线走漏的情况,援手下坐标系或弧线参数的界限。
axes = Axes(
x_range=[-2, 5, 1],
# ...
x_axis_config={"numbers_to_include": range(-1, 6)}, # ...
)
# 绘制函数图像
graph, graph_label = plot_function_curve( axes, equation, x_range=[-1, 4.5], plot_range=[-1, 5] # 援手了这里的界限
)
4. 小结本文主要本色如下: 痛点知道 :手动推导公式既低效又容易出错。SymPy 中枢手段 : expand 伸开、 factor 因式剖析、 latex 援手。Manim 联动套路 : SymPy 算 → LaTeX 转 → Manim 渲染。任何代数方程的 因式变化 或 弧线函数 王人不错 自动化生成 KaiYun Sports2026世界杯(中国)IOS/安卓官方下载,咱们不错把元气心灵蚁集在动画节律和视觉野心上。