# 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 at 2:02
Are you sure the above is copied right? V9 says there is missing argument. I copied it from your post as is. Screen shot !Mathematica graphics notice the missing sign there. Even though it runs when evaluated. – Nasser Jan 4 at 2:09
The code is missing the second argument to refresh. Change the above code to : 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}]] – Nasser Jan 4 at 2:12
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 at 2:33
This is one really impressive one-liner! – m_goldberg Jan 4 at 2:33
show 1 more comment

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 at 2:46
That was a great explanation! I understand it a lot better now. Thanks! – fizzix Jan 4 at 2:54
How about: Speak[f] where f is the function given above. I found the description to be very well spoken (but she seems to stop talking before the end of the function for some reason) – Nasser Jan 4 at 3:02
Why is #2 gets rid of the self attraction? I'm not sure what the "&, l" and "l -=" part do. – fizzix Jan 4 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 – rm -rf Jan 4 at 3:58