0
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ See also Mean distance between 2 points within a sphere on the Math SE. $\endgroup$ – MarcoB Apr 2 at 12:34
  • $\begingroup$ @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. $\endgroup$ – Our Apr 2 at 12:37
  • $\begingroup$ But first of all the formula, you use is wrong. Let us start with that. $\endgroup$ – Alexei Boulbitch Apr 2 at 12:41
  • 1
    $\begingroup$ The integrand should use (x1-x2)^2 + ..., that is, the sum of square differences. $\endgroup$ – Daniel Lichtblau Apr 2 at 13:28
  • 1
    $\begingroup$ 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]] & $\endgroup$ – Bob Hanlon Apr 2 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.