Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lots of PolynomialError contains an element of the set of generators exceptions calling integrate in sympy 1.12 #26502

Open
nasser1 opened this issue Apr 15, 2024 · 1 comment

Comments

@nasser1
Copy link

nasser1 commented Apr 15, 2024

I do not know if this is known or reported before. Feel free to close if so.

I see lots of exceptions from polyutils.py calling integrate command. Below is just one typical example

from sympy import *
x=symbols('x')
integrand="(-36-2*6**(1/2)*x**2)/(9*3**(1/2)+3*2**(1/2)*x**2-x**4)"
integrate( sympify(integrand), x)

gives

Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/sympy/polys/polyutils.py", line 211, in _parallel_dict_from_expr_if_gens
    monom[indices[base]] = exp
          ~~~~~~~^^^^^^
KeyError: 1/(2*_t**4 - 2*sqrt(2)*_t**2 + 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/site-packages/sympy/integrals/integrals.py", line 1567, in integrate
    return integral.doit(**doit_flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/integrals.py", line 612, in doit
    antideriv = self._eval_integral(
                ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/integrals.py", line 955, in _eval_integral
    result, i = risch_integrate(f, x, separate_integral=True,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/risch.py", line 1835, in risch_integrate
    ans = integrate(fa.as_expr()/fd.as_expr(), DE.x, risch=False)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/integrals.py", line 1567, in integrate
    return integral.doit(**doit_flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/integrals.py", line 612, in doit
    antideriv = self._eval_integral(
                ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/integrals.py", line 1041, in _eval_integral
    parts.append(coeff * ratint(g, x))
                         ^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/rationaltools.py", line 83, in ratint
    L = ratint_logpart(r, Q, x, t)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/integrals/rationaltools.py", line 231, in ratint_logpart
    res = Poly(res, t, composite=False)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/polys/polytools.py", line 182, in __new__
    return cls._from_expr(rep, opt)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/polys/polytools.py", line 311, in _from_expr
    rep, opt = _dict_from_expr(rep, opt)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/polys/polyutils.py", line 368, in _dict_from_expr
    rep, gens = _dict_from_expr_if_gens(expr, opt)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/polys/polyutils.py", line 307, in _dict_from_expr_if_gens
    (poly,), gens = _parallel_dict_from_expr_if_gens((expr,), opt)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
    raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: 1/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) contains an element of the set of generators.
>>> 

Here is another example

from sympy import *
x=symbols('x')
integrand="(3-2*2**(1/2)+x**2)**2*(-3+2*2**(1/2)+x**2)/(577-408*2**(1/2)-8* (-41+29*2**(1/2))*x**2-2*(-39+28*2**(1/2))*x**4-8*(2**(1/2)-1)*x**6+x**8)"
integrate( sympify(integrand), x)

gives

 File "/usr/lib/python3.11/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
    raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: 1/(-4893319121142556020618924174780472498117708482611714912381696*_t**4 + 3460099133069698398004476359279702930052248019321310378430976*sqrt(2)*_t**4 - 159769239484575670917838951113184628965915778476312975023968*_t**2 + 112973912664560957914881642683593608324699445462537847255936*sqrt(2)*_t**2 - 1304135764168161431882534016166025912200366086490350062499 + 922163242431207071074983301530248435746125497723607601750*sqrt(2)) contains an element of the set of generators.


And another example

from sympy import *
x=symbols('x')
integrand="(-3+2*2**(1/2)+x**2)/(17-12*2**(1/2)+(2-4*2**(1/2))*x**2+x**4)"
integrate( sympify(integrand), x)

Gives

 File "/usr/lib/python3.11/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
    raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: 1/(-2304*_t**4 + 1024*sqrt(2)*_t**4 - 32*_t**2 + 64*sqrt(2)*_t**2 - 1) contains an element of the set of generators.


Version info

>>> import sympy
>>> sympy.__version__
'1.12'

>python --version
Python 3.11.8
>

@oscarbenjamin
Copy link
Contributor

Somehow res here is not a polynomial in t:

f, g = Poly(f, x), Poly(g, x)
t = t or Dummy('t')
a, b = g, f - g.diff()*Poly(t, x)
res, R = resultant(a, b, includePRS=True)
res = Poly(res, t, composite=False)

Something very string happening here:

ipdb> p a
Poly(x**4 - 3*sqrt(2)*x**2 - 9*sqrt(3), x, domain='EX')
ipdb> p b
Poly(-4*_t*x**3 + 2*sqrt(6)*x**2 + 6*sqrt(2)*_t*x + 36, x, domain='EX')
ipdb> p resultant(a, b)
-1213056*sqrt(3)*_t**8/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) - 1119744*_t**8/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) + 2239488*sqrt(2)*_t**6/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) + 2426112*sqrt(6)*_t**6/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) - 1912896*sqrt(3)*_t**4/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) + 839808*_t**4/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) - 3079296*sqrt(2)*_t**2/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) - 513216*sqrt(6)*_t**2/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) + 559872*sqrt(3)/(2*_t**4 - 2*sqrt(2)*_t**2 + 1) + 1819584/(2*_t**4 - 2*sqrt(2)*_t**2 + 1)

The resultant should not have denominators like this. Maybe resultant is broken in the EX domain. Probably because EX uses cancel and cancel cannot handle sqrt(2) and sqrt(3) without extension=True. For EX probably a division free resultant algorithm should be used like actually computing the determinant of the Sylvester matrix or something.

skirpichev added a commit to skirpichev/diofant that referenced this issue Apr 21, 2024
Closes sympy/sympy#26502

Original test is slooow, used more simple version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants