Fast multipole method

Note · 12 November 2026 · Computer science › Algorithms

The Barnes–Hut algorithm makes the N-body problem cheap, as long as one percent of error is good enough. A graph layout can live with that. Electrostatics in a protein, or an integral equation in engineering, needs six to twelve correct digits. Barnes–Hut can only reach them by opening more and more cells, until it is slower than the direct sum.

The figure computes the same 4000 potentials three ways. Barnes–Hut at θ = 0.1 reaches an error of 10−510^{-5} and takes 1.6 s, longer than the 1.3 s of the exact direct sum. The fast multipole method (FMM) reaches 6×10−126\times10^{-12} in 82 ms. This note shows how.

Open the figure full-size.

The idea: let boxes talk to boxes

In 1986, Barnes–Hut had two limits. A far cell acted only through its total mass, so accuracy came only from opening cells. Each body also walked the tree on its own, which kept a logN\log N factor.

Greengard and Rokhlin removed both limits in 1987 (Greengard and Rokhlin 1987). Their first move keeps more information about a far box: not one number, but a series of pp terms. Each extra term buys a fixed number of digits. Their second move lets a far box act on a whole target box at once, through a second series centred on the target. Then a box costs the same, however many bodies it holds, and the total work becomes O(N)O(N).

The method needs four steps:

  1. describe every box by a multipole expansion of its charges;
  2. convert far multipoles into a local expansion around each target box;
  3. decide which boxes translate to which, with the interaction list;
  4. organise all of it as an upward and a downward pass over the tree.

This note works in 2D with the potential ϕ(z)=∑jqjlog|z−zj|\phi(z)=\sum_j q_j\log|z-z_j|, where points are complex numbers z=x+iyz=x+iy. That is the setting of the 1987 paper. In 3D the series are spherical harmonics, but the four steps stay the same.

Timeline of hierarchical N-body methods

Step 1: one box as one series

The charges qiq_i at ziz_i in a box with centre zcz_c produce, at any point further away than all of them, ϕ(z)=Re[Qlog(z−zc)+∑k=1pak(z−zc)k], \phi(z)=\mathrm{Re}\Big[\,Q\log(z-z_c)+\sum_{k=1}^{p}\frac{a_k}{(z-z_c)^k}\Big], Q=∑iqi,ak=−∑iqi(zi−zc)kk. Q=\sum_i q_i,\qquad a_k=-\sum_i\frac{q_i\,(z_i-z_c)^k}{k}. The coefficients aka_k are the multipole moments of the box. Computing them from the particles is the operator P2M.

Greengard and Rokhlin bound the error after pp terms by |error|≤Ac−1(1c)p,A=∑i|qi|,c=|z−zc|r, |\text{error}|\le\frac{A}{c-1}\left(\frac1c\right)^{p},\qquad A=\sum_i|q_i|,\quad c=\frac{|z-z_c|}{r}, where rr is the radius of a circle around zcz_c that holds all the charges. So the error falls geometrically with pp, and it falls faster further away.

In the figure, the dashed circle is that radius. With p = 0 only the total charge is left, which is what Barnes–Hut uses: the error is 10−210^{-2} even four box widths away. With p = 4 it is 10−510^{-5} at two box widths, and with p = 12 it is 8×10−108\times10^{-10}. Inside the circle the series diverges at every pp: no number of terms helps there.

Step 2: collect the far field in a local series

A multipole series still has to be evaluated once for every target body. The second idea removes that cost. Around the centre ztz_t of a target box, the field of a far box is smooth, so it has a Taylor series: ϕ(z)≈Re∑l=0pbl(z−zt)l. \phi(z)\approx\mathrm{Re}\sum_{l=0}^{p} b_l\,(z-z_t)^l . The operator M2L computes the blb_l directly from the far box’s multipole moments aka_k, without touching its particles. With z0z_0 the source centre relative to the target centre, b0=Qlog(−z0)+∑k=1pakz0k(−1)k, b_0=Q\log(-z_0)+\sum_{k=1}^{p}\frac{a_k}{z_0^k}(-1)^k, bl=−Qlz0l+1z0l∑k=1pakz0k(l+k−1k−1)(−1)k. b_l=-\frac{Q}{l\,z_0^l}+\frac{1}{z_0^l}\sum_{k=1}^{p}\frac{a_k}{z_0^k}\binom{l+k-1}{k-1}(-1)^k . Local series from many far boxes simply add up. One set of pp coefficients then carries the whole far field of a target box, and every body in the box reads its potential from it.

In the figure the two boxes are five box widths apart. With p = 2 the worst error inside the target box is 4×10−44\times10^{-4}. With p = 8 it is 10−910^{-9}, and with p = 16 it reaches the rounding error of double precision. The good region is a disc around the target. It grows with pp but never reaches the source box.

Step 3: who talks to whom

The FMM uses a quadtree of uniform depth LL: level ll has 4l4^l boxes. Two boxes on the same level are well separated when they do not touch. For a target box, the space splits into three parts:

At depth 3 one leaf needs 7 + 27 translations and 9 near boxes, out of 64 leaves. At depth 4 it needs 7 + 27 + 27 out of 256, and at depth 6 7 + 4 × 27 out of 4096. The work grows by 27 per level, while the number of leaves grows by a factor of 4. Click other leaves: boxes at the edge have shorter lists.

Step 4: the two passes

The four ideas become one algorithm with five operators.

Operator From → to Pass
P2M particles → multipole of a leaf upward
M2M four child multipoles → parent multipole upward
M2L multipole of a listed box → local series of the target downward
L2L parent local series → child local series downward
L2P local series → potential at each particle, plus the near field downward

The upward pass starts with P2M at the leaves, then shifts the multipoles up one level at a time with M2M. The shift is exact up to the truncation at pp terms, so no particle is visited twice. The downward pass starts at level 2 with the M2L translations. On each finer level, a box takes its parent’s series through L2L and adds the translations from its own list. At the leaves, L2P evaluates the series at the particles and adds the direct near-field sum.

The five FMM operators

Why the cost is O(N)

Performance analysis

All three methods run in the same JavaScript. The charges are uniform random unit charges, and the error is the largest potential error, relative to the largest potential. The leaf holds about 16 to 31 particles. The button measures again on your machine, up to N = 8000.

N direct Barnes–Hut θ = 0.5 FMM p = 8 FMM p = 16
1000 89 ms 24 ms, error 6×10−46\times10^{-4} 19 ms, 7×10−77\times10^{-7} 21 ms, 2×10−102\times10^{-10}
4000 1.3 s 208 ms, 5×10−45\times10^{-4} 126 ms, 4×10−74\times10^{-7} 115 ms, 3×10−103\times10^{-10}
16000 22.8 s 821 ms, 2×10−42\times10^{-4} 249 ms, 4×10−74\times10^{-7} 322 ms, 1×10−101\times10^{-10}

1992: Anderson’s rings

M (ring points K = 2M+1) 2 6 10 14
outer-ring error 3×10−23\times10^{-2} 2×10−42\times10^{-4} 2×10−62\times10^{-6} 5×10−85\times10^{-8}
inner-ring error (exact ring values) 3×10−33\times10^{-3} 2×10−82\times10^{-8} 4×10−134\times10^{-13} 00

Code

Further reading

References:

Anderson, Christopher R. 1992. “An Implementation of the Fast Multipole Method Without Multipoles.” SIAM Journal on Scientific and Statistical Computing 13 (4): 923–47. https://doi.org/10.1137/0913055.
Dongarra, J., and F. Sullivan. 2000. “Guest Editors Introduction to the Top 10 Algorithms.” Computing in Science & Engineering 2 (1): 22–23. https://doi.org/10.1109/mcise.2000.814652.
Greengard, L, and V Rokhlin. 1987. “A Fast Algorithm for Particle Simulations.” Journal of Computational Physics 73 (2): 325–48. https://doi.org/10.1016/0021-9991(87)90140-9.
Heer, Jeffrey. 2017. “The Barnes-Hut Approximation: Efficient Computation of N-Body Forces.” Jheer.github.io. https://jheer.github.io/barnes-hut/.