2
$\begingroup$

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?

$\endgroup$
1

1 Answer 1

2
$\begingroup$
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_] := Internal`ToPiecewise[#, f /@ #2] & @@ Internal`FromPiecewise[#] &;

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

True

$\endgroup$
2
  • $\begingroup$ Beautiful! Thanks, I've learned a lot from this! $\endgroup$
    – Christoph
    Commented Oct 4, 2018 at 13:33
  • $\begingroup$ @Christoph, thank you for the accept. $\endgroup$
    – kglr
    Commented Oct 4, 2018 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.