HiPPO theory: orthogonal-basis projection operators
Online function approximation by projection onto orthogonal polynomial bases — the HiPPO-LegS operator, its lower-triangular structure and exact spectrum, discretization to the S4 recurrence, and the basis-conditioning question.
On this page
- 7.1 The online function-approximation problem
- 7.2 Orthogonal polynomial bases
- 7.3 The HiPPO-LegS operator
- 7.4 From the operator to the S4-style recurrence
- 7.5 Conditioning of the HiPPO basis
- 7.6 Alternative bases
- 7.7 Eigenvalue structure and stability
- 7.8 What’s next
- 7.9 Exercises
- Exercise 7.1 (computation)
- Exercise 7.2 (computation)
- Exercise 7.3 (computation + code)
- Exercise 7.4 (theory) — solution in §7.10
- Exercise 7.5 (theory) — solution in §7.10
- Exercise 7.6 (theory) — solution in §7.10
- 7.10 Full solutions to theory exercises
- Solution to Exercise 7.4
- Solution to Exercise 7.5
- Solution to Exercise 7.6
- 7.11 Companion code
HiPPO theory: orthogonal-basis projection operators
7.1 The online function-approximation problem
A recurrent model carries a finite state and updates it one sample at a time. The central question is what should that state store? HiPPO’s answer is sharp: the state should store a compressed representation of the entire input history, chosen so that the history can be reconstructed as accurately as numbers allow.
Make this precise. Fix a time and consider the input seen so far, for . Equip the interval with a measure (a weighting that says which parts of the history matter). The best -term approximation of in the weighted space is its orthogonal projection onto an -dimensional subspace spanned by basis functions :
The coefficient vector is the compressed memory. HiPPO’s discovery is that for well-chosen bases obeys a linear ODE in — so maintaining the optimal projection online is exactly running a linear state-space model.
The picture to keep in mind is reconstruction. Drive a signal into the operator, read off at the final time, and expand : you recover the history, and the recovery sharpens as grows.
7.2 Orthogonal polynomial bases
The natural basis for functions on an interval is the Legendre polynomials . On they are orthogonal under the uniform measure,
and satisfy the three-term recurrence with , . Rescaling to the unit interval and normalizing gives an orthonormal family
These are the basis functions HiPPO projects onto; the companion’s Gram-matrix check confirms their orthonormality to under trapezoidal quadrature.
Orthogonality is what makes projection cheap and optimal. Because the basis is orthonormal, the best approximation is obtained coefficient-by-coefficient — no linear system to solve.
(Orthogonal projection is the best approximation.) Let be orthonormal in and let . For any , the coefficients minimize the reconstruction error: for every , with equality only when .
The proof (Exercise 7.5, §7.10) is the Hilbert-space Pythagorean theorem: the residual is orthogonal to , so any other only adds an orthogonal error that grows the norm.
The measure encodes which history matters. Two choices dominate the HiPPO literature Gu et al. (2020) :
- LegT (translated Legendre): a sliding window of fixed length — the state remembers the most recent time units uniformly and forgets everything older. This is a fixed-horizon memory.
- LegS (scaled Legendre): the growing window with uniform weight, rescaled as increases. The state remembers all history, allocating its coefficients to the whole past at every scale. This is the variant that gives S4 its long-range reach, and the one we develop in detail.
7.3 The HiPPO-LegS operator
For the scaled measure the projection coefficients obey a strikingly clean ODE. Differentiating through the time-dependence of the measure and using the Legendre recurrence yields the HiPPO-LegS dynamics, a linear state-space system whose matrices have a closed form.
(HiPPO-LegS closed form.) The scaled-Legendre projection coefficients evolve under a linear system with state matrix and input matrix given by
for . In particular is lower-triangular.
The derivation (Exercise 7.4, §7.10) substitutes the Legendre three-term recurrence into the time-derivative of the projection integral; the lower-triangular structure is inherited from the recurrence, which expresses using only lower-degree polynomials. The matrix has a vivid signature: a negative diagonal that grows linearly, and negative off-diagonal entries below it that grow like .
This is the answer to Chapter 4’s open question. Where Chapter 4 asked “given , how do we discretize it?”, HiPPO supplies the — not by guessing, but as the projection-update operator of an optimal polynomial memory. Random initialization of produces eigenvalues scattered across the complex plane and dynamics that vanish or explode over long horizons; the HiPPO is engineered so that its discretization gives stable, long-memory recurrences Gu et al. (2020) .
7.4 From the operator to the S4-style recurrence
To run the HiPPO operator on sampled data we discretize the LTI system exactly as in Chapter 4 §4.3. The zero-order hold gives
computed in practice through the augmented matrix-exponential trick of Chapter 4 (the HiPPO is invertible — that promise from §4.3 is kept here). Because every eigenvalue of has negative real part (§7.7), Chapter 4’s ZOH stability proposition applies verbatim: every discrete eigenvalue satisfies , so the recurrence is stable for any step size. ZOH is this book’s default discretization throughout Chapters 7–9 precisely because it preserves the HiPPO spectrum so cleanly (the original S4 paper used the bilinear transform instead, and S4D supports either; Chapter 4 §4.6 lays out the trade-off).
This recurrence is the recurrent view of an S4-style layer Gu et al. (2022) . Chapter 8 adds the second view — unrolling the recurrence into a convolution and computing it with an FFT — and shows the two views are identical for zero initial state. The HiPPO matrix is exactly the initialization that S4 places on before training; S4D Gu et al. (2022) then diagonalizes it for speed, a move §7.5 explains is delicate precisely because of conditioning. Everything S4 does is downstream of the operator in §7.3.
7.5 Conditioning of the HiPPO basis
The HiPPO-LegS matrix is lower-triangular and its eigenvalues are the well-separated integers (§7.7), which sounds maximally benign. It is not, and the reason is non-normality: is far from symmetric, so its eigenvectors are highly oblique and the matrix that diagonalizes it is ill-conditioned. The condition number of that eigenvector basis grows exponentially in Yu et al. (2023) , so a naive diagonalization of HiPPO-LegS is numerically untrustworthy at the used in practice — and the sub-quadratic conditioning one wants is achieved only by deliberately perturbing the transform (the “robustifying” construction below).
Two consequences follow. First, diagonalization is dangerous: S4D’s diagonal reparameterization Gu et al. (2022) must be done in a numerically careful (often complex, approximately-diagonal) form, and the “robustifying” line of work Yu et al. (2023) exists exactly to tame the conditioning of the diagonalizing transform. Second, the choice of discretization interacts with conditioning: a poorly conditioned basis amplifies the per-step error of a low-order scheme, so the second-order schemes of Chapter 4 (and the geometric integrators of Chapter 6) are not academic refinements but a response to a real failure mode. This conditioning question is the seed of research niche C4 — characterizing where HiPPO-style projections become numerically untrustworthy and what to do about it.
7.6 Alternative bases
Legendre is one choice among many; HiPPO is a framework parameterized by the basis–measure pair Gu et al. (2023) . Swapping the basis changes what the state is good at remembering:
- LagT (Laguerre): an exponentially-weighted infinite horizon — the measure decays into the past, so the state emphasizes recent history with a graceful tail.
- FouT (Fourier): a Fourier basis on a sliding window, well matched to periodic or oscillatory inputs; closely related to the implicit long convolutions of Chapter 11.
- Wavelet bases (WaLRUS): replacing polynomials with a redundant wavelet frame Babaei et al. (2025) shifts the optimal-projection target from polynomial smoothness to wavelet sparsity, which can suit signals with localized or multi-scale structure better than a global polynomial basis.
The “How to Train Your HiPPO” generalization Gu et al. (2023) derives the projection ODE for arbitrary measure–basis pairs and identifies which ones are well-conditioned to train — directly connecting basis choice back to §7.5. The lesson is that the structure of (lower-triangular, stable spectrum, projection-update semantics) survives the basis change; the closed-form entries do not. When a later chapter reaches for a non-Legendre SSM, this is the machinery underneath.
7.7 Eigenvalue structure and stability
Return to the matrix itself. Because is lower-triangular, its eigenvalues are exactly its diagonal entries.
(HiPPO-LegS spectrum.) The eigenvalues of the HiPPO-LegS matrix are exactly — real, distinct, and strictly negative. Consequently the continuous system is asymptotically stable (Chapter 2), and its ZOH discretization has all discrete eigenvalues strictly inside the unit disk for every (Chapter 4).
The proof is immediate from lower-triangularity (Exercise 7.6, §7.10). The companion verifies it numerically: for up to the computed eigenvalues match to better than , and the strict upper triangle is exactly zero.
The integer spectrum has a memory interpretation: under ZOH the -th mode decays per step by , so low-index coefficients (coarse features of the history) are long-lived while high-index coefficients (fine detail) fade fastest. The state is a graded, multi-timescale memory — and the learnable slides the whole schedule. This is the dynamical-systems reading of why HiPPO works, and it is the structure a stability or Lyapunov analysis of a trained S4 layer (Chapter 15) measures against.
7.8 What’s next
This chapter answered “what is ?” with “the projection-update operator of an optimal polynomial memory,” and showed that operator is lower-triangular, stably-spectrumed, and ZOH-discretizable into a recurrence. Chapter 8 takes that recurrence and develops the full S4 / S4D / S5 machinery: the convolutional view, the FFT kernel, the diagonalization that §7.5 warned is delicate, and the parallel scan. Chapter 9 then makes the parameters input-dependent — the selective SSMs (Mamba) — and Chapter 10 swaps ZOH for the exponential-trapezoidal scheme of Chapter 4 when the dynamics turn stiff. HiPPO is the hinge: every architecture from here inherits the structured derived in §7.3.
7.9 Exercises
Six problems mixing computation and theory. Short/numerical (7.1–7.3) have inline collapsible solutions; the theory exercises (7.4–7.6) have full worked solutions in §7.10.
Exercise 7.1 (computation)
Using the three-term recurrence, write down and verify by direct integration that . Then confirm .
Solution
. The product is even, and , so the integral is — Legendre orthogonality. For the norm, , matching .
Exercise 7.2 (computation)
Build the HiPPO-LegS matrix from the closed form of §7.3 by hand. Verify that and that the diagonal is .
Solution
Entry has , so . The diagonal entries are for , i.e. . The full matrix is lower-triangular with , and zeros above the diagonal. The companion hippo_matrix.py prints exactly .
Exercise 7.3 (computation + code)
Run companions/ch07/jax/hippo_reconstruction.py. Confirm that the relative reconstruction error of the two-sinusoid test signal falls sharply from to , then plateaus. Why does it plateau rather than continue to zero?
Solution
The companion prints errors . The sharp drop happens once is large enough to represent the signal’s two frequencies (≈ 1.5 and 4 cycles on the window); beyond that the error plateaus at the discretization floor of the time-varying LegS recurrence at the chosen sample count (), not at zero. Adding more Legendre modes cannot beat the error already incurred by integrating the projection ODE on a finite grid — exactly the conditioning/discretization interaction of §7.5. The Julia and PyTorch companions reproduce these numbers to machine precision.
Exercise 7.4 (theory) — solution in §7.10
Show that the HiPPO-LegS matrix is lower-triangular, and that its diagonal entries are , by appealing to the structure of the Legendre three-term recurrence. (You may take the off-diagonal closed form as given; the point is the structure.)
Exercise 7.5 (theory) — solution in §7.10
Prove the best-approximation theorem (Theorem, §7.2): if is orthonormal in and , then is the unique minimizer of over .
Exercise 7.6 (theory) — solution in §7.10
Using lower-triangularity, prove that the eigenvalues of are exactly . Then show that ZOH discretization sends every eigenvalue strictly inside the unit disk for all , and identify the per-step decay rate of the -th mode.
7.10 Full solutions to theory exercises
Solution to Exercise 7.4
The shifted Legendre polynomials satisfy a three-term recurrence in which is expressed through and — never through higher-degree polynomials. When the projection coefficient is differentiated in , the time-derivative of the (scaled) measure couples to the inner products of against — and the recurrence guarantees only appear. Hence depends on but not on , which is exactly the statement that has zero entries above the diagonal: for . For the diagonal, the “self-coupling” term of the -th coefficient under the scaled measure contributes (the normalization and the measure’s scaling combine to the integer ); this is the claim. The off-diagonal entries for follow from the same expansion carried through the normalization, given here without the full bookkeeping.
Solution to Exercise 7.5
Let with , and let be any element of . First, the residual is orthogonal to every :
using orthonormality . Therefore , and in particular since . Now decompose and apply the Pythagorean theorem:
with equality iff , i.e. . So is the unique minimizer.
Solution to Exercise 7.6
A lower-triangular matrix has its eigenvalues on the diagonal: is the product of diagonal entries because the determinant of a triangular matrix is the product of its diagonal, and subtracting keeps it triangular. With the roots are for , i.e. exactly — real, distinct, strictly negative.
Under ZOH the discrete dynamics matrix is , whose eigenvalues are . Then
so every discrete eigenvalue lies strictly inside the unit disk (A-stability, Chapter 4). The per-step decay rate of mode is : higher-index modes decay faster, giving the graded multi-timescale memory of §7.7.
7.11 Companion code
Three runnable companions for Chapter 7 — the first SSM-core chapter, where the foundations and numerics threads meet in one operator. All three language tracks produce the same HiPPO-LegS matrix and the same reconstruction errors, which is itself the lesson: the mathematics is framework-independent; the idioms differ.
JAX (companions/ch07/jax/):
hippo_matrix.py— builds the HiPPO-LegS from the §7.3 closed form with a vectorizedjnp.where(no Python loop,jit-fusable); emitshippo_matrix_structure.pngandhippo_eigenvalues.png.hippo_reconstruction.py— runs the time-varying LegS recurrence viajax.lax.scanand reconstructs the history; emitslegendre_basis.pngandhippo_reconstruction.png.tests/test_hippo.py— pins the closed form, the integer spectrum, the reconstruction-error decay, and alax.scan-vs-naive-loop equivalence guard.
Julia (companions/ch07/julia/):
hippo_legendre.jl— the same operator with stdlibLinearAlgebraonly; the encoder is an explicitforloop (the contrast tolax.scan), and the Legendre basis is built from the three-term recurrence.runtests.jl—@testsetassertions including an orthonormality Gram-matrix check.
PyTorch (companions/ch07/torch/):
hippo_operator.py— HiPPO-LegS wrapped as annn.Moduleprojection operator (fixed buffers, zero learnable parameters); a define-by-run eager loop encodes the signal. This is the object Chapter 8’s S4 layer extends.tests/test_hippo_torch.py— cross-framework consistency: the matrix matches the closed form and the reconstruction errors reproduce the JAX/Julia numbers.
To run from the repo root:
# JAX (uses the uv .venv with jax, numpy, scipy, matplotlib)
PYTHONPATH=. python companions/ch07/jax/hippo_matrix.py
PYTHONPATH=. python companions/ch07/jax/hippo_reconstruction.py
# Julia (stdlib only; instantiates instantly)
julia --project=companions/ch07/julia companions/ch07/julia/hippo_legendre.jl
# PyTorch (needs the .venv [torch] extra)
PYTHONPATH=. python companions/ch07/torch/hippo_operator.py
All figures emit to public/figures/ch07/.