Chikorita Rai
Chikorita Rai

Reputation: 951

Finding coefficient of a specific term using sympy

I am trying to find coefficient of xy using sympy. This is the program.

import sympy as sp
fro sympy import Poly
from sympy.abc import x, y
x,y,xy=sp.symbols('x,y,xy')
K=Poly(x**2 + 5*xy + 1).coeff(xy)
print K

Kindly help. Thanks in advance.

Upvotes: 1

Views: 5257

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

import sympy as sp

from sympy.abc import x, y
x,y,xy=sp.symbols('x,y,xy')
expression = x**2 + 5*xy + 1
print expression.coeff(xy)

Note that this is probably not what you want. You're not looking for a symbol xy but for the coefficient with the crossterm of symbols x and y. You should therefor change the expression to:

expr = x**2 + 5*x*y+1
expr.coeff(y).coeff(x)

Upvotes: 3

Related Questions