ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 08 Apr 2011 12:06:36 +0200Variable type returned after integrating.https://ask.sagemath.org/question/8038/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'Fri, 08 Apr 2011 00:17:29 +0200https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/Answer by DSM for <p>If I have an expression and I integrate it, the type of object that is returned is not constant.</p>
<pre><code>> x = var('x')
> type((x**2).integral(x))
<type 'sage.symbolic.expression.Expression'>
> type((0*x).integral(x))
<type 'sage.rings.integer.Integer'>
</code></pre>
<p>This makes it quite hard to write functions that do things with Expressions. Is there some trick I'm missing to get around this?</p>
<p>An example of where this causes a problem:</p>
<pre><code>> 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'
</code></pre>
https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/?answer=12271#post-id-12271The problem with the Piecewise functions was reported in [trac #10841](http://trac.sagemath.org/sage_trac/ticket/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.
Fri, 08 Apr 2011 03:01:53 +0200https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/?answer=12271#post-id-12271Comment by Ben Reynwar for <p>The problem with the Piecewise functions was reported in <a href="http://trac.sagemath.org/sage_trac/ticket/10841">trac #10841</a> 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?</p>
<p>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.</p>
https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/?comment=21886#post-id-21886That makes sense. Thank you.Fri, 08 Apr 2011 12:06:36 +0200https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/?comment=21886#post-id-21886Comment by kcrisman for <p>The problem with the Piecewise functions was reported in <a href="http://trac.sagemath.org/sage_trac/ticket/10841">trac #10841</a> 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?</p>
<p>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.</p>
https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/?comment=21887#post-id-21887And 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!)Fri, 08 Apr 2011 09:56:37 +0200https://ask.sagemath.org/question/8038/variable-type-returned-after-integrating/?comment=21887#post-id-21887