Declare string as a "constant" in sympy

sympy

Solution

The way to do this is to specify the generators of the polynomials when you create them. For instance, to treat only `y` as a variable, use

f = Poly(x*y + y**2, y)

By default, `Poly` assumes that all the symbols in an expression should be generators.

You can also pass the generator as the third argument to gcdex

s, t, gcd = gcdex(f, g, y)

gives

(s, t, gcd) == (Poly(0, y, domain='ZZ(x)'), Poly(1, y, domain='ZZ(x)'), Poly(x + y, x, y, domain='ZZ'))

Problem

I'm trying to implement an algorithm which involves polynomials in two variables "x" and "y", but some times I need to interpret them as univariable polynomials (that is, leaving x as a constant), for example, in order use the function gcdex (the extended euclidean algorithm). Is there a simple way to make sympy interpret "x" as a constant instead of a variable? I've tried the following: ``` import sympy x = sympy.Symbol('x', constant=True) y = sympy.Symbol('y') f = sympy.Poly(x*y + y**2) g = sympy.Poly(x+y) (s, t, gcd) = sympy.gcdex(f,g) ``` but it throws an error: univariate polynomials expected.

Original source