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
Integral fails to evaluate #17119
Comments
Not sure if it solves this, but it is generally advisable to write S(1)/2 or Rational(1, 2) as it will keep the exponent in integer/rational form. 1/2 will be evaluated into a float (0.5) before it enters SymPy. Floats have a number of potential problems associated with them when doing symbolic maths. |
That is good advice for working around issues with SymPy. Thanks! |
It's not just a question of working around the issue. SymPy deliberately treats floats as inexact numbers - you shouldn't assume that 0.5 and |
I'm not assuming they're equivalent. I'm assuming SymPy shouldn't crash with reasonable input. |
Good point :) Apparently this can be evaluated for an arbitrary symbol instead of a float: In [17]: integrate(x**a*(1+x), x)
Out[17]:
⎧ 1
⎪ log(x) - ─ for a = -2
⎪ x
⎪
⎪ x + log(x) for a = -1
⎨
⎪ 2 a a 2 a a
⎪ a⋅x ⋅x a⋅x⋅x x ⋅x 2⋅x⋅x
⎪──────────── + ──────────── + ──────────── + ──────────── otherwise
⎪ 2 2 2 2
⎩a + 3⋅a + 2 a + 3⋅a + 2 a + 3⋅a + 2 a + 3⋅a + 2
In [18]: integrate(x**a*(1+x), x).subs(a, 0.5)
Out[18]:
1.5 2.5
0.666666666666667⋅x + 0.4⋅x I wonder if something like that might be a good general approach for integrals with floats in because some methods seem very sensitive to them. |
This diff causes a few test failures but looks manageable: diff --git a/sympy/integrals/integrals.py b/sympy/integrals/integrals.py
index 49e955d388..359c955cfb 100644
--- a/sympy/integrals/integrals.py
+++ b/sympy/integrals/integrals.py
@@ -9,7 +9,7 @@
from sympy.core.function import diff
from sympy.core.logic import fuzzy_bool
from sympy.core.mul import Mul
-from sympy.core.numbers import oo, pi
+from sympy.core.numbers import oo, pi, Float
from sympy.core.relational import Ne
from sympy.core.singleton import S
from sympy.core.symbol import (Dummy, Symbol, Wild)
@@ -385,6 +385,12 @@ def doit(self, **hints):
if not hints.get('integrals', True):
return self
+ if self.has(Float):
+ fvals = self.atoms(Float)
+ fsubs = {f: Dummy() for f in fvals}
+ rsubs = {s: f for f, s in fsubs.items()}
+ return self.subs(fsubs).doit(**hints).subs(rsubs)
+
deep = hints.get('deep', True)
meijerg = hints.get('meijerg', None)
conds = hints.get('conds', 'piecewise') and fixes this issue: In [1]: integrate(x**(0.5)*(1+x))
...:
Out[1]:
1.5 2.5
0.666666666666667⋅x + 0.4⋅x There are a number of other Float/Integral issues that this might help with as well. |
Also fixes #14431 (possible duplicate of this issue): In [9]: integrate((x-t)**(-1/2)*t,(t,0,x))
Out[9]:
1.5
1.33333333333333⋅x |
Tested with sympy 1.4, issue repros on https://live.sympy.org/
integrate(x**(0.5)*(1+x))
crashes with ValueError: Non-suitable parameters.
The following both evaluate:
integrate(x**(1/2)*(1+x))
integrate( expand(x**0.5*(1+x)) )
This is the result from live.sympy.org:
The text was updated successfully, but these errors were encountered: