Nerd Sniping - the Infinite Grid of Resistors #
In this file we formalize the answer and related results to xkcd's "nerd sniping" question

Here we state the problem in a more general form in aribtrary dimensions: On the $n$-dimensional grid where each neighboring nodes are connected by an one-ohm resistor, what is the equivalent resistance between the two marked nodes?
This file formalizes the following result
- The translation to a formal mathematical problem and proof of the unique solution (if exists).
- The general formula in arbitrary dimensions and for any pair of specific nodes.
- Computation for the solution in the two-dimension case, and the answer to the original question.
Hopefully this can protect me from a car accident.
1. Mathematical Model #
The equivalent resistance is computed by $V / I$, where $V$ is the electrical potential difference between the two nodes, and $I$ is the current into one node and equally out from the other. Throughout the file, we will assume the standard units (ohm, volt, and amp) and omit them from formulas. By this convention, if we fix the input current to 1, the equivalent resistance is equal to the potential difference.
Once voltage is applied to the two nodes, there are two laws that determine the current and the potential:
- Ohm's law: $R = V / I$ for any segment of the curcuit. Since every segment between two neighboring nodes has a resistance of 1, we can simplify this to $I(p, q) = U(p) - U(q)$ for any pair of neighboring nodes $p$ and $q$, where $U(p)$ is the potential at node $p$, and $I(p, q)$ is the current from $p$ to $q$.
- Kirchhoff's current law: the signed sum of currents at a node is zero. In our $n$-dimensional grid, each node has $2n$ currents with neighbors, and possibly one external current input. In our convention, the current out from the node is positive, and the current into the node is negative.
We can combine the two laws to form a formula about potentials $U(p)$ and external currents $I(p)$, whih we simply call "Kirchhoff's law" in the code: $$ \left(\sum_{i=0}^{n-1} U(p + e_i) - U(p)\right) + \left(\sum_{i=0}^{n-1} U(p - e_i) - U(p)\right) = I(p) $$ where $e_i$ are unit vectors in each direction.
In addition to the laws, we add another constraint: the potential as a function of node should be bounded. This is to rule out existence of a global external electrical field, where there is no external current input, but internal current is still induced.
We group these contraints in IsValidCircuit I U, where I : (Fin n → ℤ) → ℝ is the external
current at each node, and U : (Fin n → ℤ) → ℝ is the potential at each node.
IsValidCircuit I U means the external current input I and potential U forms a valid
circuit over the infinite grid.
- kirchhoff (x : Fin n → ℤ) : ∑ k : Fin n, (pot (x - Pi.single k 1) - pot x) + ∑ k : Fin n, (pot (x + Pi.single k 1) - pot x) = cur x
A valid circuit should follow Ohm's law and Kirchhoff's law.
A valid potential function should be bounded below.
A valid potential function should be bounded above.
Instances For
Once we have the definition of a valid circuit, the formalization of equivalent resistance is to
ask the potential difference given a pair of unit input current. We use unitCur c to express a
current input function that consists of a unit input at node c : Fin n → ℤ. The formalized
question them becomes: given a valid circuit for (unitCur 0 - unitCur x), find the potential
between 0 and x.
The definition of the solution. This definition uses Exists.choose to nonstructively return
the return the potential difference for input current (unitCur 0 - unitCur x), and returns none.
if no valid circuit exists. We will later show the existence of the valid circuit constructively,
as well as their uniqueness.
Equations
Instances For
2. Uniqueness of Solution #
This section justifies the mathematical model by showing that its solution space is sub-singleton. This follows from the discrete version of Liouville's theorem applied to bounded harmonic function, which we will prove first.
Liouville's theorem says that if bounded function (above and below) satisfies the Laplace's equation everywhere (which is equivalent to Kirchhoff's law with null external current), then it is a constant function. There are stronger versions of this theorem (e.g. only requiring one-side boundedness, and only requiring a large portion of nodes to satisfy the equation), but the elementary version will suffice here.
We follow the proof posted at https://math.stackexchange.com/a/4452978/1197328
Lemma 1 for Liouville's theorem: if a discrete harmonic function $f : \mathbb{Z}^n \to \mathbb{R}$ satisfies that for certain $L$ and a unit direction $e$ $$|f(x) + f(x + e) + f(x + 2e) + \cdots + f(x + ke)| \le L$$ for all $x$ and $k$, then $f$ is constantly 0. In the first version we shows the nonpositivity of $f$, and then use antisymmetry in the next version.
Full lemma 1 for Liouville's theorem: use antisymmetry on the previous lemma.
Liouville's theorem for two neighboring points: for a harmonic $f$, set $g(x) = f(x + e) - f(x)$, then $g$ satisfies the condition for lemma 1, and thus constantly zero, showing $f(x + e) = f(x)$.
By inductiog on the previous lemma, a bounded harmonic function is constant on a line in any direction.
By induction on the previous lemma, we get Liouville's theorem: a bounded harmonic function is constant globally.
For any specific external current functions, the valid potential function, if exists, is unique up to a constant.
3. Fourier Transform: a Potential Solution #
In this section, we construct a function φ that represents the potential function corresponding to
a singleton external current (as opposite to a pair of in/out current as we have been discussing).
Physically, such current dissipates via the grid to infinity. It is almost a valid solution to
IsValidCircuit (unitCur 0) φ, but it is not necessarily bounded. We will further discuss its
asymptotic behavior in the next section.
The construction of φ is obtained by taking an inverse Fourier transform of a certain function.
The full derivation via Fourier transform is omitted, but you can get the general idea by the proof
here.
We compute the discrete Fourier transform of unitCur 0, then state the result using inverse
Fourier transform to recover unitCur 0, which is a integral over a hypercube
$$ \varphi (x_0, x_1,\cdots,x_{n-1}) = \frac{1}{(2\pi)^n} \int_{[-\pi, \pi]^n} \left(\cos \sum_i x_i w_i\right) dw $$
Then we define function φ also as a similar inverse Fourier transform
$$ \varphi (x_0, x_1,\cdots,x_{n-1}) = \frac{1}{(2\pi)^n} \int_{[-\pi, \pi]^n} \frac{1 - \cos \sum_i x_i w_i}{\sum_i 2 - 2\cos w_i} dw $$
Equations
- One or more equations did not get rendered due to their size.
Instances For
We start exploring some basic properties of φ:
We should justify that the integral in φ actually makes sense. This is not trivial: the integrant
has an unremovable singularity at 0. It turns out that this singularity is bounded, so you can
either treat it as 0 using Lean's junk value, or remove the singularity from integration.
Lastly, we show that φ satisfies Kirchhoff's law for singleton unit current, making it an almost
solution. As a corollary, we compute the value of φ eᵢ for a unit vector eᵢ in φ_off_center
using Kirchhoff's law and symmetry.
4. Asymptotic Behavior of φ #
In this section, we show the asymptotic behavior of φ in different dimensions $n$:
- When $n \ge 3$,
φ xis bounded - When $n = 2$,
φ xgrows like $\log \lVert x \rVert$ - When $n = 1$,
φ xgrows linearly
Compare this to physical intuition: in a $n$-dimensional space, current density sourced from a point should decrease like ${\lVert x \rVert}^{-(n - 1)}$ (inverse-square law in 3D), and the potential, being the integral of t, should grow like ${\lVert x \rVert}^{-(n - 2)}$ except for $n = 2$, where the integral becomes $\log \lVert x \rVert$.
We start with the $n \ge 3$ case
We show the asymptotic behavior for $n = 2$ next. The precise statement we want to prove is: the function $g(x)$ defined below is bounded above and below
$$ g(x, y) = \varphi(x, y) - \frac{1}{4 \pi} \log (x^2 + y^2) = \varphi(x, y) - \frac{1}{2 \pi} \log \lVert (x, y) \rVert_2 $$
(This assumes the junk value $\log 0 = 0$, but a single point isn't important for the global bound)
We show this using a chain of asymptotic equivalences:
$$ \varphi(x, y) \sim \iint_{[-\pi,\pi]^2} \frac{1-\cos (x u + y v)}{4 - (2 \cos u + 2 \cos v)}du dv $$
$$ \sim \iint_{u^2+v^2\le 1} \frac{1-\cos (x u + y v)}{4 - (2 \cos u + 2 \cos v)}du dv $$
$$ \sim \iint_{u^2+v^2\le 1} \frac{1-\cos (x u + y v)}{u^2 + v^2}du dv $$
$$ \sim \int_{r=0}^{\lVert (x, y) \rVert_2} \int_{\theta=-\pi}^{\pi} \frac{1-\cos (r \cos(\theta))}{r}dr d\theta $$
$$ \sim \log \lVert (x, y) \rVert_2 $$
where each equivalence means bounded difference between two sides after multiplying by some
constants. The main idea here is that the logarithmic growth comes from the integration around the
singularity, so we can carve out a small disk (a unit disk will suffice), and change to polar
coordinates. The last equivalence is a property of Bessel function, which is proved at
asymptotic_bessel.
Equivalence when we change the denominator of the integrant.
Finally, we turn to the 1D case. We can direclty compute using induction that
$$ \varphi (x) = \frac{1}{2} |x| $$
and thus φ grows linearly in 1D case.
5. Solution to IsValidCircuit #
In this section, we prove
IsValidCircuit (unitCur a - unitCur b) (fun x ↦ φ (x - a) - φ (x - b)). The two conditions
for this are now within reach:
- Because of linarity, Kirchhoff's law holds for any linear combination of
φ. - The difference between two
φis bounded thanks to the tame asymptotics.
The hardest part in this is to show the boundedness for 2D cases. We will first show that
the difference between two log-like functions are bounded, then transfer the result to φ.
Applying this to the neighbor of the center, we get that the equivalent resistance between two neighboring points is $1 / n$.
6. Calculation for the 2D case #
In this section, we derive more closed-form results for the 2D case, and answers the original nerd sniping question.
The key result is that φ ![x, x] along the diagonal line has a simple formula
$$ \varphi(x, x) = \frac{1}{\pi}\left(1 + \frac{1}{3} + \frac{1}{5} + \cdots + \frac{1}{2x - 1}\right) $$
Combining with the known value φ ![1, 0] = 4⁻¹, it is possible to calculate any φ ![x, y]
using symmetry and Kirchhoff's law, and they will be some rational combinations of $1$ and $π$.
To calculate the integral on the diagonal line, we expand the integral domain from the square to the diamond $(2\pi, 0) - (0, 2\pi) - (-2\pi, 0) - (0, -2π)$. Because of the periodicity of the integrand, this simply doubles the resulting value. We then change the variable to rotate it by 45 degrees. The new integral can have variables separated, allowing us to calculate it.
This process is unfortunately tedious to formalize. We start with defining the diamond region,
as well as the four triangles triangleU, triangleD, triangleL, and triangleR between the
diamand and the square.
The square is the union of the same four triangles after translation.
We show that the integration in φ makes sense in all these regions.
We then show there is no overlap between regions.
Now we can rewrite φ as a integral on the diamond.
We perform the rotation, and finalize the result
With the formula ready, we can calculate φ at any given point. In fact, we can write a
recursive algorithm for this.
Given a φTable, we can extend it by another column.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Now we can verify the value of φ at any point in the first quadrant with just kernel
reduction.
Finally, let's answer the original question: the equivalent resistance is $4/\pi - 1/2$