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.Tue, 13 Feb 2018 09:55:39 -0600piecewise defined function via defhttps://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/I've made the following experiment with Sage:
def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.Sat, 10 Feb 2018 20:43:20 -0600https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/Comment by tmonteil for <p>I've made the following experiment with Sage:</p>
<pre><code> def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
</code></pre>
<p>However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.</p>
https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?comment=41085#post-id-41085Note: duplicate of https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/Tue, 13 Feb 2018 04:33:23 -0600https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?comment=41085#post-id-41085Comment by dan_fulea for <p>I've made the following experiment with Sage:</p>
<pre><code> def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
</code></pre>
<p>However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.</p>
https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?comment=41071#post-id-41071Sometimes one should constrain sage to evaluate...
def f(x):
if bool(0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
This gives:
\newcommand{\Bold}[1]{\mathbf{#1}}1
\newcommand{\Bold}[1]{\mathbf{#1}}1Sun, 11 Feb 2018 14:50:04 -0600https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?comment=41071#post-id-41071Answer by tmonteil for <p>I've made the following experiment with Sage:</p>
<pre><code> def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
</code></pre>
<p>However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.</p>
https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?answer=41094#post-id-41094This is clearly a bug, thanks for reporting ! See [trac ticket 24726](https://trac.sagemath.org/ticket/24726)
Note that this is actually not the problem with `assume` but with symbolic expression involving more than one comparison operator:
sage: 0 <= x <= 1/2
0 <= x
sage: e = 0 <= x <= 1/2
sage: e.operator()
<built-in function le>
sage: e.operands()
[0, x]
Note the following weird behaviour:
sage: assume(0 <= x)
sage: 0 <= x <= 1/2
x <= (1/2)
This is because now `0 <= x` is evaluated to `True`, and `True and something` returns `something`.Tue, 13 Feb 2018 09:55:39 -0600https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?answer=41094#post-id-41094Answer by eric_g for <p>I've made the following experiment with Sage:</p>
<pre><code> def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
</code></pre>
<p>However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.</p>
https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?answer=41072#post-id-41072I think the issue is the double inequality in `assume`: only the first one is taken into account, as one can check with the function `assumptions()`, which returns the list of assumptions known to Sage:
sage: assume(0 <= x <= 1/2)
sage: assumptions()
[0 <= x]
Actually, one shall use `assume` with two single inequalities instead of a double inequality:
sage: assume(0 <= x, x <= 1/2)
sage: assumptions()
[0 <= x, x <= (1/2)]
Then your function will return `1` for `f(x)`, as expected.
Sun, 11 Feb 2018 15:54:34 -0600https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?answer=41072#post-id-41072