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:

enter image description here

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

enter image description here


2 Answers 2



g[f[x]] //  PiecewiseExpand

Mathematica graphics

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.

enter image description here

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.

  • $\begingroup$ The definition field corresponding to 1+x ^ 2 in the obtained results is true. How to make the definition field display as: x>=0? $\endgroup$
    – csn899
    Jun 5 at 23:38

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

enter image description here


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

enter image description here


Your Answer

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

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