I need to integrate integrals of the type

Integrate[Boole[2 k*q >= Absolute[p^2 - k^2 - q^2]], q]

where $q$, $k$, and $p$ go from $0$ to $1$.

To learn to interpret the Mathematica output what I first evaluated (as a test) was

Integrate[Boole[2 k*q > (p^2 - k^2 - q^2)], {q, 0, 1},
          Assumptions -> 0 < k < 1 && 0 < p < 1]

Performing the integral by hand I find $1-{\rm max}(0,p-k)$ as a result. The Mathematica result yields the following piecewise function

\begin{cases} 1 & 0 < k < 1\mathtt{\;\&\&\;}p > 0\mathtt{\;\&\&\;}k-p \ge 0\\ 1 + k - p & \mathtt{True} \end{cases}

OK, the first line of the output I can interpret as a new Heaviside function which tells me that $k\geq p$ but what does the second line of the output try to tell me? Is there a convenient way to transform the piecewise output into $\min$, $\max$ functions or construct new Heaviside functions?

  • $\begingroup$ I wonder if you are reading the Piecewise expression backwards: this is telling you that, for $k \ge p$, the value of the integral is $1$; in all other cases, the integral has the value corresponding to the True condition, i.e. $1+k-p$ in this case. Notice also that you can have MMA Simplify your conditions: try Assuming[{0 < k < 1, 0 < p < 1}, Simplify@Integrate[Boole[2 k q >= p^2 - k^2 - q^2], {q, 0, 1}]]. $\endgroup$
    – MarcoB
    Jan 27, 2017 at 22:43
  • $\begingroup$ @MarcoB Thanks for the quick answer! Ok Simplify makes the result much easier to read and I also understood now that True means that the following expression is true for all cases not mentioned in the above conditions but still I wonder if one can transform such piecewise expressions into other expressions like max,min functions or new boole functions automatically? Or do I have to go through the mathematica expressions by hand to create those functions? $\endgroup$
    – Rico
    Jan 27, 2017 at 23:04
  • $\begingroup$ I don't know of an automated way to do what you ask. As a side note, frankly I find find the piece-wise output much more readable than the same thing expressed as a max function. $\endgroup$
    – MarcoB
    Jan 27, 2017 at 23:09
  • $\begingroup$ @MarcoB If i solve the original problem with the abosolute function in the Boole function my piecewise function cases get messy and lengthy while in the min max representation I still get some handy expressions which are not that long. That is why I want to to try avoid piecewise expressions. $\endgroup$
    – Rico
    Jan 27, 2017 at 23:14
  • 2
    $\begingroup$ Simplify`PWToUnitStep[%]? $\endgroup$
    – Michael E2
    Jan 28, 2017 at 2:51

1 Answer 1


Expanding on the comment by @Michael E2, we could use:

pw = Integrate[Boole[2 k*q > (p^2-k^2-q^2)], {q,0,1}, Assumptions -> 0<k<1 && 0<p<1];

unit = Simplify`PWToUnitStep[pw]

(1-UnitStep[-1+k]) (1-UnitStep[-k]) UnitStep[k-p] (1-UnitStep[-p])+(1+k-p) (1+UnitStep[-1+k]+UnitStep[-k]-UnitStep[k-p]+UnitStep[-p]-UnitStep[-k] UnitStep[-p]-(1-UnitStep[k-p]) (UnitStep[-k]+UnitStep[-p]-UnitStep[-k] UnitStep[-p])-UnitStep[-1+k] (1+UnitStep[-k]-UnitStep[k-p]+UnitStep[-p]-UnitStep[-k] UnitStep[-p]-(1-UnitStep[k-p]) (UnitStep[-k]+UnitStep[-p]-UnitStep[-k] UnitStep[-p])))

This is a bit messy, so it would be nice to Simplify this and reduce the number of UnitStep objects:

Simplify[unit] //InputForm

Piecewise[{{1, Inequality[0, Less, k, Less, 1] && k >= p && p > 0}}, 1 + k - p]

Naturally, this returned the original Piecewise object. However, it is possible to convert pw to a simpler UnitStep version as follows:

    ComplexityFunction -> (LeafCount[#] + 1000 Boole[!FreeQ[#, Piecewise]]&),
    TransformationFunctions -> {Automatic, Simplify`PWToUnitStep}

1+k-p+(k-p) (-1+UnitStep[-1+k]) (-1+UnitStep[-k]) UnitStep[k-p] (-1+UnitStep[-p])

The ComplexityFunction penalizes the presence of Piecewise objects, and the TransformationFunctions option includes the Piecewise to UnitStep transformation.

  • $\begingroup$ Simplify`PWToUnitStep looks like something one might include in mathematica.stackexchange.com/questions/805/…. Would you consider posting it there? $\endgroup$
    – Mr.Wizard
    Jan 29, 2017 at 19:46
  • 1
    $\begingroup$ The title to this question is about HeavisideTheta interpretation yet nowhere in the comments or answers does HeavisideTheta occur. $\endgroup$ Mar 17, 2022 at 21:31

Your Answer

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

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