Search code examples
pythonwolfram-mathematicasympy

Python (+SymPy): How to get the same result as in Mathematica?


I calculated the following using Mathematica and Python.

enter image description here

Mathematica with the following code

f[x_] = a*b/(a - b)^2*Exp[-r*x] (Exp[-b*x] - Exp[-a*x]) (a*Exp[-b*x] - b*Exp[-a*x])
Assuming[{a > 0, b > 0, r > 0}, Integrate[f[x], {x, 0, \[Infinity]}]]

gives a rather nice result:

enter image description here

But, the following Python (with SymPy) code

from sympy import *
init_printing()
x = symbols('x')
a, b, r = symbols('a b r', positive=True)
fun = a*b/((a-b)**2) * exp(-r*x) * (exp(-b*x) - exp(-a*x)) * (a*exp(-b*x) - b*exp(-a*x))
simplify(integrate(fun, (x, 0, oo)))

generates a rather messy result:

enter image description here

What am I missing in the Python code to obtain the same result in Mathematica? Or is it possible at all?


Solution

  • The function cancel can be used to cancel fractions:

    from sympy import *
    init_printing()
    x = symbols('x')
    a, b, r = symbols('a b r', positive=True)
    fun = a*b/((a-b)**2) * exp(-r*x) * (exp(-b*x) - exp(-a*x)) * (a*exp(-b*x) - b*exp(-a*x))
    factor(cancel(integrate(fun, (x, 0, oo)))
    

    gives

          a⋅b⋅(2⋅a + 2⋅b + r)
    ───────────────────────────────
    (2⋅a + r)⋅(2⋅b + r)⋅(a + b + r)