# How to handle the calculation of piecewise functions?

Given piecewise function f [x]:

f[x_] = Piecewise[{{1/x, x < 0}, {x^2, x >= 0}}]


Another function g [x]:

g[x_] = x + 1


Want to calculate g[f[x]]

get the result is:

How can I obtain the correct answer as shown in the following figure:

Use

g[f[x]] //  PiecewiseExpand


How to make the definition field display as: x>=0?

I do not know offhand without writing special code to post-process it. But these are really the same. i.e. True in this case is the same as x>=0, because True means for all other possible values of x, which is x>=0.

So Mathematically they are the same. If you want x>=0 to show there instead of True just for display purposes, I think special code might be needed to do this. I could not see an option in PiecewiseExpand to change this when I just looked. May be someone will have an idea how to do this easily.

• The definition field corresponding to 1+x ^ 2 in the obtained results is true. How to make the definition field display as: x>=0? Commented Jun 5, 2023 at 23:38
Clear["Global*"]

f[x_] = Piecewise[{{1/x, x < 0}, {x^2, x >= 0}}];

g[x_] = x + 1;


As shown by Nasser,

h[x_] = g[f[x]] // PiecewiseExpand


Post-processing,

h2[x_] = ReplacePart[
h[x], {1 -> Append[h[x][[1]], {h[x][[2]], Not[h[x][[1, 1, -1]]]}],
2 -> 0}]
`