ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 17 Oct 2012 02:08:43 -0500shift picewise functionshttps://ask.sagemath.org/question/8433/shift-picewise-functions/Suppose I define a piecewise function f for example:
f = Piecewise([[(-infinity,1),1],[(1,+infinity),x]])
How to define the shifted function g with g(x) = f(x-2) for all x?
Or more generally: If h is another function. How to define $g = f\circ h$ in sage?Thu, 03 Nov 2011 05:15:34 -0500https://ask.sagemath.org/question/8433/shift-picewise-functions/Answer by rbeezer for <p>Suppose I define a piecewise function f for example:</p>
<pre><code>f = Piecewise([[(-infinity,1),1],[(1,+infinity),x]])
</code></pre>
<p>How to define the shifted function g with g(x) = f(x-2) for all x?</p>
<p>Or more generally: If h is another function. How to define $g = f\circ h$ in sage?</p>
https://ask.sagemath.org/question/8433/shift-picewise-functions/?answer=14156#post-id-14156The following works as expected:
f1(x)=0
f2(x)=exp(-1/(x^2))
f=Piecewise([[(-10,0),f1],[(0,10),f2]])
h(x)=x-2
c = compose(f, h)
plot(c, -8, 12)Tue, 16 Oct 2012 16:49:41 -0500https://ask.sagemath.org/question/8433/shift-picewise-functions/?answer=14156#post-id-14156Comment by kcrisman for <p>The following works as expected:</p>
<pre><code>f1(x)=0
f2(x)=exp(-1/(x^2))
f=Piecewise([[(-10,0),f1],[(0,10),f2]])
h(x)=x-2
c = compose(f, h)
plot(c, -8, 12)
</code></pre>
https://ask.sagemath.org/question/8433/shift-picewise-functions/?comment=18851#post-id-18851Wow, I had no idea that existed. Nice!Wed, 17 Oct 2012 02:08:43 -0500https://ask.sagemath.org/question/8433/shift-picewise-functions/?comment=18851#post-id-18851Answer by kcrisman for <p>Suppose I define a piecewise function f for example:</p>
<pre><code>f = Piecewise([[(-infinity,1),1],[(1,+infinity),x]])
</code></pre>
<p>How to define the shifted function g with g(x) = f(x-2) for all x?</p>
<p>Or more generally: If h is another function. How to define $g = f\circ h$ in sage?</p>
https://ask.sagemath.org/question/8433/shift-picewise-functions/?answer=12855#post-id-12855To answer your second question:
sage: f(x) = x^2+5
sage: g(x) = 25*sqrt(x+1)
sage: g(f(x))
25*sqrt(x^2 + 6)
sage: f(g(x))
625*x + 630
sage: h(x) = f(g(x))
sage: h
x |--> 625*x + 630
The first one is harder, because `Piecewise` functions were implemented long before we even had symbolic functions, and hence are missing a lot of functionality. This is a long-standing need, but it takes a certain expertise in both the old and new code, as well as Ginac, to do this.
(To the cognoscenti - sympy seems to have piecewise functions, but I can't find much documentation, just obscure hints that they are there. How powerful are theirs?)Thu, 03 Nov 2011 05:58:40 -0500https://ask.sagemath.org/question/8433/shift-picewise-functions/?answer=12855#post-id-12855Comment by sagefan for <p>To answer your second question:</p>
<pre><code>sage: f(x) = x^2+5
sage: g(x) = 25*sqrt(x+1)
sage: g(f(x))
25*sqrt(x^2 + 6)
sage: f(g(x))
625*x + 630
sage: h(x) = f(g(x))
sage: h
x |--> 625*x + 630
</code></pre>
<p>The first one is harder, because <code>Piecewise</code> functions were implemented long before we even had symbolic functions, and hence are missing a lot of functionality. This is a long-standing need, but it takes a certain expertise in both the old and new code, as well as Ginac, to do this. </p>
<p>(To the cognoscenti - sympy seems to have piecewise functions, but I can't find much documentation, just obscure hints that they are there. How powerful are theirs?)</p>
https://ask.sagemath.org/question/8433/shift-picewise-functions/?comment=20961#post-id-20961So there is no workaround to compose piecewise-defined functions?Fri, 04 Nov 2011 10:41:47 -0500https://ask.sagemath.org/question/8433/shift-picewise-functions/?comment=20961#post-id-20961Comment by kcrisman for <p>To answer your second question:</p>
<pre><code>sage: f(x) = x^2+5
sage: g(x) = 25*sqrt(x+1)
sage: g(f(x))
25*sqrt(x^2 + 6)
sage: f(g(x))
625*x + 630
sage: h(x) = f(g(x))
sage: h
x |--> 625*x + 630
</code></pre>
<p>The first one is harder, because <code>Piecewise</code> functions were implemented long before we even had symbolic functions, and hence are missing a lot of functionality. This is a long-standing need, but it takes a certain expertise in both the old and new code, as well as Ginac, to do this. </p>
<p>(To the cognoscenti - sympy seems to have piecewise functions, but I can't find much documentation, just obscure hints that they are there. How powerful are theirs?)</p>
https://ask.sagemath.org/question/8433/shift-picewise-functions/?comment=20958#post-id-20958There may be, but I can't think of one offhand. That doesn't mean it doesn't exist, just that I can't think of it right now.Fri, 04 Nov 2011 16:53:20 -0500https://ask.sagemath.org/question/8433/shift-picewise-functions/?comment=20958#post-id-20958