Ask Your Question
0

Substitute piecewise function variable

asked 2017-03-24 20:39:26 +0100

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,

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-03-24 23:41:35 +0100

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

Comments

@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-28 01:08:35 +0100 )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

Stats

Asked: 2017-03-24 20:39:26 +0100

Seen: 782 times

Last updated: Mar 24 '17