# Integrate over piecewise function defined using /;

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


The above does not work (Mathematica returns it unevaluated), but the below does.

g[x_] = Piecewise[{ {x,x<0}, {x^2,x>=0}}]
Integrate[g[x],{x,-1,1}]


Why? More importantly, I've written a lot of code using the /; form: is there any way I can make it work and/or automagically convert it to Piecewise form without having to rewrite my code?

In real life, I'm doing something like Integrate[Cos[n x] f[x], {x, -1, 1}] for arbitrary n, and want a symbolic result, so NIntegrate won't solve my problem.

• NIntegrate[f[x], {x, -1, 1}] will work and can be used with either Rationalize or RootApproximant to return -1/6. Commented Nov 6, 2014 at 14:41
• @BobHanlon Unfortunately won't work if the integration contains variables other than x.
– user1722
Commented Nov 6, 2014 at 14:51
• I would consider /; a programming construct, while Piecewise is meant for representing a mathematical concept. Piecewise was designed with symbolic manipulation in mind. /; is for controlling evaluation. These are two entirely different uses, and many constructs have two versions in Mathematica for this reason. Some others don't and the line between 'math' and 'programming' can be blurry (e.g. I'd also consider If a programming construct but it does work in Integrate) ... which can cause problems and confusion. Commented Nov 6, 2014 at 16:15

The difference lies in the evaluation of f vs. g. Consider what happens when you supply them with a symbol, e.g.

f[x]
(* f[x] *)


versus

g[x]
(* Piecewise[{{x, x < 0}, {x^2, x >= 0}}, 0] *)


So, Integrate does not see the inside of f[x], and, more importantly, it cannot do any symbolic evaluation on it because Condition (/;) prevents it from doing so. A similar problem arises if you use PatternTest, instead, e.g.

h[x_?Negative] := x
h[x_?NonNegative] := x^2
Integrate[h[x], {x, -1, 1}]
(* Integrate[h[x], {x, -1, 1}] *)

• OK, is there any "magic" way to convert one form to the other? (ie, a way that requires minimal effort on my part?)
– user1722
Commented Nov 6, 2014 at 14:52
• @barrycarter I've been thinking about it. Provided that the conditions are simple, this works: Piecewise[(DownValues[f][[All, 2]] /. Verbatim[Condition][a_, b_] :> {a, b})]. Commented Nov 6, 2014 at 15:07
• @AlexeyPopkov I added it as neither evaluate to True, so the rhs is never seen by Integrate. Similar problem, same result. Commented Nov 6, 2014 at 16:58