EN

滚球新闻

滚球新闻

滚球(中国)官网app 让数学公式自动推导

发布日期:2026-05-19 06:32 来源:未知 作者:admin 浏览次数:

滚球(中国)官网app 让数学公式自动推导

咱们在作念数学公式推导的视频时,比如,念念展示一个因式证据: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 +

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)}", 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 渲染。任何代数方程的 因式变化 或 弧线函数 齐不错 自动化生成 滚球(中国)官网app,咱们不错把元气心灵靠拢在动画节拍和视觉狡计上。

上一篇:上一篇:滚球app 特雷克斯2026年Q1功绩超预期, EBITDA利润率同比下滑
下一篇:下一篇:没有了