Ask Your Question

what type of object is a function defined with the piecewise command?

asked 2012-06-07 08:32:23 -0600

calc314 gravatar image

I understand that in Sage, there are three function-like things: functions, expressions, and python functions. How do you classify functions defined with the piecewise command?

Functions defined with the piecewise command seem to be a separate class. When I use the parent() command on a piecewise function, Sage says it is an "instance." Can you help me understand what that means? Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-06-07 08:58:33 -0600

DSM gravatar image

updated 2012-06-07 08:59:44 -0600

As you know, instance is a given case, a given version, of a class. When parent returns instance, that simply means that there's nothing particularly Sage-specific about the structure and it's not living within Sage's Category structure, so it's basically returning Python type information.

sage: class fred: pass
sage: parent(fred)
<type 'classobj'>
sage: a = fred()
sage: parent(a)
<type 'instance'>

which is why we have

sage: f(x) = x^2
sage: parent(f)
Callable function ring with arguments (x,)
sage: g = Piecewise([[(0,1),x], [(1,2),x^2]], x) 
sage: g
Piecewise defined function with 2 parts, [[(0, 1), x |--> x], [(1, 2), x |--> x^2]]

sage: parent(g)
<type 'instance'>

And if you type sage.functions.piecewise?? at the console to see the source of the module, you see that PiecewisePolynomial is defined purely as a Python class:

class PiecewisePolynomial:
    Returns a piecewise function from a list of (interval, function)
edit flag offensive delete link more


Yup. A fantastic thing would be to redo our piecewise functions, but it's just big enough of a job to really require some dedicated time to do (it would have to be done all at once), and many of the people who would have the expertise or interest simply don't have the time. Sorry we don't have those fit in with our expression framework yet.

kcrisman gravatar imagekcrisman ( 2012-06-07 09:09:35 -0600 )edit

answered 2013-12-16 07:49:05 -0600

subbu gravatar image

Use lambda functions it worked for me.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-06-07 08:32:23 -0600

Seen: 261 times

Last updated: Dec 16 '13