Skip to main content

How to select a certain portion of a piecewise function?


For example, if I have Piecewise[{{x^2, x < 0}, {x, x > 0}}], how do I select only the function that is for x < 0? I want to set a new function equal to the x^2 only, not the entire piecewise function.



Answer



As you may know, Part ([[ ]]) works on non-lists as well. So, you can index your Piecewise like so:


Piecewise[{{x^2, x < 0}, {x, x > 0}}][[1, 1, 1]]



x^2



With Cases you can pick out the part with a specific condition:


Cases[Piecewise[{{x^2, x < 0}, {x, x > 0}}], {a_, x < 0} -> a, {2}] // First


x^2




Comments