Ask Your Question

Substitute piecewise function variable

asked 2017-03-24 14:39:26 -0500

jrojasqu gravatar image

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,


but if I declare another variable 'h' and try to input that variable into the piecewise function, it does not appear to work:

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:


How can I overcome this? How can I successfully operate a variable substitution in my piecewise function?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-24 17:41:35 -0500

If 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.

edit flag offensive delete link more


@paulmasson Thanks, it worked; and I would at least vote your answer but I lack the minimum 10 points to do so...

jrojasqu gravatar imagejrojasqu ( 2017-03-27 18:08:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-03-24 14:39:26 -0500

Seen: 50 times

Last updated: Mar 24