Ask Your Question
2

error using algorithm="sympy" on an integral

asked 2018-06-18 02:22:48 +0100

Nasser gravatar image

updated 2018-06-18 06:55:57 +0100

Hello, sorry if this is basic question, but I just started yesterday using sage.

When I set algorithm to sympy, I get strange error. But for all other algorithms, it works ok. May be because I am using beta version? SageMath version 8.3.beta5, Release Date: 2018-06-09

 var('t')
sage: integrate(log(t)/(t + 1),t, algorithm="maxima")
log(t + 1)*log(t) + dilog(-t)

sage: integrate(log(t)/(t + 1),t, algorithm="fricas")
integral(log(t)/(t + 1), t)

sage: integrate(log(t)/(t + 1),t, algorithm="giac")
integrate(log(t)/(t + 1), t)

sage: integrate(log(t)/(t + 1),t, algorithm="sympy")

gives

AttributeError                            Traceback (most recent call last)
<ipython-input-18-636e47034533> in <module>()
----> 1 integrate(log(t)/(t + Integer(1)),t, algorithm="sympy")

/usr/lib/python2.7/site-packages/sage/misc/functional.py in integral(x, *args, **kwds)
    751     """
    752     if hasattr(x, 'integral'):
--> 753         return x.integral(*args, **kwds)
    754     else:
    755         from sage.symbolic.ring import SR

/usr/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.integral (build/cythonized/sage/symbolic/expression.cpp:69762)()
  12345                     R = ring.SR
  12346             return R(integral(f, v, a, b, **kwds))
> 12347         return integral(self, *args, **kwds)
  12348 
  12349     integrate = integral

/usr/lib/python2.7/site-packages/sage/symbolic/integration/integral.py in integrate(expression, v, a, b, algorithm, hold)
    814         if not integrator:
    815             raise ValueError("Unknown algorithm: %s" % algorithm)
--> 816         return integrator(expression, v, a, b)
    817     if a is None:
    818         return indefinite_integral(expression, v, hold=hold)

/usr/lib/python2.7/site-packages/sage/symbolic/integration/external.py in sympy_integrator(expression, v, a, b)
     54     else:
     55         result = sympy.integrate(ex, (v, a._sympy_(), b._sympy_()))
---> 56     return result._sage_()
     57 
     58 def mma_free_integrator(expression, v, a=None, b=None):

/usr/lib/python2.7/site-packages/sage/interfaces/sympy.py in _sympysage_piecewise(self)
    508     """
    509     from sage.functions.other import cases
--> 510     return cases([(p.cond._sage_(),p.expr._sage_()) for p in self.args])
    511 
    512 def _sympysage_fresnels(self):

/usr/lib/python2.7/site-packages/sage/interfaces/sympy.py in _sympysage_add(self)
    190     s = 0
    191     for x in self.args:
--> 192         s += x._sage_()
    193     return s
    194 

/usr/lib/python2.7/site-packages/sage/interfaces/sympy.py in _sympysage_mul(self)
    204     s = 1
    205     for x in self.args:
--> 206         s *= x._sage_()
    207     return s
    208 

/usr/lib/python2.7/site-packages/sage/interfaces/sympy.py in _sympysage_function(self)
    273     fname = self.func.__name__
    274     func = getattr(sagefuncs, fname, None)
--> 275     args = [arg._sage_() for arg in self.args]
    276 
    277     # In the case the function is not known in sage:

AttributeError: 'TupleArg' object has no attribute '_sage_'
sage:

Sagemath seems to ship with its own python. So may be this is an why? I have python 3.6.5 installed on my Linux, and it works ok there, with sympy 1.1.1

[me@me-pc rubi_4_15_2]$ python --version
Python 3.6.5 :: Anaconda, Inc.

[me@me-pc rubi_4_15_2]$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> t=symbols('t')
>>> integrate(log(t)/(t + 1),t)

Piecewise((I*pi*log(t + 1) - polylog(2, t + 1), Abs(t + 1) < 1), (-I*pi*log(1/(t + 1)) - polylog(2, t + 1), Abs(1/(t + 1)) < 1), (-I*pi*meijerg(((), (1, 1)), ((0, 0), ()), t + 1) + I*pi*meijerg(((1, 1), ()), ((), (0, 0)), t + 1) - polylog(2, t + 1), True))
>>>

And sage seems to be using sympy 1.1.1 as well

sage: import sympy
sage: print(sympy.__version__)
1.1.1

So why this error only shows up from inside sage and not from python itself, both calling same sympy?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2018-06-18 11:46:33 +0100

Emmanuel Charpentier gravatar image

updated 2018-06-18 12:10:52 +0100

When you call integrate(<...>, algorithm="sympy"), what happens is essentially:

  • conversion of the arguments (but the last) to sympy objects ;
  • call of the sympy.integrate function (or method) ;
  • conversion of the result back to Sage objects.

This last step fails in your specific case :

sage: var("t")
t
sage: import sympy
sage: foo=sympy.integrate(sympy.sympify(log(t)/(t+1)),[t]); foo
Piecewise((I*pi*log(t + 1) - polylog(2, t + 1), Abs(t + 1) < 1), (-I*pi*log(1/(t + 1)) - polylog(2, t + 1), Abs(1/(t + 1)) < 1), (-I*pi*meijerg(((), (1, 1)), ((0, 0), ()), t + 1) + I*pi*meijerg(((1, 1), ()), ((), (0, 0)), t + 1) - polylog(2, t + 1), True))
sage: type(foo)
Piecewise
sage: foo._sage_()
...
AttributeError: 'TupleArg' object has no attribute '_sage_'

meaning that Sage does not (currently) have methods to convert a Sympy Piecewise object to a Sage piecewise object. BTW, since Sympy's Piecewise and Sage's piecewise have different semantics, this would be a bit problematic.

You can still access the various parts of Sympy's answer by indexing the result :

sage: foo.args[0]
(I*pi*log(t + 1) - polylog(2, t + 1), Abs(t + 1) < 1)
sage: foo.args[0][0]
I*pi*log(t + 1) - polylog(2, t + 1)
sage: foo.args[0][0]._sage_()
I*pi*log(t + 1) - dilog(t + 1)

Note that in the future, conversion methods may be added to Sage to handle these cases (Ralf Stefan has started a large set of additions in this direction). Note also that in this specific case, foo[2][0] won't translate back to Sage (which currently doesn't know meijerg functions).

edit flag offensive delete link more

Comments

Note that the above answer completely ignores the validity of the result...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2018-06-18 12:13:20 +0100 )edit

Implementing Meijer G function in SageMath is tracked at

slelievre gravatar imageslelievre ( 2019-02-07 22:54:47 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-06-18 02:22:48 +0100

Seen: 543 times

Last updated: Jun 18 '18