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.Mon, 25 Apr 2016 16:37:04 -0500Function with a lower limithttps://ask.sagemath.org/question/33197/function-with-a-lower-limit/ I need to define a function like the following:
y = 5 - 2x if f(x)>0
0 otherwise
I tried different solutions:
f(x) = 5 - 2*x
g(x) = max(f(x),0)
or:
f(x) = 5 - 2*x
def g(x):
return f(x) if f(x)>0 else 0
However, when I try to plot them, they do not seem to work: the first ignores the lower bound, while the second is always zero. This is strange, as when I try `[g(x) for x in range(0,10)]` it returns the correct values of `g(x)`Fri, 22 Apr 2016 17:10:44 -0500https://ask.sagemath.org/question/33197/function-with-a-lower-limit/Answer by tmonteil for <p>I need to define a function like the following:</p>
<pre><code>y = 5 - 2x if f(x)>0
0 otherwise
</code></pre>
<p>I tried different solutions:</p>
<pre><code>f(x) = 5 - 2*x
g(x) = max(f(x),0)
</code></pre>
<p>or:</p>
<pre><code>f(x) = 5 - 2*x
def g(x):
return f(x) if f(x)>0 else 0
</code></pre>
<p>However, when I try to plot them, they do not seem to work: the first ignores the lower bound, while the second is always zero. This is strange, as when I try <code>[g(x) for x in range(0,10)]</code> it returns the correct values of <code>g(x)</code></p>
https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?answer=33214#post-id-33214Concerning your second definition, as @paulmasson, i can not reproduce your problem.
Concerning you first definition, there is something tricky about symbolics to understand: when you write `g(x) = max(f(x),0)`, the Python builtin `max` is used, which takes the max between two Python/Sage objects `f(x)` and `0`, not pointwise! Since those two objects are not comparable, it considers that the first is to be returned, so `max(f(x),0)` is nothing but the object `f(x)`. You can check by changing the order `max(0,f(x))`:
sage: g(x) = max(f(x),0)
sage: g
x |--> -2*x + 5
sage: g(x) = max(0,f(x))
sage: g
x |--> 0
So, you have to use is the "symbolic maximum", viewed as a mathematical function:
sage: g(x) = max_symbolic(f(x),0)
sage: g
x |--> max(-2*x + 5, 0)
sage: g.plot(-10,10)
Sat, 23 Apr 2016 14:29:15 -0500https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?answer=33214#post-id-33214Comment by paulmasson for <p>Concerning your second definition, as <a href="/users/19915/paulmasson/">@paulmasson</a>, i can not reproduce your problem.</p>
<p>Concerning you first definition, there is something tricky about symbolics to understand: when you write <code>g(x) = max(f(x),0)</code>, the Python builtin <code>max</code> is used, which takes the max between two Python/Sage objects <code>f(x)</code> and <code>0</code>, not pointwise! Since those two objects are not comparable, it considers that the first is to be returned, so <code>max(f(x),0)</code> is nothing but the object <code>f(x)</code>. You can check by changing the order <code>max(0,f(x))</code>:</p>
<pre><code>sage: g(x) = max(f(x),0)
sage: g
x |--> -2*x + 5
sage: g(x) = max(0,f(x))
sage: g
x |--> 0
</code></pre>
<p>So, you have to use is the "symbolic maximum", viewed as a mathematical function:</p>
<pre><code>sage: g(x) = max_symbolic(f(x),0)
sage: g
x |--> max(-2*x + 5, 0)
sage: g.plot(-10,10)
</code></pre>
https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?comment=33227#post-id-33227@tmonteil It appears that Sage has native `ceil` and `floor` functions that override Python functions and can handle symbolics. Do you know why that wasn't done for `max` and `min` as well?Mon, 25 Apr 2016 16:37:04 -0500https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?comment=33227#post-id-33227Answer by paulmasson for <p>I need to define a function like the following:</p>
<pre><code>y = 5 - 2x if f(x)>0
0 otherwise
</code></pre>
<p>I tried different solutions:</p>
<pre><code>f(x) = 5 - 2*x
g(x) = max(f(x),0)
</code></pre>
<p>or:</p>
<pre><code>f(x) = 5 - 2*x
def g(x):
return f(x) if f(x)>0 else 0
</code></pre>
<p>However, when I try to plot them, they do not seem to work: the first ignores the lower bound, while the second is always zero. This is strange, as when I try <code>[g(x) for x in range(0,10)]</code> it returns the correct values of <code>g(x)</code></p>
https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?answer=33200#post-id-33200Could be how you're plotting the function. This works for me:
f(x) = 5 - 2*x
def g(x):
return f(x) if f(x)>0 else 0
plot(g,x,-10,10)
With a slight modification to your first solution, this also works:
f(x) = 5 - 2*x
def g(x):
return max(f(x),0)
plot(g,x,-10,10)
EDIT: Your plotting issues are a result of what Sage documentation calls "accidental evaluation". See item (4) on this page: http://doc.sagemath.org/html/en/tutorial/tour_functions.html
Fri, 22 Apr 2016 20:12:51 -0500https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?answer=33200#post-id-33200Comment by Massimo2013 for <p>Could be how you're plotting the function. This works for me:</p>
<pre><code>f(x) = 5 - 2*x
def g(x):
return f(x) if f(x)>0 else 0
plot(g,x,-10,10)
</code></pre>
<p>With a slight modification to your first solution, this also works: </p>
<pre><code>f(x) = 5 - 2*x
def g(x):
return max(f(x),0)
plot(g,x,-10,10)
</code></pre>
<p>EDIT: Your plotting issues are a result of what Sage documentation calls "accidental evaluation". See item (4) on this page: <a href="http://doc.sagemath.org/html/en/tutorial/tour_functions.html">http://doc.sagemath.org/html/en/tutor...</a></p>
https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?comment=33205#post-id-33205Besides, the function `g(x)` does not seem to be used correctly by `solve`:
`solve([g(x)==y,x==4],[x,y])` gives a negative value for ySat, 23 Apr 2016 02:20:36 -0500https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?comment=33205#post-id-33205Comment by Massimo2013 for <p>Could be how you're plotting the function. This works for me:</p>
<pre><code>f(x) = 5 - 2*x
def g(x):
return f(x) if f(x)>0 else 0
plot(g,x,-10,10)
</code></pre>
<p>With a slight modification to your first solution, this also works: </p>
<pre><code>f(x) = 5 - 2*x
def g(x):
return max(f(x),0)
plot(g,x,-10,10)
</code></pre>
<p>EDIT: Your plotting issues are a result of what Sage documentation calls "accidental evaluation". See item (4) on this page: <a href="http://doc.sagemath.org/html/en/tutorial/tour_functions.html">http://doc.sagemath.org/html/en/tutor...</a></p>
https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?comment=33204#post-id-33204You are right: it seems to work with `plot(g,x,0,10)`.
However, it does not work properly with `plot(g(x),x,0,10)`
Is there something I'm missing about the working of `plot`?Sat, 23 Apr 2016 01:57:03 -0500https://ask.sagemath.org/question/33197/function-with-a-lower-limit/?comment=33204#post-id-33204