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.Wed, 16 Nov 2016 08:26:18 -0600Alternative to piecewise functions?http://ask.sagemath.org/question/26614/alternative-to-piecewise-functions/If I only need indicator functions (the function is 1 in an interval [a, b], 0 outside of it). Is there an alternative to piecewise functions (with all their issues like plotting), which I can use? Thank you!
EDIT: Ok, I had an idea:
indicator(x,a,b)=(sign(x-a)-sign(x-b))/2
That does pretty much what I want.Tue, 21 Apr 2015 08:47:42 -0500http://ask.sagemath.org/question/26614/alternative-to-piecewise-functions/Answer by calc314 for <p>If I only need indicator functions (the function is 1 in an interval [a, b], 0 outside of it). Is there an alternative to piecewise functions (with all their issues like plotting), which I can use? Thank you!</p>
<p>EDIT: Ok, I had an idea:</p>
<pre><code>indicator(x,a,b)=(sign(x-a)-sign(x-b))/2
</code></pre>
<p>That does pretty much what I want.</p>
http://ask.sagemath.org/question/26614/alternative-to-piecewise-functions/?answer=26615#post-id-26615Here are two other options:
1) Use the Heaviside function:
f(x)=heaviside(x-1)-heaviside(x-3)
plot(f(x),(x,0,5))
2) define a Python function
def f(x,a,b):
if x>a and x<b:
return(1)
else:
return(0)
plot(lambda x: f(x,1,3),(x,-3,5))Tue, 21 Apr 2015 09:59:24 -0500http://ask.sagemath.org/question/26614/alternative-to-piecewise-functions/?answer=26615#post-id-26615Comment by candide for <p>Here are two other options:</p>
<p>1) Use the Heaviside function:</p>
<pre><code>f(x)=heaviside(x-1)-heaviside(x-3)
plot(f(x),(x,0,5))
</code></pre>
<p>2) define a Python function</p>
<pre><code>def f(x,a,b):
if x>a and x<b:
return(1)
else:
return(0)
plot(lambda x: f(x,1,3),(x,-3,5))
</code></pre>
http://ask.sagemath.org/question/26614/alternative-to-piecewise-functions/?comment=35614#post-id-35614`f(x,a,b)` and `a<x<b` have same value.Wed, 16 Nov 2016 08:26:18 -0600http://ask.sagemath.org/question/26614/alternative-to-piecewise-functions/?comment=35614#post-id-35614