在Sympy中隔离多元多项式的一个系数的最佳方法 [英] Best way to isolate one coefficient of a multivariate polynomial in sympy

查看:227
本文介绍了在Sympy中隔离多元多项式的一个系数的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多元多项式(在通常情况下,它包含许多变量),其系数列出了一些我需要读取的数据,但这似乎不像sympy提供了一种好的方法.

I have a multivariate polynomial (which in the general case many many variables) whose coefficients list some data that I need to read off, but it doesn't seem like sympy gives a good way to do this.

collect函数似乎是正确的主意,但是当您将其与多个变量一起使用时,实际上并没有给您单项式,而是取决于列出变量顺序的奇怪的单项式分组.

The collect function seemed like the right idea, but when you use it with several variables, it doesn't actually give you the individual monomials, but rather strange groupings of monomials that depend on the order you listed the variables.

有人知道这样做的方法吗?

Does anyone know of a way to do this?

推荐答案

文档多项式模块列出了许多处理系数的方法.例如:

The documentation of polynomial module lists plenty of ways to handle coefficients. For example:

>>> import sympy
>>> x,y,z = sympy.symbols('x,y,z')
>>> p = sympy.poly((x+2*y-z)**3)
>>> p.coeffs()
[1, 6, -3, 12, -12, 3, 8, -12, 6, -1]

这些是按字典顺序的非零系数.要按匹配顺序查看单项式样,请使用

These are nonzero coefficients in lexicographic order. To see the monomials in matching order, use

>>> p.monoms()
[(3, 0, 0), (2, 1, 0), (2, 0, 1), (1, 2, 0), (1, 1, 1), (1, 0, 2), (0, 3, 0), (0, 2, 1), (0, 1, 2), (0, 0, 3)]

要获取特定单项式的系数,请使用

To get the coefficient of a particular monomial, use

>>> p.coeff_monomial(x**2*y)
6

这篇关于在Sympy中隔离多元多项式的一个系数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆