# Piecewise integration doesn't evaluate

I'm trying to integrate the following function: $$q(d, \theta) = q_x(d, \theta) + q_y(d, \theta) - 2q_x(d, \theta) q_y(d, \theta)$$ where $$q_x(d, \theta) = \min\left(2 - d\cos\theta, d\cos\theta\right)$$ and $$q_y(d, \theta)$$ is defined likewise with $$\sin$$. So I defined the following functions in Mathematica:

qx[d_, o_] := Min[2 - d*Cos[o], d*Cos[o]]
qy[d_, o_] := Min[2 - d*Sin[o], d*Sin[o]]
q[d_, o_] := PiecewiseExpand[qx[d, o] + qy[d, o] - 2*qx[d, o]*qy[d, o]]


but when I try and integrate, Mathematica doesn't calculate $$\int_0^{\frac{\pi}{2}} q(d, \theta) \,d\theta$$: Nothing works to simplify that: PiecewiseExpand, FullSimplify don't work. Interestingly enough, integrating $$q_x$$ and $$q_y$$ work: What's going on here? Why can't it handle the product of the two? I've tried just doing Integrate[qx[d, o]*qy[d, o], {o, 0, Pi/2}] with similar failure.

\$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global*"]

qx[d_, o_] := Min[2 - d*Cos[o], d*Cos[o]]
qy[d_, o_] := Min[2 - d*Sin[o], d*Sin[o]]
q[d_, o_] := PiecewiseExpand[qx[d, o] + qy[d, o] - 2*qx[d, o]*qy[d, o]]


Integrate needs a numeric value for d. However, it is extremely slow.

int[d_?NumericQ] := Integrate[q[d, o], {o, 0, Pi/2}]

Plot[int[d], {d, -1.5, 2}] // AbsoluteTiming


Using NIntegrate is much faster but still quite slow .

int2[d_?NumericQ] := NIntegrate[q[d, o], {o, 0, Pi/2}]

Plot[int2[d], {d, -1.5, 2}] // AbsoluteTiming


• Why is it the case that d needs to be numerical? I can’t seem to find the reason in Integrate’s documentation. It worked fine with qx, so what gives with q? I wanted to find the symbolic expression for the integral, so it would be best if d was symbolic. Commented Sep 18, 2023 at 18:45
• Mathematica cannot do all integrals symbolically. When you encounter an integral that it cannot perform symbolically, you can try to find some clever trick or do the integral numerically. I don't know a clever trick that works here so I did it numerically. Commented Sep 18, 2023 at 19:56
• OK, I did do the integral by hand so I know it’s possible to do symbolically. But, it seems like Mathematica cannot. Perhaps I can try a clever trick by writing it as Piecewise functions, and explicitly defining cases in terms of o <= function of d`. Thanks. Commented Sep 19, 2023 at 20:27