# Explain a Mathematica winning one-liner

Dynamic[Refresh[
Graphics3D[
Sphere[l -=
MapIndexed[
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.

-
Suggestion ... Start learning Dynamics with easier examples :) –  belisarius Jan 4 '13 at 2:02
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. –  fizzix Jan 4 '13 at 2:33
This is one really impressive one-liner! –  m_goldberg Jan 4 '13 at 2:33
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. –  Alexey Popkov 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$.

-
+1 I never thought it could be done in less than a few pages –  belisarius Jan 4 '13 at 2:46
That was a great explanation! I understand it a lot better now. Thanks! –  fizzix Jan 4 '13 at 2:54
Why is #2 gets rid of the self attraction? I'm not sure what the "&, l" and "l -=" part do. –  fizzix Jan 4 '13 at 3:04
@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 –  The Toad Jan 4 '13 at 3:58
@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`. –  Michael E2 Jan 4 '13 at 5:52