gpt4 book ai didi

python - sympy : keep coefficients and variables together 的基本代数

转载 作者:行者123 更新时间:2023-11-28 17:27:16 28 4
gpt4 key购买 nike

我正在尝试将 sympy 用于一些基本的代数和物理学。当定义一个等式时,例如

import sympy as sy
g,h = sy.Symbols("g h")
v = sy.sqrt(2 * g * h)

等式 v 展开为

v = sqrt(2) * sqrt(g * h)

有一种方法可以指定不应从中分解出系数“2”在平方根下,平方根下的项保持为“2 * g * h”?我想这样做的原因是基础物理和所得方程的推导有时更清楚地查看相关系数是否被分解。

另一个例子是像在等式中计算弧长

phi = sy.symbols("phi")
r = sy.symbols("r")
a= r * phi

角度的两倍

phi = sy.pi / 4

a = r * (2 * phi)

收集到

a = r * sy.pi / 2

什么时候

a = r *  2 * (sy.pi / 4)

可能会更清楚地显示结果是如何获得的。

最佳答案

is there a way to specify that the coefficient "2" should not be factored out from under the square root and the term under the sqare root is kept as "2 * g * h"?

是的,只需使用可选参数 evaluate=False 创建一个 Pow 对象(power raise):

>>> Pow(2*g*h, S(1)/2, evaluate=False)
_______
\/ 2*g*h

也就是说,2*g*hS(1)/2 引发(二分之一)。 evaluate=False 禁用对象构造期间的自动评估。

关于python - sympy : keep coefficients and variables together 的基本代数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870629/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com