Skip to content Skip to sidebar Skip to footer

Sympy Error When Using Poly With Sqrt

I have the following code: from sympy import * a = Symbol('a') p = Poly(sqrt(a), domain=QQ) p.eval(a,2) What I expect after eval is the square-root of 2. however what I get is: Va

Solution 1:

The actual full error is

Traceback (most recent calllast):
  File "./sympy/polys/polytools.py", line 1701, in _gen_to_level
    return f.gens.index(sympify(gen))
ValueError: tuple.index(x): x notin tuple

During handling of the above exception, another exception occurred:

Traceback (most recent calllast):
  File "<stdin>", line 3, in<module>
  File "./sympy/polys/polytools.py", line 2308, in eval
    j = f._gen_to_level(x)
  File "./sympy/polys/polytools.py", line 1704, in _gen_to_level
    "a valid generator expected, got %s" % gen)
sympy.polys.polyerrors.PolynomialError: a valid generator expected, got a

The issue is that

>>> Poly(sqrt(a))
Poly((sqrt(a)), sqrt(a), domain='ZZ')

You've given Poly an expression that isn't a polynomial, but it tries to create a polynomial out of it anyway, by making it a polynomial in sqrt(a) instead of just a.

In general, I would avoid this sort of thing, unless you explicitly know what you are doing. It's always good practice to pass the generators to Poly that you expect your expression to be a polynomial in.

>>> Poly(sqrt(a), a)
Traceback (most recent calllast):
...
sympy.polys.polyerrors.PolynomialError: sqrt(a) contains an element of the generators set

Post a Comment for "Sympy Error When Using Poly With Sqrt"