# Elegant way to integrate absolute value functions

I have a certain class of integrals that I have to calculate quite often. The special thing about them is that they all contain an absolute value function (with a real argument). Here is an example:

function = Sin[m*r1]*Sin[n*r2]*Exp[I*k*Abs[r1 - r2]]
fullIntegral = Integrate[Integrate[function, {r2, 0, R}], {r1, 0, R}]
(* Returns unevaluated integral expression *)


The problem is that Mathematica is not able to simplify the absolute value. I have tried teaching it to do so by using assumptions and simplify etc (following Simplifying the derivative of $|x|$, Numerical Integration with absolute value and http://www.walkingrandomly.com/?p=2283), but I can't seem to get it to work for this type of integral.

I do have a way to do it with Mathematica, by evaluating the integral in domains by hand

function2 = Sin[m*r1]*Sin[n*r2]*Exp[I*k*(r1 - r2)];
function3 = Sin[m*r1]*Sin[n*r2]*Exp[I*k*(r2 - r1)];
fullIntegral2 = Simplify[Integrate[Integrate[function2, {r2, 0, r1}], {r1, 0, R}] + Integrate[Integrate[function3, {r2, r1, R}], {r1, 0, R}]]
(* evaluates correctly, returning a nice algebraic expression *)


I have been doing these kinds of integrals like that for a while now, except it is starting to get tedious with multiply domains and dimensions. Mathematica can clearly perform these integrals, so my question is how to do it using the input function instead of function1, function2 and the corresponding domains.

One approach is to use a Piecewise function:

function[r1_, r2_] = Sin[m*r1]*Sin[n*r2]*
Piecewise[{{Exp[I*k*(r1 - r2)], r1 > r2}, {Exp[I*k*(r2 - r1)],r1 <= r2}}];
Integrate[function[r1, r2], {r1, 0, r}, {r2, 0, r}, Assumptions -> r > 0]


FullSimplify makes the answer a bit shorter. You can transform the Abs function into Piecewise form:

PiecewiseExpand[RealAbs[r2 - r1]]


For example, for your function, it looks like you need to use PiecewiseExpand twice:

function[r1_, r2_] = PiecewiseExpand[Sin[m*r1]*Sin[n*r2]*
PiecewiseExpand[Exp[I*k*RealAbs[r1 - r2]]]]

• +1 great stuff! Now I just need a way to translate the absolute value expression into the Piecewise function. – Wolpertinger Sep 4 '18 at 16:12
• See update for how to change Abs into Piecewise. – bill s Sep 4 '18 at 16:25