Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why sympy cannot calculate fraction power formula like (6-x*x)**(1.5)?

I used sympy to calculate some integral as follows.

#Calculate Calculus
import sympy
x = sympy.Symbol('x')
f = (6-x*x)**(1.5)
f.integrate()

This will fail and throw excepiton like:

ValueError: gamma function pole

It works fine if I just use an integer as power num

f = (6-x*x)**(2)
#result: x**5/5 - 4*x**3 + 36*x
like image 686
Pythoner Avatar asked Feb 08 '23 02:02

Pythoner


2 Answers

My guess is a 1.5 expression is treated as a floating point, which is imprecise. You'd want a symbolic (exact) representation, instead. (I would guess if you were after a computational integral a floating point would probably be okay, generally, as a math library that supports a computational integral would typically use an integral approximation method to compute the integral.) If you need to do arbitrary rational exponents, consider using sympy.Rational. Here's a relevant answer on StackOverflow that seems to support this. I think the documentation for sympy.Rational is here. You can try this modified code here:

#Calculate Calculus
import sympy
frac = sympy.Rational
x = sympy.Symbol('x')
f = (6-x*x)**(frac('1.5'))
f.integrate() 
like image 71
Jesus is Lord Avatar answered Feb 20 '23 12:02

Jesus is Lord


The previous answer is right, I'm just posting my final result here

import sympy
frac = sympy.Rational
x = sympy.symbols('x')
f1 = (x+3)/(6-x**2)**(frac('1.5'))
f1.integrate()
like image 37
Pythoner Avatar answered Feb 20 '23 12:02

Pythoner