Best way to isolate one coefficient of a multivariate polynomial in sympy

后端 未结 2 1493
-上瘾入骨i
-上瘾入骨i 2020-12-17 20:46

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 g

2条回答
  •  隐瞒了意图╮
    2020-12-17 20:56

    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
    

提交回复
热议问题