Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 5. Spatial and frequency hierarchies: Barnes–Hut quadtrees and the fast Fourier transform

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 5. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Verify Discrete Parseval’s Energy Theorem ∑∣xn∣2=1N∑∣X^k∣2\sum |x_n|^2 = \frac{1}{N}\sum |\hat{X}_k|^2 using divide_spatial._fft_radix2 across 20 random complex signals (N=512N = 512), and verify the exact modulo-NN folding relation between circular FFT product and divide_spatial.radix2_fft_convolve.


Exercise 1: Parseval energy conservation and modulo-N circular convolution folding
==================================================================================
Max Parseval energy relative error across 20 complex signals : 2.19e-16
Max |Circular FFT(N) - Modulo-N Folded Linear Convolution|   : 7.11e-15

Explanation. Up to normalization 1/N1/\sqrt{N}, the DFT matrix Fk,n=e−2πikn/N/N\mathbf{F}_{k,n} = e^{-2\pi i k n / N}/\sqrt{N} is unitary (F∗F=I\mathbf{F}^*\mathbf{F} = \mathbf{I}), preserving inner products and total physical wave energy. Without zero-padding, sampling frequency domain at ωk=2πk/N\omega_k = 2\pi k / N enforces periodic boundary conditions xn+N≡xnx_{n + N} \equiv x_n, folding index n≥Nn \ge N of the linear convolution directly onto n−Nn - N.


Exercise 2

Problem. Quantify (a) non-causal wrap-around pre-echo energy at n<90n < 90 when an impulse at n0=980n_0 = 980 (N=1024N = 1024) is convolved with a 128-tap exponential filter without zero-padding, and (b) net system force drift ∥∑iFi∥\left\|\sum_i \mathbf{F}_i\right\| (Newton’s 3rd Law violation) in barnes_hut_quadtree_2d at θ=1.2\theta = 1.2 vs. θ=0.0\theta = 0.0.


Exercise 2: Audit of unpadded FFT causal wrap-around and Barnes-Hut momentum drift
==================================================================================
(a) Causal output energy on n in [0..89] (before shock at n=980):
    True zero-padded linear conv = 3.05e-30 | Unpadded circular FFT = 62.45 <-- wrap-around before the impulse

(b) Net system momentum-drift force ||sum F_i||:
    Exact theta = 0.0 : 1.52e-13 | Aggressive theta = 1.2 : 114.5613 (119.44% of mean particle force)

Explanation. Because 980+127=1107>1024980 + 127 = 1107 > 1024, output samples 1024…11071024 \dots 1107 wrap modulo 1024 onto n=0…83n = 0 \dots 83, so the circular result shows signal before the impulse arrives. In Barnes–Hut, particle ii in a tight cluster may lump a wide cell BB (sB/d<θs_B / d < \theta), while particles j∈Bj \in B open ii’s parent cell differently, breaking pairwise antisymmetry Fij=−Fji\mathbf{F}_{ij} = -\mathbf{F}_{ji} so the centre of mass of an isolated system drifts unless symmetric cell–cell interactions (Fast Multipole Method) are used.


Exercise 3

Problem. Implement quadtree_range_count(coords, rect_queries) building a 2-D Quadtree with exact subtree counts node.count and answering each rectangle (xmin, ymin, xmax, ymax) by pruning disjoint quadrants (0) and short-circuiting wholly contained quadrants (+node.count). Verify 100%100\% exact counts against brute force across 80 random rectangles on N=2,000N = 2{,}000 clustered points while achieving >10×>10\times reduction in point/node inspections.


Exercise 3: Implement it yourself — sublinear 2-D quadtree orthogonal range counting
====================================================================================
80 range queries on N = 2,000 points -> Exact match: True
Brute-force point checks = 160,000 vs Quadtree node visits = 12,508 (12.8x speedup)

Summary. Small query boxes are answered by pruning disjoint quadrants near the root, and large boxes by accepting whole interior subtrees in O(1)\mathcal{O}(1) through node.count. Only cells that straddle the edge of the query rectangle are opened further, so the work scales with the perimeter of the box rather than its area; here 12,508 node visits replace 160,000 point checks.