# Mapping over a piecewise function

This is a followup to a question I posted earlier today. Suppose you have a piecewise function such as "snap=max[x,0]= x if x>0, 0 otherwise".

Then you would think that it must be straightworward to apply a function to this, i.e. to obtain "f[x] if x > 0, f[0] otherwise".

My intuition is that I should be able to say "f[snap]", but this doesn't work. The best I can do (after help from AccidentalFourierTransform) is quite complicated:

snap = PiecewiseExpand[Max[0, x]]
MapAt[f[#] &, Map[MapAt[f[#] &, 1], snap, {2}], -1]


This produces the desired result, but it seems strange that I need to do this. Is there a more straight-forward solution?

pwMap1[f_] := MapAt[f, #, {{1, All, 1}, {2}}] &;
pwMap1[f]@snap // TeXForm


$$\begin{cases} f(x) & x>0 \\ f(0) & \text{True} \end{cases}$$

Also:

pwMap2[f_] := InternalToPiecewise[#, f /@ #2] & @@ InternalFromPiecewise[#] &;

pwMap2[f]@snap == pwMap1[f]@snap


True

• Beautiful! Thanks, I've learned a lot from this! – Christoph Oct 4 '18 at 13:33
• @Christoph, thank you for the accept. – kglr Oct 4 '18 at 13:34