Cached at:
07/27/26, 09:43 AM
**TL;DR:** The video explains in detail how to implement an N-body gravity simulation in O(N) time using the Fast Multipole Method (FMM), contrasting it with the limitations of naive O(N²) and Barnes-Hut O(N log N) approaches, with a focus on the mathematical principles of complex numbers, multipole expansions, and local expansions.
## Overview
This video demonstrates a real-time N-body simulation where hundreds of thousands of particles interact via gravity. Common implementations include the naive brute-force method (O(N²)) and the Barnes-Hut algorithm (O(N log N)). The author originally planned to implement Barnes-Hut but, after research, found it "not cool enough and error bounds are tricky". A chance link led to the **Fast Multipole Method (FMM)**:
- Proposed by Leslie Greengard and Vladimir Rokhlin in 1987
- Time complexity O(N)
- Strict mathematical error bounds
- Named one of the top ten algorithms of the 20th century
## From Brute Force to Hierarchical Thinking
The N-body problem involves computing gravitational forces between N particles. In two dimensions, the force magnitude is proportional to the product of masses divided by distance (not squared), and the force vector is obtained from the gradient of the potential function.
### Simplifying the Potential with Complex Numbers
The 2D potential is related to the logarithm of distance. Representing particle positions as complex numbers, displacements in polar form allow the product inside the logarithm to be split into a sum. The real part of the complex number is exactly the logarithm of the magnitude.
Using the **Cauchy–Riemann equations**, the derivative of a complex differentiable function can be expressed via partial derivatives. The force is the negative gradient of the potential; through the complex derivative, a compact complex expression for the force is obtained.
### Hierarchical Approach
Both FMM and Barnes-Hut use a quadtree (in 2D) to organize particles hierarchically. Tree nodes contain a box, and each level can be subdivided into four child boxes. This allows particles to be classified into near-field (requiring exact computation) and far-field (can be approximated).
Barnes-Hut approximates a far-field cluster using its center of mass, with a criterion that the node size to particle distance ratio is below a threshold θ. However, it still computes far-field contributions per particle, and the center-of-mass approximation has limited accuracy.
## The Core of FMM: Multipole Expansion
For more accurate far-field potential approximation of a cluster, FMM uses **multipole expansion**:
- The potential from a source cluster is written as a Laurent series about the origin (typically the node center).
- Coefficients \(A_k\) depend only on the source positions and masses and can be precomputed.
- Evaluation simply requires substituting the target position \(Z\) with negative powers.
This series converges outside the cluster's convergence radius (i.e., the bounding circle of the cluster). The error bound is:
\[
\left| \Phi(Z) - \Phi_P(Z) \right| \le \frac{\sum m_i}{R} \cdot \frac{(R/|Z|)^{P+1}}{1 - R/|Z|}
\]
Error decays exponentially with \(R/|Z|\). With \(P=10\), the error is already nearly invisible.
Unlike Barnes-Hut, FMM's multipole expansion uses the node center (not the center of mass) as the origin, ensuring a controllable convergence radius.
## Local Expansion: The Real Acceleration
Multipole expansions only broadcast from far-field sources to a single target point. To completely avoid per-particle computation, FMM introduces **local expansion**: centered on the target box, it collects all distant source multipole expansions and merges them into a single series around the target center. This allows a single evaluation for all particles inside the target box.
The local expansion is also a Laurent series, whose coefficients are obtained by "translating" the multipole coefficients from distant sources. The whole process consists of three steps:
1. **Upward Pass**: Compute the multipole expansion for each node (from leaves to root).
2. **Downward Pass**: Propagate the parent's local expansion downward and merge it with local expansions converted from sibling nodes' multipole expansions.
3. **Direct Computation**: For near-field particles (same node or adjacent nodes), use naive brute force.
Because each node's multipole and local expansions require only O(P²) or O(P log P) operations, and the tree depth is O(log N), the overall complexity is O(N).
## Error Analysis
The error in FMM comes from truncating multipole and local expansions. Accuracy can be controlled by choosing an appropriate truncation order \(P\). The video gives exponential error bounds; in practice, \(P\) between 10 and 20 achieves satisfactory accuracy.
## Summary
The Fast Multipole Method reduces O(N²) brute-force computation to O(N) through **multipole expansion** and **local expansion**, while guaranteeing bounded error. It is faster and more accurate than Barnes-Hut, making it the gold standard for large-scale N-body simulations. The video ends with a real-time demonstration showing hundreds of thousands of particles interacting smoothly, validating the algorithm's effectiveness.
**Source:** https://www.youtube.com/watch?v=FhMftauQZqU