中的给定多项式中提取系数和相应的单项式

中的给定多项式中提取系数和相应的单项式

本文介绍了从 SymPy 中的给定多项式中提取系数和相应的单项式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个符号多元多项式P,我需要将其系数和相应的单项式提取为列表:

Given a symbolic multivariate polynomial P, I need to extract both its coefficients and corresponding monomials as lists:

def poly_decomp(P):
    ....
    return coeffs, monoms

使得 P 是系数和单项式的点积,例如,如果 P(x,y) = ax**2 + bxy + cy**2那么我们应该得到 coeffs = [a, b, c]monoms = [x**2, x*y, y**2].

such that P is the dot product of coefficients and monomials, e.g., if P(x,y) = ax**2 + bxy + cy**2 then we should get coeffs = [a, b, c] and monoms = [x**2, x*y, y**2].

获得系数很容易,因为函数内置于coeffs = P.coeffs().但是,我无法获得单项式.这里内置函数返回一个指数列表,例如,在上面的例子中,我们将得到 P.monoms() = [(2,0),(1,1),(0,2)].

Getting the coefficients is easy since the function is built in coeffs = P.coeffs(). However, I'm having trouble getting the monomials. Here the build in function returns a list of exponents, e.g., in the example above we would get P.monoms() = [(2,0),(1,1),(0,2)].

显然的想法是,提供一个变量列表var=[x,y],来做类似的事情

Obviously the idea would be, provided a list of the variables var=[x,y], to do something like

powers = P.monoms()
monoms = [sympy.prod(x**k for x,k in zip(var, mon)) for mon in powers ]

然而,多项式类似乎没有提供返回变量列表的函数.我能找到的只有 free_symbolsfree_symbols_in_domain 方法,它们返回集合 {a, b, c, x, y}{a, b, c}.因此,通过取它们的差异,可以得到 set {x, y}.

However the polynomial class doesn't seem to offer a function that returns a list of variables. All I could find were the methods free_symbols and free_symbols_in_domain which return the sets {a, b, c, x, y} and {a, b, c}. So by taking their difference one could get the set {x, y}.

然而,我们面临的问题是集合是无序的,因此将其转换为列表可能会根据变量的数量以不同的方式弄乱顺序.

However then we are faced with the issue that the sets are unordered, hence converting it into a list might mess up the order in different ways depending on the number of variables.

我有点不知所措.有什么提示吗?

I am kind of at a loss here. Any tips?

推荐答案

属性 gens(generators 的缩写)包含一个 tuple符号或其他合适的对象,定义了多项式.

The property gens (short for generators) holds a tuple of symbols or other suitable objects that the polynomial is defined over.

from sympy import symbols, Poly

x, y = symbols('x y')
p = Poly(x**3 + 2*x**2 + 3*x*y + 4*y**2 + 5*y**3, x, y)
q = Poly(x**3 + 2*x**2 + 3*x*y + 4*y**2 + 5*y**3, y, x)
print(p.gens)  # (x, y)
print(q.gens)  # (y, x)

所以,

[prod(x**k for x, k in zip(p.gens, mon)) for mon in p.monoms()]

返回[x**3, x**2, x*y, y**3, y**2].

另请注意,生成器可以是符号以外的类型,例如:

Note also that the generators can be types other than symbols, for example:

import sympy

x = sympy.symbols('x')
poly = sympy.poly(sympy.sqrt(2) * x**2)
print('generators: {g}'.format(g=poly.gens))
print('monomials: {m}'.format(m=poly.monoms()))
print('coefficients: {c}'.format(c=poly.coeffs()))

打印:

generators: (x, sqrt(2))
monomials: [(2, 1)]
coefficients: [1]

哪里:

  • type(poly.gens[0]),和
  • type(poly.gens[1]).
  • type(poly.gens[0]) is <class 'sympy.core.symbol.Symbol'>, and
  • type(poly.gens[1]) is <class 'sympy.core.power.Pow'>.

一个相关的方法是sympy.polys.polytools.Poly.as_dict,它返回一个dict,键是单项式,值是相应的系数.

A relevant method is sympy.polys.polytools.Poly.as_dict, which returns a dict with keys that are monomials, and values that are the corresponding coefficients.

这篇关于从 SymPy 中的给定多项式中提取系数和相应的单项式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:08