ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 16 Dec 2013 07:49:05 -0600what type of object is a function defined with the piecewise command?http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/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!Thu, 07 Jun 2012 08:32:23 -0500http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/Answer by DSM for <p>I understand that in Sage, there are three function-like things: functions, expressions, and python functions. How do you classify functions defined with the <code>piecewise</code> command?</p>
<p>Functions defined with the <code>piecewise</code> command seem to be a separate class. When I use the <code>parent()</code> command on a piecewise function, Sage says it is an "instance." Can you help me understand what that means? Thanks!</p>
http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/?answer=13667#post-id-13667As 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)
pairs.
Thu, 07 Jun 2012 08:58:33 -0500http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/?answer=13667#post-id-13667Comment by kcrisman for <p>As you know, <code>instance</code> is a given case, a given version, of a <code>class</code>. When <code>parent</code> returns <code>instance</code>, 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.</p>
<pre><code>sage: class fred: pass
....:
sage: parent(fred)
<type 'classobj'>
sage: a = fred()
sage: parent(a)
<type 'instance'>
</code></pre>
<p>which is why we have</p>
<pre><code>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'>
</code></pre>
<p>And if you type <code>sage.functions.piecewise??</code> at the console to see the source of the module, you see that PiecewisePolynomial is defined purely as a Python class:</p>
<pre><code>class PiecewisePolynomial:
"""
Returns a piecewise function from a list of (interval, function)
pairs.
</code></pre>
http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/?comment=19663#post-id-19663Yup. 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.Thu, 07 Jun 2012 09:09:35 -0500http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/?comment=19663#post-id-19663Answer by subbu for <p>I understand that in Sage, there are three function-like things: functions, expressions, and python functions. How do you classify functions defined with the <code>piecewise</code> command?</p>
<p>Functions defined with the <code>piecewise</code> command seem to be a separate class. When I use the <code>parent()</code> command on a piecewise function, Sage says it is an "instance." Can you help me understand what that means? Thanks!</p>
http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/?answer=15823#post-id-15823Use lambda functions it worked for me. Mon, 16 Dec 2013 07:49:05 -0600http://ask.sagemath.org/question/9042/what-type-of-object-is-a-function-defined-with-the-piecewise-command/?answer=15823#post-id-15823