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.Tue, 28 Mar 2017 01:08:35 +0200Substitute piecewise function variablehttps://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/I have the following piecewise function:
phi = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi = phi.extension(0);
It appears to be a valid function since I can obtain/plot its values for any 'x'. But whenever I try to substitute the variable, it does not work. For example,
phi(2)
0
but if I declare another variable 'h' and try to input that variable into the piecewise function, it does not appear to work:
h=pi/2
phi(h)
TypeError: self must be a numeric expression
At first I thought that 'h' was not a 'numeric' or 'real' value, but when I test it, it is a real value:
h.is_real()
True
How can I overcome this? How can I successfully operate a variable substitution in my piecewise function?Fri, 24 Mar 2017 20:39:26 +0100https://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/Answer by paulmasson for <p>I have the following piecewise function:</p>
<pre><code>phi = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi = phi.extension(0);
</code></pre>
<p>It appears to be a valid function since I can obtain/plot its values for any 'x'. But whenever I try to substitute the variable, it does not work. For example,</p>
<pre><code>phi(2)
0
</code></pre>
<p>but if I declare another variable 'h' and try to input that variable into the piecewise function, it does not appear to work: </p>
<pre><code>h=pi/2
phi(h)
TypeError: self must be a numeric expression
</code></pre>
<p>At first I thought that 'h' was not a 'numeric' or 'real' value, but when I test it, it is a real value:</p>
<pre><code>h.is_real()
True
</code></pre>
<p>How can I overcome this? How can I successfully operate a variable substitution in my piecewise function?</p>
https://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/?answer=37069#post-id-37069If you check the type of `h` with `type(h)`, it comes back as
<type 'sage.symbolic.expression.Expression'>
which is not numeric but symbolic. You can convert it to a numeric value with `n(h)` or `h.n()` and then the substitution will give zero as expected.Fri, 24 Mar 2017 23:41:35 +0100https://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/?answer=37069#post-id-37069Comment by jrojasqu for <p>If you check the type of <code>h</code> with <code>type(h)</code>, it comes back as</p>
<pre><code><type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>which is not numeric but symbolic. You can convert it to a numeric value with <code>n(h)</code> or <code>h.n()</code> and then the substitution will give zero as expected.</p>
https://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/?comment=37099#post-id-37099@paulmasson Thanks, it worked; and I would at least vote your answer but I lack the minimum 10 points to do so...Tue, 28 Mar 2017 01:08:35 +0200https://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/?comment=37099#post-id-37099