ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 09 Oct 2017 23:37:53 +0200piecewise function with variable within domainshttps://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/Dear all,
I am trying to define a piecewise function where the domains are parametrized by some variable:
import sage.all as sage
sage.var('x')
sage.var('x0')
sage.var('x1')
sage.assume( 0<x0)
sage.assume(x0<x1)
sage.assume(x1< 1)
sage.piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),1)], var=x)
However, I cannot seem to get it to work:
TypeError: unable to simplify to a real interval approximation
Any idea how to make it work? Thanks!
MartinSun, 08 Oct 2017 19:30:19 +0200https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/Comment by mforets for <p>Dear all,</p>
<p>I am trying to define a piecewise function where the domains are parametrized by some variable:</p>
<pre><code>import sage.all as sage
sage.var('x')
sage.var('x0')
sage.var('x1')
sage.assume( 0<x0)
sage.assume(x0<x1)
sage.assume(x1< 1)
sage.piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),1)], var=x)
</code></pre>
<p>However, I cannot seem to get it to work:</p>
<pre><code>TypeError: unable to simplify to a real interval approximation
</code></pre>
<p>Any idea how to make it work? Thanks!</p>
<p>Martin</p>
https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/?comment=39081#post-id-39081a workaround is to define it as linear combination of heaviside, or step functions. these special functions are documented [here](http://doc.sagemath.org/html/en/reference/functions/sage/functions/generalized.html#generalized-functions).Mon, 09 Oct 2017 07:21:47 +0200https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/?comment=39081#post-id-39081Comment by dan_fulea for <p>Dear all,</p>
<p>I am trying to define a piecewise function where the domains are parametrized by some variable:</p>
<pre><code>import sage.all as sage
sage.var('x')
sage.var('x0')
sage.var('x1')
sage.assume( 0<x0)
sage.assume(x0<x1)
sage.assume(x1< 1)
sage.piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),1)], var=x)
</code></pre>
<p>However, I cannot seem to get it to work:</p>
<pre><code>TypeError: unable to simplify to a real interval approximation
</code></pre>
<p>Any idea how to make it work? Thanks!</p>
<p>Martin</p>
https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/?comment=39093#post-id-39093The constructor
f = sage.piecewise( [ ((0,x0),0), ([x0,x1],1), ((x1,1),2) ], var=x )
crashes because `RLF( x0 )` makes no sense. It is generally not a good idea to assume that code written for a special purpose can do the same job for a similar mathematical, possibly more general situation. But let's say the constructor would have instantiated something (now with possibly broken methods).
Which is the application of the above?
Make "it" work for which purpose?
What should be done now with "that `f` that works" ?Mon, 09 Oct 2017 23:37:53 +0200https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/?comment=39093#post-id-39093Answer by ndomes for <p>Dear all,</p>
<p>I am trying to define a piecewise function where the domains are parametrized by some variable:</p>
<pre><code>import sage.all as sage
sage.var('x')
sage.var('x0')
sage.var('x1')
sage.assume( 0<x0)
sage.assume(x0<x1)
sage.assume(x1< 1)
sage.piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),1)], var=x)
</code></pre>
<p>However, I cannot seem to get it to work:</p>
<pre><code>TypeError: unable to simplify to a real interval approximation
</code></pre>
<p>Any idea how to make it work? Thanks!</p>
<p>Martin</p>
https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/?answer=39084#post-id-39084What's about a python function?
def my_piecewise(x0,x1):
if 0 < x0 < x1 < 1:
return piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),2)], var=x)
my_piecewise(0.2,0.7).plot()
Mon, 09 Oct 2017 12:19:26 +0200https://ask.sagemath.org/question/39073/piecewise-function-with-variable-within-domains/?answer=39084#post-id-39084