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

Non-deterministic exception from integrals/poly #17132

Open
oscarbenjamin opened this issue Jun 30, 2019 · 1 comment
Open

Non-deterministic exception from integrals/poly #17132

oscarbenjamin opened this issue Jun 30, 2019 · 1 comment

Comments

@oscarbenjamin
Copy link
Contributor

The script below fails non-deterministically for me:

from sympy import *

t = Symbol('t')
f = t*exp(-3*t**2/2) + exp(t**2/2)
print(integrate(f, t))

Half the time I get

$ python z.py 
sqrt(2)*sqrt(pi)*erfi(sqrt(2)*t/2)/2 - exp(-3*t**2/2)/3

which I think is the right answer.

The other half of the time I get:

$ python z.py 
Traceback (most recent call last):
  File "z.py", line 5, in <module>
    print(integrate(f, t))
  File "/Users/enojb/current/sympy/sympy/sympy/integrals/integrals.py", line 1520, in integrate
    return integral.doit(**doit_flags)
  File "/Users/enojb/current/sympy/sympy/sympy/integrals/integrals.py", line 573, in doit
    antideriv = self._eval_integral(
  File "/Users/enojb/current/sympy/sympy/sympy/integrals/integrals.py", line 905, in _eval_integral
    result, i = risch_integrate(f, x, separate_integral=True,
  File "/Users/enojb/current/sympy/sympy/sympy/integrals/risch.py", line 1754, in risch_integrate
    ans, i, b = integrate_hyperexponential(fa, fd, DE, conds=conds)
  File "/Users/enojb/current/sympy/sympy/sympy/integrals/risch.py", line 1521, in integrate_hyperexponential
    i = p - (qd*derivation(qa, DE) - qa*derivation(qd, DE)).as_expr()/\
  File "/Users/enojb/current/sympy/sympy/sympy/integrals/risch.py", line 898, in derivation
    r += (d*pv.diff(v)).as_poly(t)
TypeError: unsupported operand type(s) for +=: 'Poly' and 'NoneType'

This happens with both Python 3.5 and 3.8 on OSX.

@jksuom
Copy link
Member

jksuom commented Jul 1, 2019

There is a weakness in this construction where va*t1**i/vd is added to qa/qd

sympy/sympy/integrals/risch.py

Lines 1459 to 1460 in 1d3327b

qa = qa*vd + va*Poly(t1**i)*qd
qd *= vd

integrate_hyperexponential_polynomial actually has to deal with Laurent polynomials. Positive and negative and exponents are handled separately earlier in the loop

sympy/sympy/integrals/risch.py

Lines 1439 to 1447 in 1d3327b

elif i < 0:
# If you get AttributeError: 'NoneType' object has no attribute 'nth'
# then this should really not have expand=False
# But it shouldn't happen because p is already a Poly in t and z
a = p.as_poly(z, expand=False).nth(-i)
else:
# If you get AttributeError: 'NoneType' object has no attribute 'nth'
# then this should really not have expand=False
a = p.as_poly(t1, expand=False).nth(i)

However, on line 1459 we now get polynomials with inverted generators for negative exponents, and those lead to problems later on:

In [6]: Poly(t**-3)
Out[6]: Poly((1/t)**3, 1/t, domain='ZZ')

It seems that we should put Poly(t1**-i) in the denominator qd in case i < 0.

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