# Variable type returned after integrating.

If I have an expression and I integrate it, the type of object that is returned is not constant.

> x = var('x')
> type((x**2).integral(x))
<type 'sage.symbolic.expression.Expression'>
> type((0*x).integral(x))
<type 'sage.rings.integer.Integer'>


This makes it quite hard to write functions that do things with Expressions. Is there some trick I'm missing to get around this?

An example of where this causes a problem:

> ff = Piecewise([[(-Infinity, -1), 0*x], [(-1, 1), x**0], [(1, Infinity), 0*x]], x)
> ff.integral(x, -1.0, 1.0)
Traceback (click to the left of this block for traceback)
...
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'function'

edit retag close merge delete

Sort by » oldest newest most voted

The problem with the Piecewise functions was reported in trac #10841 and a fix has been accepted and is pending for sage 4.7. It basically only adds four characters to cast the integrand to an Expression. If there are other functions which don't behave, could you report them to trac?

Integration does gives different type results depending on the ranges (e.g. for definite integrals you can easily get Integers, Rationals, RealDoubleElements, and so on.) You can work around this easily enough on the "outside" in your own code by explicitly casting to SR, but I admit it's sometimes hard to work around problems on the "inside", i.e. in built-in functions.

more

And if you report it to Trac, could you also cc: kcrisman on this? It's sometimes hard to track down all cases where this should be done. (Of course, then hoping that we don't break something because it doesn't return something in ZZ which requires a ZZ method!)

( 2011-04-08 09:56:37 +0200 )edit

That makes sense. Thank you.

( 2011-04-08 12:06:36 +0200 )edit