mrrclb48z
mrrclb48z

Reputation: 207

Integration in SymPy raises "no attribute '_eval_power'" error

Why does integral_0^1 log(x)/(x^2 - 1) dx not work in SymPy?

AttributeError: 'Not' object has no attribute '_eval_power'

http://www.ms.u-tokyo.ac.jp/kyoumu/a20170524.pdf#page=4

(OK) Wolfram|Alpha Examples:

https://www.wolframalpha.com/input/?i=∫%5B0,1%5D+log(x)%2F(x%5E2-1)+dx

integral_0^1 log(x)/(x^2 - 1) dx = π^2/8?

1.2337

(??) sympy

from sympy import *
# var("x")
x = symbols('x', positive=True)
f=log(x)/(x^2-1)
print(integrate(f,(x, 0, 1)))
print(float(integrate(f,(x, 0, 1))))
# AttributeError: 'Not' object has no attribute '_eval_power'

Upvotes: 0

Views: 749

Answers (2)

user6655984
user6655984

Reputation:

Write f = log(x)/(x**2-1) because in Python, powers are denoted by ** (and ^ is XOR). This is why the error is thrown. However, SymPy is still unable to integrate that function: the integral returns unevaluated. These polylog-type nonelementary integrals give a lot of trouble to SymPy.

If you are okay with a floating point answer, then use numerical integration:

print(Integral(f,(x, 0, 1)).evalf())

which returns 1.23370055013617...

A thing worth trying with such integrals is nsimplify, which finds a symbolic answer than matches the outcome of numeric integration.

>>> nsimplify(Integral(f, (x, 0, 1)), [pi, E])
pi**2/8

Here the list [pi, E] includes the two most famous math constants, which are likely to appear in integrals. (Another constant that shows up often is EulerGamma).

Upvotes: 1

seralouk
seralouk

Reputation: 33147

In python, the power symbol is not ^ but **.

Use this:

from sympy import *
# var("x")
x = symbols('x', positive=True)
f=log(x)/(x**2-1)
print(integrate(f,(x, 0, 1)))

Results:

Integral(log(x)/((x - 1)*(x + 1)), (x, 0, 1))

Upvotes: 2

Related Questions