Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To extract the independent variable from a SymPy Poly object, you can use the gens attribute. The gens attribute returns a tuple of the generator symbols of the Poly. The independent variable is typically the first generator symbol in the tuple.

For example, consider the following SymPy Poly object:

from sympy import Poly, symbols

x, y = symbols('x y')
p = Poly(x**2 + 2*x*y**2, x, y)

To extract the independent variable from p, you can use the gens attribute as follows:

independent_variable = p.gens[0]
print(independent_variable)  # Output: x

Note that the gens attribute returns a tuple, so you can access the generator symbols by their index in the tuple. In this case, since x is the first generator symbol, we access it with index 0.