# Speed up Integrate around Max/Min or Piecewise

Integrate around a convex combination of two functions containing Min or Max or Piecewise does not finish in 5 min. By contrast, Integrate finishes in a second or two when the argument is one linear function, and similarly when the argument contains one function with Min or Max or Piecewise, and similarly when the argument contains a convex combination of linear functions. In the example below, the integrals of d, da, d1, d2 take <2s and the integrals of d1a, d2a do not finish in 5 min.

Am I making some simple mistake or how to speed up similar integrals of convex combinations of Piecewise functions?

MWE:

Clear[d, d1, d2, da, d1a, d2a, cdf, pdf, cdf1, pdf1, cdf2, pdf2, s,
vi, vj, vlo, mui, muj, pi, pj, pis, pjs]
\$Assumptions =
Flatten@{Thread[0 < {s, pi, pj, pis, pjs, vlo, vi, vj}], s < 1};
cdf[v_] = v - vlo; pdf[v_] = 1;
cdf1[v_] = Max[0, Min[1, v - vlo]]; pdf1[v_] = 1;
cdf2[v_] =
Piecewise[{{0, v < vlo}, {v - vlo, vlo <= v <= vlo + 1}, {1,
v > vlo + 1}}];
pdf2[v_] =
Piecewise[{{0, v < vlo || v > vlo + 1}, {1, vlo <= v <= vlo + 1}}];
d[pi_, pj_, s_, pis_] =
Integrate[(1 - cdf[Max[pi, vj - pj + pi - s]])*pdf[vj], {vj, vlo,
vlo + 1}];
d[0.2, 0.3, 0.1, 0.25] /. {vlo -> 0.1, mui -> 0.4}
da[pi_, pj_, s_, pis_] =
Integrate[(mui*(1 - cdf[Max[pi, vj - pj + pi - s]]) + (1 - mui)*(1 -
cdf[Max[0, vj - pj] + Max[pi, pis + s]]))*pdf[vj], {vj, vlo,
vlo + 1}];
da[0.2, 0.3, 0.1, 0.25] /. {vlo -> 0.1, mui -> 0.4}
d1[pi_, pj_, s_, pis_] =
Integrate[(1 - cdf1[Max[pi, vj - pj + pi - s]])*pdf1[vj], {vj, vlo,
vlo + 1}];
d1[0.2, 0.3, 0.1, 0.25] /. {vlo -> 0.1, mui -> 0.4}
d1a[pi_, pj_, s_, pis_] =
Integrate[(mui*(1 - cdf1[Max[pi, vj - pj + pi - s]]) + (1 -
mui)*(1 - cdf1[Max[0, vj - pj] + Max[pi, pis + s]]))*
pdf1[vj], {vj, vlo, vlo + 1}];
d1a[0.2, 0.3, 0.1, 0.25] /. {vlo -> 0.1, mui -> 0.4}
d2[pi_, pj_, s_, pis_] =
Integrate[(1 - cdf2[Max[pi, vj - pj + pi - s]])*pdf2[vj], {vj, vlo,
vlo + 1}];
d2[0.2, 0.3, 0.1, 0.25] /. {vlo -> 0.1, mui -> 0.4}
d2a[pi_, pj_, s_, pis_] =
Integrate[(mui*(1 - cdf2[Max[pi, vj - pj + pi - s]]) + (1 -
mui)*(1 - cdf2[Max[0, vj - pj] + Max[pi, pis + s]]))*
pdf2[vj], {vj, vlo, vlo + 1}];
d2a[0.2, 0.3, 0.1, 0.25] /. {vlo -> 0.1, mui -> 0.4}


Using Simplify`PWToUnitStep@ does not seem to speed up the integration.

Python's Sympy package in Jupyter Notebook does symbolically integrate the functions above quickly. The integrals are not hard.