Sphere[l -= 
      Plus @@ (Function[
           x, (d = # - x)/(d = Sqrt[d.d]) Log@d/2^(2 + 2 d)] /@ 
          Drop[l, #2]) &, l], E^-1]], UpdateInterval -> 1],
  l = RandomReal[7, {21, 3}]]

This code is from a past one-liner winner. I'm new to Mathematica and can't understand it. Can some one explain the code in ordinary language terms? I searched all the functions in the help, know @@, /@ do and currently am reading Dynamics Tutorial from Wolfram.

  • 3
    $\begingroup$ Suggestion ... Start learning Dynamics with easier examples :) $\endgroup$ Jan 4 '13 at 2:02
  • 2
    $\begingroup$ Hi, the code is here. blog.wolfram.com/2010/12/17/… There are a whole bunch of amazing code. I think he left out the UpdateInterval to save space since there is a character requirement. $\endgroup$
    – fizzix
    Jan 4 '13 at 2:33
  • $\begingroup$ This is one really impressive one-liner! $\endgroup$
    – m_goldberg
    Jan 4 '13 at 2:33
  • $\begingroup$ There is another very interesting physical simulation from Mathematica One-Liner Competition 2012: r:=.5-Random[]; p=Array[{8^9 {r,r},r+.5}&,99]; Dynamic@Graphics[ Disk@@@(p={#2 #1+{r,r}+(1-#2)MousePosition["Graphics",#1],#2}&@@@p), PlotRange->44 ]. It is interactive. $\endgroup$ Jan 5 '13 at 5:43

OK, the verbal description isn't very easy, but I'll try:

This is a simulation showing how the attractive forces between 21 spheres cause them to aggregate. Instead of simulating the equations of motion, the Dynamic approximates the physics of the attractive force and the repulsive core in the somewhat artificial body of Function[{x}, ...].

Its argument x is a three-dimensional position of a sphere in the list l containing 21 randomly generated starting locations. Relative to this position x, the attraction due to the other spheres is calculated and collected in a list. The Drop statement in the coordinate list which is injected into the Function (via the mapping /@) makes sure that the sphere doesn't try to attract itself. The argument of Drop is the index of the sphere we're currently looking at, provided as #2 by the MapIndexed statement inside which the Function appears.

In addition to the attraction, we want to get zero force at a certain minimum distance, for which temporarily the variable d is used inside Function. This is achieved with the help of the Log@d term. d becomes the inter-sphere distance by forming d = Sqrt[d.d] where on the right-hand side d was initially a vectorial difference between two spheres represented by d=#-x.

The Plus@@ part is just the step in which the contributions of the attractive forces that were collected as a list are then all added up.

The Dynamic is updated repeatedly and leads to an animation because during the display the actual coordinate list l is modified by l -= ... which subtracts from each coordinate in l an incremental displacement proportional to the net force on the sphere at that location. Comparing this to Newton's equation of motion, $\vec{F} = m \vec{a}$, something akin to this behavior arises if you assume there is an additional damping force. But the algorithm doesn't contain that explicitly - it's "fake physics" just trying to look physical. We are using for each coordinate $\vec{x}_i$ in the list l the approximation $\vec{x}_i = \vec{x}_i + \vec{v} \Delta t$ where the time interval $\Delta t$ could be considered to be unity. The instantaneous velocity $\vec v$ is assumed to be proportional to the resultant "force" on particle $i$.

  • 2
    $\begingroup$ +1 I never thought it could be done in less than a few pages $\endgroup$ Jan 4 '13 at 2:46
  • 1
    $\begingroup$ That was a great explanation! I understand it a lot better now. Thanks! $\endgroup$
    – fizzix
    Jan 4 '13 at 2:54
  • $\begingroup$ Why is #2 gets rid of the self attraction? I'm not sure what the "&, l" and "l -=" part do. $\endgroup$
    – fizzix
    Jan 4 '13 at 3:04
  • 2
    $\begingroup$ @Nasser Ok, you just made me listen to a machine speak out 63 real numbers. There's got to be some retribution for this... :P $\endgroup$
    – rm -rf
    Jan 4 '13 at 3:58
  • $\begingroup$ @fizzix The # represents the coordinates of a sphere in the list l and #2 represents its index. Drop[l,#2] is the coordinates of all the other spheres in 'l`. $\endgroup$
    – Michael E2
    Jan 4 '13 at 5:52

Your Answer

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

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