I have a piecewise function that has in each case an N number of functions that take N arguments. I want to retrieve the diagonals of the Jacobian, or simply the element-wise first derivative of the N functions and N arguments.
Example code:
i[a_,b_]:=Piecewise[
{{{f[a,b],g[a,b]},a>b},
{{j[a,b],k[a,b]},2a<b}},
{l[a,b],m[a,b]}
]
D[i[a,b],{{a,b}}]
D
can handle piecewise functions and provides the full Jacobians inside of the piecewise function.
How do I obtain a piecewise function that contains the diagonals of the Jacobian instead of the full Jacobian?
Can I rewrite this so Mathematica does not calculate the full Jacobian, which when N is large results in wasteful unnecessary computation?
EDIT In looking at MichealE2's helpful answer to this question, I was trying to think of a more general way to apply functions to expressions inside piecewise functions and found this kglr piecewise function mapping solution. Reproduced and and applied with MichaelE2's MapThread approach here:
pwMap[f_] := MapAt[f,#,{{1, All, 1},{2}}]&;
i[a,b]//pwMap[MapThread[D,{#,{a,b}}]&]
This is nice because it should be a completely general way to apply functions to expressions inside piecewise functions.