# how to correctly check that integration evaluated?

What is the correct way in sage to check that the result of integration has evaluated? I.e. the result has no unevaluated integrate remaining in it?

The way I am checking fail in some cases. For example

sage: var('x')
sage: anti=integrate((4*x - sqrt(-x^2 + 1))/(sqrt(-x^2 + 1) + 5), x)

sage: anti
-x - 4*sqrt(-x^2 + 1) + 5*integrate(1/(sqrt(x + 1)*sqrt(-x + 1) + 5), x) + 20*log(sqrt(-x^2 + 1) + 5)


The above did not fully evaluate, since there is an integrate inside the result returned. But when I do

sage: isinstance(anti.operator(), sage.symbolic.integration.integral.IndefiniteIntegral)
False


I was expecting to get True. It failed the check, because integrate was not the first operator. This one works

sage: anti=integrate(1/(sqrt(x + 1)*sqrt(-x + 1) + 5), x)
sage: isinstance(anti.operator(), sage.symbolic.integration.integral.IndefiniteIntegral)
True


So what is the correct way to tell sage to scan the whole expression and if there is an instance of integrate any where, to return True

Thank you

--Nasser

edit retag close merge delete

Sort by » oldest newest most voted

Does this help? this is just an idea. You can improve it, of course.

var('x')
anti=integrate((4*x - sqrt(-x^2 + 1))/(sqrt(-x^2 + 1) + 5), x)
#anti=integrate(1/(sqrt(x + 1)*sqrt(-x + 1) + 5), x)
if str(anti).find("integrate") != -1:
print("Result contains unevaluated integral!")
anti


Output:

Result contains unevaluated integral!
-x - 4*sqrt(-x^2 + 1) + 5*integrate(1/(sqrt(x + 1)*sqrt(-x + 1) + 5), x) + 20*log(sqrt(-x^2 + 1) + 5)

more

You must parse (recursively) the output expression, and tchack that tour tree does not contain any integrate call (i. e. a subexpression whose operator is sage.all.integrate. This must be recursive because integrate may return a sum or a product whose one term or factr is an unevaluated integrate call...

The implementation is Left As An Exercise (TM) to the reader (who may be wise to check if a general parsng function exists in Sage... or in some Python library...).

more