# Derivative of piecewise function returns one more case

I define the following piecewise function:

U[c_,n_,g_,p_]:=Piecewise[{{c^(1-g)/(1-g)-n^(1+p)/(1+p),g!=1}, {Log[c]-n^(1+p)/(1+p),g==1}}]


When I take the derivative

D[U[c,n,g,p],c]


It returns a a third unexpected line.

(In the image, gamma corresponds to g)

What is the meaning of this third line and how can I remove it from the printed output?

Clear["Global*"]


Instead of using g != 1 use g < 1 || g > 1, then

U[c_, n_, g_, p_] :=
Piecewise[{{c^(1 - g)/(1 - g) - n^(1 + p)/(1 + p), g < 1 || g > 1}},
Log[c] - n^(1 + p)/(1 + p)]

D[U[c, n, g, p], c] // Simplify

(* c^-g *)


EDIT: Or use Simplify with your original definition

U[c_, n_, g_, p_] :=
Piecewise[{{c^(1 - g)/(1 - g) - n^(1 + p)/(1 + p),
g != 1}, {Log[c] - n^(1 + p)/(1 + p), g == 1}}]

D[U[c, n, g, p], c] // Simplify

(* c^-g *)

• Using Simplify does not return the derivative for the second case of the piecewise function, i.e. I expect two outputs but I only get one Commented Oct 3, 2022 at 21:21
• They simplify to one case since c^-1 == 1/c Commented Oct 3, 2022 at 21:41
• I understand thank you Commented Oct 3, 2022 at 22:34

It looks like Mathematica insists on having a default condition (with value zero, unless otherwise specifed).

With

ass=D[U[c,n,g,p],c]


You can construct something like

Piecewise[Drop[ass[[1]]], ass[[1, 2, 1]]]
`

This uses the second terms as the fall through value.