Ask Your Question

Ben Reynwar's profile - activity

2020-05-26 17:20:51 +0100 received badge  Popular Question (source)
2020-05-26 17:20:51 +0100 received badge  Notable Question (source)
2017-10-08 21:20:55 +0100 received badge  Popular Question (source)
2017-10-08 21:20:55 +0100 received badge  Notable Question (source)
2011-04-09 20:06:00 +0100 received badge  Teacher (source)
2011-04-09 20:00:05 +0100 answered a question Composite function

h(t) = f(*g(t))

2011-04-09 01:28:01 +0100 received badge  Good Question (source)
2011-04-08 14:18:27 +0100 received badge  Nice Question (source)
2011-04-08 12:09:21 +0100 received badge  Student (source)
2011-04-08 12:08:45 +0100 received badge  Supporter (source)
2011-04-08 12:08:42 +0100 marked best answer Cast to Expression

Yeah, that tripped me up too!

You can do it by calling the parent ring, which in this case is the Symbolic Ring, abbreviated SR:

sage: a = 3
sage: type(a)
<type 'sage.rings.integer.Integer'>
sage: parent(a)
Integer Ring
sage: b = SR(a)
sage: type(b)
<type 'sage.symbolic.expression.Expression'>
sage: parent(b)
Symbolic Ring
2011-04-08 12:06:36 +0100 commented answer Variable type returned after integrating.

That makes sense. Thank you.

2011-04-08 12:02:33 +0100 asked a question Cast to Expression

How do I cast a number to an Expression?

I would have assumed:

> Expression(0)
Traceback (click to the left of this block for traceback)
...
TypeError: Cannot convert sage.rings.integer.Integer to
sage.structure.parent.Parent
2011-04-08 11:51:24 +0100 received badge  Scholar (source)
2011-04-08 11:51:24 +0100 marked best answer Variable type returned after integrating.

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.

2011-04-08 00:17:29 +0100 asked a question 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'