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.

A Mental Move is a habit for turning a problem into a computation: a question to ask before writing code, with a quantity that the code must keep invariant. The moves are catalogued in engthink.constants_benchmarks.MENTAL_MOVES_DB, each with the lecture that introduces it and the function of the package that carries it out.

The 24 Mental Moves as tiles coloured by the part of the course

Which move to try

SituationMovesModule
Small increments vanish next to a large value, or a computed variance is negative1–4representation_scaling
An experiment or a simulation has many dimensional parameters5representation_scaling
A loop with several pointers or a protocol with several states keeps failing at the boundaries6representation_scaling
A computation is too slow, and the reason is not yet known7–10invariants_complexity
A design value is hard to compute directly, but a given value is easy to test11divide_spatial
Long-range sums or convolutions cost O(N2)\mathcal{O}(N^2)12–14divide_spatial
A recursion solves the same subproblem many times, or future inputs are uncertain15–16dynamic_optimality
A greedy rule looks attractive, or a search explores far more than it needs17–18greedy_heuristic_search
A new combinatorial puzzle or a project schedule has to be solved19–20reductions_constraints
A fault must be found with few tests, or a stream must be summarized in fixed memory21–22information_streams
Local interactions may produce a sudden change of the whole system23emergence_verification
A fast heuristic or generated code passes a few hand-picked tests24emergence_verification

The moves

Part I. Representation, precision and dimensional invariants

MoveLectureFunctionWhat to keep invariant
1. Inspect the Bit Budget0representation_scaling.ieee754_float64_inspectx = (-1)^s (1 + m/2^52) 2^e; spacing = ulp(x)
2. Compensate Lost Low-Order Bits0representation_scaling.kahan_babuska_sums_k + c_k tracks exact running sum to O(eps)
3. Shift Before You Exponentiate0representation_scaling.stable_logsumexplog sum exp(z) = m + log sum exp(z - m)
4. Maintain Centered Running Moments0representation_scaling.welford_streaming_statsM2_k = M2_{k-1} + (x_k - mu_{k-1})(x_k - mu_k) >= 0
5. Nondimensionalize Before You Sweep1representation_scaling.buckingham_pi_nullspaceD @ pi = 0 collapses n vars onto n - rank(D) groups
6. Write the Loop Invariant Before the Loop2representation_scaling.dutch_national_flag_partitionarr[:lo] < p1 <= arr[lo:mid] <= p2 < arr[hi:]

Part II. Scaling, divide and conquer, spatial and frequency hierarchies

MoveLectureFunctionWhat to keep invariant
7. Measure Doubling Ratios Before Micro-Tuning3invariants_complexity.fit_empirical_complexitylog2(T(2N)/T(N)) -> asymptotic exponent p
8. Precompute Prefix Integrals for O(1) Queries3invariants_complexity.prefix_sum_2d_integralS = P[r1,c1] - P[r0,c1] - P[r1,c0] + P[r0,c0]
9. Prune Dominated Candidates in a Monotonic Queue3invariants_complexity.sliding_window_max_monotonic_dequeEach item pushed/popped <= 1 time -> <= 2N ops
10. Bucket Space into Cutoff Cells3invariants_complexity.spatial_cell_list_neighborsNeighbors within r_c lie strictly in 3x3 cells
11. Binary-Search the Answer on Monotone Feasibility4divide_spatial.parametric_binary_search_minfeasible(lo) == False and feasible(hi) == True
12. Piggyback Global Counts onto Divide-and-Conquer4divide_spatial.mergesort_with_inversionsCross-inversions counted in O(1) per right-half merge
13. Lump Distant Clusters via Multipole Trees5divide_spatial.barnes_hut_quadtree_2dOpen cell iff size / d >= theta; theta=0 is exact
14. Diagonalize Convolution in Frequency Space5divide_spatial.radix2_fft_convolveF(a * b) = F(a) . F(b) in O(N log N)
MoveLectureFunctionWhat to keep invariant
15. Tabulate Overlapping Subproblems Once6dynamic_optimality.seam_carving_min_energyEach state is solved once, in topological order of the DAG
16. Induct Backward Under Future Uncertainty7dynamic_optimality.stochastic_bellman_storageV_t(s) = max_a {r(s,a) + E[V_{t+1}(s')]}
17. Prove Greedy via an Exchange Argument (or Break It)8greedy_heuristic_search.belady_optimal_cacheEvicting furthest-in-future never worsens misses
18. Guide Search with an Admissible Lower Bound9greedy_heuristic_search.grid_shortest_path_search0 <= h(u) <= h*(u) guarantees optimal path

Part IV. Problem reduction, information and data streams

MoveLectureFunctionWhat to keep invariant
19. Compile New Constraints into Exact Cover10reductions_constraints.exact_cover_algorithm_xBranch on primary constraint with minimum options
20. Optimize Only Zero-Slack Critical Bottlenecks11reductions_constraints.critical_path_dagslack(u) = LS(u) - ES(u) == 0 defines critical path
21. Choose the Measurement That Maximizes Entropy Drop12information_streams.optimal_fault_diagnosis_treeExpected tests >= H(p) bits (Shannon bound)
22. Summarize Infinite Streams with One-Sided Sketches13information_streams.count_min_sketch_frequenciesf_true <= f_hat <= f_true + eps * N in O(1) RAM

Part V. Emergence, randomness and verification

MoveLectureFunctionWhat to keep invariant
23. Locate Critical Thresholds Before Micro-Detailing14emergence_verification.site_percolation_2dConnectivity jumps sharply across p_c ~ 0.5927
24. Fuzz Fast or AI Code Against Metamorphic & Micro-Oracles16emergence_verification.metamorphic_differential_auditEquivariance + small-N oracle catches bugs