# Define a function with the condition that it must be positive

I have a function f(x) and I want to define a new function g(x) that is equal to f(x) only when f(x) is positive.

What is the right way to do that?

Sorry for this silly question, but I cannot find the proper way to accomplish this simple task in Mathematica.

• Maybe g[x_]:=Max[f[x],0] ? (Provided I correctly remember the syntax of Max). But what should be the value of your new function when f is not positive? Is the zero value important? Nov 9, 2013 at 18:17
• @Peltio That sounds like an answer to me. Nov 9, 2013 at 18:24
• @Peltio it's reasonable, but I'd like that the function g(x) was undefined when f(x) was negative. Nov 9, 2013 at 18:27
• Try Piecewise function, if it's works for you. Nov 9, 2013 at 18:32
• @s.s.o In this way g(x) is defined only for positive x, instead I want that it is defined only when f(x) is positive. Nov 9, 2013 at 18:54

You can check the Mathematica documentation about Condition /;

here is a simple example :

f[x_] := x + 5;
g[x_] := f[x] /; f[x] > 0

g


10

g[-9]


g[-9]

• @Szabolcs, in fact it was what I suggested as comment but it was not what OP asked for. Nov 10, 2013 at 19:39