# How to write these kinds of integrals in a more compact way?

I wanted to calculate the average distance between two points both lying inside $$B^2(\vec 0, R)$$, and ended up writing

Clear[r1,r2,x,y,z,R,x1,x2,y1,y2,z1,z2]
Assuming[R>0,Integrate[Integrate[Sqrt[Abs[x1^2 +y1^2+z1^2 - x2^2 - y2^2 - z2^2]], {x1,y1,z1 }\[Element]Ball[{0,0,0},R]],{x2,y2,z2}\[Element]Ball[{0,0,0},R]]]


However, given the elegance of the Mathematica language, and any my experience in programming in general, I, sort of, felt ashamed of myself writing such ugly code.

Is there a more compact & elegant way of writing the same code in a short manner such that it will also improve the speed of the code? Because I run this code on wolframcloud and it couldn't find the result withing the limits of its execution time.

• See also Mean distance between 2 points within a sphere on the Math SE. Apr 2, 2021 at 12:34
• @MarcoB thanks for the comment; I know how to calculate this quantity mathematically, but my main question is about the code rather than the mathematics that the code tries to compute.
– Our
Apr 2, 2021 at 12:37
• But first of all the formula, you use is wrong. Let us start with that. Apr 2, 2021 at 12:41
• The integrand should use (x1-x2)^2 + ..., that is, the sum of square differences. Apr 2, 2021 at 13:28
• Look at Norm[{x1, y1, z1} - {x2, y2, z2}] or Norm[{x1, y1, z1} - {x2, y2, z2}] // Simplify[#, Element[{x1, y1, z1, x2, y2, z2}, Reals]] & Apr 2, 2021 at 18:57