Ask Your Question
1

isympy/sympy sagemath integration ?

asked 2020-07-09 22:06:01 +0200

rrogers gravatar image

I'm trying to integrate

integrate((1/x**a)*(1/(1-x)),x)

With sympy; which succeeds in isympy. But I need to separately import sympy

from sympy import *
x, a = symbols('x a')
integrate(1/(1-x)*1/(x^a),x)

Works, whereas simple

integrate(1/(1-x)*1/(x^a),x, algorithm="sympy")

fails when the import is missing. It also fails (differently) after the import :) only

integrate(1/(1-x)*1/(x^a),x)

works. I didn't see that mentioned.
No big problem, after I spent some time going down the subprocess error rabbit hole.
Is this a documentation error that should be reported?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-07-09 23:20:32 +0200

Emmanuel Charpentier gravatar image

Indeed:

sage: sympy.integrate(*map(sympy.sympify, [(1/x^a)*(1/(1-x)),x])).simplify()
x**(1 - a)*lerchphi(x, 1, 1 - a)

By the way :

sage: sx, sa=sympy.symbols("sx, sa")
sage: foo=sympy.integrate(1/(sx^sa)*(1/(1-sx)),sx).simplify() ; foo
sx**(1 - sa)*lerchphi(sx, 1, 1 - sa)
sage: type(foo)
<class 'sympy.core.mul.Mul'>

And, indeed,

sage: foo._sage_()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-59090414f898> in <module>()
----> 1 foo._sage_()

/usr/local/sage-9/local/lib/python3.7/site-packages/sage/interfaces/sympy.py in _sympysage_mul(self)
    217     s = 1
    218     for x in self.args:
--> 219         s *= x._sage_()
    220     return s
    221 

/usr/local/sage-9/local/lib/python3.7/site-packages/sage/interfaces/sympy.py in _sympysage_function(self)
    304         else:
    305             # the function defined in sympy is not known in sage
--> 306             raise AttributeError
    307 
    308     return func(*args)

AttributeError: 
sage:

This can be corrected at the price of implementing sympy's lerchphi (a. k. a. Lerch transcendent (Lerch phi function)) in Sage. Quite possibly as an interface to sympy's function...

HTH,

edit flag offensive delete link more

Comments

Well this is interesting and getting more complicated. I was running in Jupyter Sagemath 9.1 and after

from sympy import *                                                       
 x, a = symbols('x a')
integrate(1/(1-x)*1/(x^a),x)

works fine; expected symbol and answer. So I brought up a shell and typed Sage. (to get the Sage: prompt. ┌────────────────────────────────────────────────────────────────────┐ │ SageMath version 9.0, Release Date: 2020-01-01 │ │ Using Python 3.8.2. Type "help()" for help. │ └────────────────────────────────────────────────────────────────────┘

And using your code and import sympy , all I get is NameError: name 'sympy' is not defined BTW: running Jupyter in a new notebook with Python also fails :)

rrogers gravatar imagerrogers ( 2020-07-10 14:11:45 +0200 )edit

Why should I reimplement lerchphi() when I can just import sympy? Originally I was just wondering if there was a documentation error in Sagemath. And now, I don't understand the naming crosstalk!
I am not writing general purpose programs; just trying to generate and apply "fractional derivatives" to some Generalized Hypergeometric functions. Which you can recognize from the formula, everything seems to be running fine (for now). Sympy is the only program that recognizes this integral as far as I can see. But I have to go off script to use it in Jupyter.

rrogers gravatar imagerrogers ( 2020-07-10 14:20:56 +0200 )edit
1

That's more complicated that that : imported sympy functions are not usabe as Sage functions : you have to explicitely substitute their arguments in order to obtain variable-free sympy expressions (whic are evaluated correctly).

I'm searching in that direction.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-07-10 15:56:27 +0200 )edit

Thanks I am trying the answer(s) to your question!

rrogers gravatar imagerrogers ( 2020-07-11 14:22:51 +0200 )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

2 followers

Stats

Asked: 2020-07-09 22:06:01 +0200

Seen: 276 times

Last updated: Jul 09 '20