确定 SymPy 表达式的各个项

发布于 2025-01-09 00:41:07 字数 637 浏览 0 评论 0原文

对于我正在从事的项目,我必须从任意 SymPy 表达式中选择特定术语。为此,我使用 .args 如果表达式包含多个不同的项,它可以正常工作,但如果仅存在单个项,则返回项中的各个系数。请参阅以下示例:

import sympy as sym

x, c0, c1 = sym.symbols('x c0 c1')

f0 = c0*x
f1 = c1*x**2
f2 = c0*x + c1*x**2

print(f0.args) # Output: (c0, x)    Desired: (c0*x)
print(f1.args) # Output: (c1, x**2) Desired: (c1*x**2)
print(f2.args) # Output: (c0*x, c1*x**2)

此方法的工作原理与我希望的 f2 一样,它返回 (c0*x, c1*x**2)。然而,对于f0f1,它分别返回系数c0、c1x。对于单项表达式,系数和 x 也作为乘法表达式返回,我将如何实现所需的输出?

请注意,理想情况下,我希望此方法适用于任何形式的表达式,并且任意依赖于 x 和任意数量的系数。

For a project I am working on I have to select a specific term from an arbitrary SymPy expression. For this I am using .args which works fine if the expression consists of multiple distinct terms, but returns the individual coefficients in the term if only a single term is present. See the following example:

import sympy as sym

x, c0, c1 = sym.symbols('x c0 c1')

f0 = c0*x
f1 = c1*x**2
f2 = c0*x + c1*x**2

print(f0.args) # Output: (c0, x)    Desired: (c0*x)
print(f1.args) # Output: (c1, x**2) Desired: (c1*x**2)
print(f2.args) # Output: (c0*x, c1*x**2)

This method works as I would want it to for f2, where it returns (c0*x, c1*x**2). For f0 and f1 however it returns the coefficients c0, c1 and x separately. How would I go achieving the desired output where for the single term expressions the coefficients and x are also returned as a multiplicative expression?

Note that ideally I would want this method to work for any form of expression, with arbitrary dependencies on x and an arbitrary amount of coefficients.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮是我掰弯的 2025-01-16 00:41:08

功劳归于奥斯卡·本杰明。由于我无法接受他们的评论作为答案,因此我将其重新发布到此处,同时还提供了一个具体的示例。

“您可以使用 Add.make_args(f0)

这将如下所示:

import sympy as sym

x, c0, c1 = sym.symbols('x c0 c1')

f0 = c0*x
f1 = c1*x**2
f2 = c0*x + c1*x**2

print(sym.Add.make_args(f0))
print(sym.Add.make_args(f1))
print(sym.Add.make_args(f2))

它将返回:

(c0*x,)
(c1*x**2,)
(c0*x, c1*x**2)

Credit goes to Oscar Benjamin. Since I can't accept their comment as the answer I am reposting it here while also providing a concrete example.

"You can use Add.make_args(f0)"

This would then look like this:

import sympy as sym

x, c0, c1 = sym.symbols('x c0 c1')

f0 = c0*x
f1 = c1*x**2
f2 = c0*x + c1*x**2

print(sym.Add.make_args(f0))
print(sym.Add.make_args(f1))
print(sym.Add.make_args(f2))

Which would return:

(c0*x,)
(c1*x**2,)
(c0*x, c1*x**2)
眼泪淡了忧伤 2025-01-16 00:41:08

您需要使用poly来说明您有多项式:

import sympy as sym
from sympy import symbols, Poly


x, c0, c1 = sym.symbols('x c0 c1')

f0 = sym.poly(c0*x)
f1 = sym.poly(c1*x**2)
f2 = c0*x + c1*x**2

print(f0.args) # Output: (c0, x)    Desired: (c0*x)
print(f1.args) # Output: (c1, x**2) Desired: (c1*x**2)
print(f2.args) # Output: (c0*x, c1*x**2)

返回

(c0*x, x, c0)
(c1*x**2, x, c1)
(c0*x, c1*x**2)

以获取单项式,只需使用

f0.args[0]

which给出

c0*x

You need to use poly to state the fact that you have polynomials:

import sympy as sym
from sympy import symbols, Poly


x, c0, c1 = sym.symbols('x c0 c1')

f0 = sym.poly(c0*x)
f1 = sym.poly(c1*x**2)
f2 = c0*x + c1*x**2

print(f0.args) # Output: (c0, x)    Desired: (c0*x)
print(f1.args) # Output: (c1, x**2) Desired: (c1*x**2)
print(f2.args) # Output: (c0*x, c1*x**2)

returns

(c0*x, x, c0)
(c1*x**2, x, c1)
(c0*x, c1*x**2)

to get the monimials, simply use

f0.args[0]

which gives

c0*x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文