Part II · SSM Core Week 7 Published

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
  1. 7.1 The online function-approximation problem
  2. 7.2 Orthogonal polynomial bases
  3. 7.3 The HiPPO-LegS operator
  4. 7.4 From the operator to the S4-style recurrence
  5. 7.5 Conditioning of the HiPPO basis
  6. 7.6 Alternative bases
  7. 7.7 Eigenvalue structure and stability
  8. 7.8 What’s next
  9. 7.9 Exercises
  10. Exercise 7.1 (computation)
  11. Exercise 7.2 (computation)
  12. Exercise 7.3 (computation + code)
  13. Exercise 7.4 (theory) — solution in §7.10
  14. Exercise 7.5 (theory) — solution in §7.10
  15. Exercise 7.6 (theory) — solution in §7.10
  16. 7.10 Full solutions to theory exercises
  17. Solution to Exercise 7.4
  18. Solution to Exercise 7.5
  19. Solution to Exercise 7.6
  20. 7.11 Companion code

HiPPO theory: orthogonal-basis projection operators

7.1 The online function-approximation problem

A recurrent model carries a finite state htRN\statevec_t \in \R^N 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 NN numbers allow.

Make this precise. Fix a time t>0t > 0 and consider the input seen so far, u(s)u(s) for s[0,t]s \in [0, t]. Equip the interval with a measure ωt(s)\omega_t(s) (a weighting that says which parts of the history matter). The best NN-term approximation of uu in the weighted space L2(ωt)L^2(\omega_t) is its orthogonal projection onto an NN-dimensional subspace spanned by basis functions g0,,gN1g_0, \dots, g_{N-1}:

u(s)    n=0N1cn(t)gn(s),cn(t)=u,gnωt=0tu(s)gn(s)ωt(s)ds.u(s) \;\approx\; \sum_{n=0}^{N-1} c_n(t)\, g_n(s), \qquad c_n(t) = \langle u, g_n \rangle_{\omega_t} = \int_0^t u(s)\, g_n(s)\, \omega_t(s)\, ds.

The coefficient vector c(t)=(c0(t),,cN1(t))c(t) = (c_0(t), \dots, c_{N-1}(t))^\top is the compressed memory. HiPPO’s discovery is that for well-chosen bases c(t)c(t) obeys a linear ODE in tt — 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 c(t)c(t) at the final time, and expand ncngn\sum_n c_n g_n: you recover the history, and the recovery sharpens as NN grows.

Left: a two-sinusoid history and its HiPPO-LegS reconstructions at N=4, 16, 64, converging to the true signal. Right: relative L2 reconstruction error versus N on a log scale, dropping sharply then plateauing.
HiPPO-LegS as online function approximation. Left: the input history (dark) and its reconstruction from the HiPPO state at N=4, 16, 64. Right: relative $L^2$ error falls from $0.80$ at $N=4$ to $0.011$ at $N=16$, then plateaus near $7\times10^{-3}$ once the state dimension exceeds the signal's intrinsic complexity. Produced by companions/ch07/jax/hippo_reconstruction.py.

7.2 Orthogonal polynomial bases

The natural basis for functions on an interval is the Legendre polynomials PnP_n. On [1,1][-1, 1] they are orthogonal under the uniform measure,

11Pn(x)Pm(x)dx=22n+1δnm,\int_{-1}^{1} P_n(x)\, P_m(x)\, dx = \frac{2}{2n+1}\,\delta_{nm},

and satisfy the three-term recurrence (n+1)Pn+1(x)=(2n+1)xPn(x)nPn1(x)(n+1)P_{n+1}(x) = (2n+1)\,x\,P_n(x) - n\,P_{n-1}(x) with P0=1P_0 = 1, P1(x)=xP_1(x) = x. Rescaling to the unit interval z[0,1]z \in [0,1] and normalizing gives an orthonormal family

P~n(z)=2n+1Pn(2z1),01P~n(z)P~m(z)dz=δnm.\tilde P_n(z) = \sqrt{2n+1}\,P_n(2z - 1), \qquad \int_0^1 \tilde P_n(z)\, \tilde P_m(z)\, dz = \delta_{nm}.

These are the basis functions HiPPO projects onto; the companion’s Gram-matrix check confirms their orthonormality to 10310^{-3} under trapezoidal quadrature.

The first five normalized shifted Legendre basis functions on the unit interval, increasing in oscillation with degree.
The normalized shifted Legendre basis $\tilde P_n(z) = \sqrt{2n+1}\,P_n(2z-1)$ for $n = 0,\dots,4$ on $z \in [0,1]$. Higher-degree polynomials oscillate more and resolve finer detail of the history. Produced by companions/ch07/jax/hippo_reconstruction.py.

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.

Theorem 7.1.

(Orthogonal projection is the best approximation.) Let {gn}n=0N1\{g_n\}_{n=0}^{N-1} be orthonormal in L2(ω)L^2(\omega) and let V=span{gn}V = \operatorname{span}\{g_n\}. For any uL2(ω)u \in L^2(\omega), the coefficients cn=u,gnωc_n = \langle u, g_n\rangle_\omega minimize the reconstruction error: uncngnωuvω\big\lVert u - \sum_n c_n g_n \big\rVert_\omega \le \big\lVert u - v \big\rVert_\omega for every vVv \in V, with equality only when v=ncngnv = \sum_n c_n g_n.

The proof (Exercise 7.5, §7.10) is the Hilbert-space Pythagorean theorem: the residual uncngnu - \sum_n c_n g_n is orthogonal to VV, so any other vVv \in V only adds an orthogonal error that grows the norm.

The measure ωt\omega_t encodes which history matters. Two choices dominate the HiPPO literature Gu et al. (2020) :

  • LegT (translated Legendre): a sliding window of fixed length θ\theta — the state remembers the most recent θ\theta time units uniformly and forgets everything older. This is a fixed-horizon memory.
  • LegS (scaled Legendre): the growing window [0,t][0,t] with uniform weight, rescaled as tt increases. The state remembers all history, allocating its NN 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 cn(t)=u,P~nc_n(t) = \langle u, \tilde P_n\rangle 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.

Proposition 7.2.

(HiPPO-LegS closed form.) The scaled-Legendre projection coefficients evolve under a linear system with state matrix ARN×N\statemat \in \R^{N\times N} and input matrix BRN×1\inputmat \in \R^{N\times 1} given by

Aij={(2i+1)(2j+1)i>ji+1i=j0i<jBi=2i+1,\statemat_{ij} = -\begin{cases} \sqrt{(2i+1)(2j+1)} & i > j \\ \,i + 1 & i = j \\ \,0 & i < j \end{cases} \qquad \inputmat_{i} = \sqrt{2i+1},

for i,j=0,,N1i, j = 0, \dots, N-1. In particular A\statemat 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 Pn+1P_{n+1} 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 ij\sqrt{ij}.

Heatmap of the 8x8 HiPPO-LegS A matrix: zero above the diagonal, negative on and below it, with magnitudes growing toward the bottom-left.
The HiPPO-LegS state matrix $\statemat$ for $N=8$. Strictly upper-triangular entries are exactly zero; the diagonal is $-(i{+}1)$ and the sub-diagonal block grows in magnitude like $-\sqrt{(2i{+}1)(2j{+}1)}$. The lower-triangular structure is the algebraic fingerprint of the Legendre recurrence. Produced by companions/ch07/jax/hippo_matrix.py.

This is the answer to Chapter 4’s open question. Where Chapter 4 asked “given A\statemat, how do we discretize it?”, HiPPO supplies the A\statemat — not by guessing, but as the projection-update operator of an optimal polynomial memory. Random initialization of A\statemat produces eigenvalues scattered across the complex plane and dynamics that vanish or explode over long horizons; the HiPPO A\statemat 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 ddth=Ah+Bu\ddt \statevec = \statemat \statevec + \inputmat u exactly as in Chapter 4 §4.3. The zero-order hold gives

Aˉ=eAΔ,Bˉ=A1 ⁣(eAΔI)B,hk+1=Aˉhk+Bˉuk,\discA = e^{\statemat \stepsize}, \qquad \discB = \statemat^{-1}\!\left(e^{\statemat \stepsize} - I\right)\inputmat, \qquad \statevec_{k+1} = \discA\, \statevec_k + \discB\, u_k,

computed in practice through the augmented matrix-exponential trick of Chapter 4 (the HiPPO A\statemat is invertible — that promise from §4.3 is kept here). Because every eigenvalue of A\statemat has negative real part (§7.7), Chapter 4’s ZOH stability proposition applies verbatim: every discrete eigenvalue satisfies μi=eRe(λi)Δ<1\abs{\mu_i} = e^{\operatorname{Re}(\lambda_i)\stepsize} < 1, 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 A\statemat 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 1,,N-1, \dots, -N (§7.7), which sounds maximally benign. It is not, and the reason is non-normality: A\statemat 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 NN Yu et al. (2023) , so a naive diagonalization of HiPPO-LegS is numerically untrustworthy at the NN 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 A\statemat (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 A\statemat is lower-triangular, its eigenvalues are exactly its diagonal entries.

Proposition 7.3.

(HiPPO-LegS spectrum.) The eigenvalues of the HiPPO-LegS matrix A\statemat are exactly {1,2,,N}\{-1, -2, \dots, -N\} — real, distinct, and strictly negative. Consequently the continuous system ddth=Ah+Bu\ddt \statevec = \statemat\statevec + \inputmat u is asymptotically stable (Chapter 2), and its ZOH discretization has all discrete eigenvalues strictly inside the unit disk for every Δ>0\stepsize > 0 (Chapter 4).

The proof is immediate from lower-triangularity (Exercise 7.6, §7.10). The companion verifies it numerically: for NN up to 3232 the computed eigenvalues match 1,,N-1, \dots, -N to better than 10810^{-8}, and the strict upper triangle is exactly zero.

The 16 eigenvalues of the HiPPO-LegS matrix lie on the negative real axis at the integers minus 1 through minus 16, inside the shaded left half-plane.
Spectrum of the HiPPO-LegS $\statemat$ for $N=16$: the eigenvalues are the negative integers $-1, \dots, -16$, all real and in the open left half-plane (shaded). The integer spectrum is what gives HiPPO its graded multi-timescale memory — mode $n$ decays at rate $n+1$. Produced by companions/ch07/jax/hippo_matrix.py.

The integer spectrum has a memory interpretation: under ZOH the nn-th mode decays per step by e(n+1)Δe^{-(n+1)\stepsize}, 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 Δ\stepsize 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 A\statemat?” 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 A\statemat 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 P0,P1,P2,P3P_0, P_1, P_2, P_3 and verify by direct integration that 11P1(x)P3(x)dx=0\int_{-1}^{1} P_1(x) P_3(x)\, dx = 0. Then confirm 11P2(x)2dx=2/5\int_{-1}^1 P_2(x)^2\, dx = 2/5.

Solution

P0=1, P1=x, P2=12(3x21), P3=12(5x33x)P_0 = 1,\ P_1 = x,\ P_2 = \tfrac12(3x^2 - 1),\ P_3 = \tfrac12(5x^3 - 3x). The product P1P3=12(5x43x2)P_1 P_3 = \tfrac12(5x^4 - 3x^2) is even, and 11(5x43x2)dx=525323=22=0\int_{-1}^1 (5x^4 - 3x^2)\,dx = 5\cdot\tfrac{2}{5} - 3\cdot\tfrac{2}{3} = 2 - 2 = 0, so the integral is 00 — Legendre orthogonality. For the norm, 1114(3x21)2dx=1411(9x46x2+1)dx=14(925623+2)=14(1854+2)=1485=25\int_{-1}^1 \tfrac14(3x^2-1)^2 dx = \tfrac14\int_{-1}^1 (9x^4 - 6x^2 + 1)\,dx = \tfrac14(9\cdot\tfrac25 - 6\cdot\tfrac23 + 2) = \tfrac14(\tfrac{18}{5} - 4 + 2) = \tfrac14\cdot\tfrac{8}{5} = \tfrac{2}{5}, matching 2/(22+1)2/(2\cdot2+1).

Exercise 7.2 (computation)

Build the 4×44\times 4 HiPPO-LegS matrix A\statemat from the closed form of §7.3 by hand. Verify that A31=21\statemat_{31} = -\sqrt{21} and that the diagonal is (1,2,3,4)(-1, -2, -3, -4).

Solution

Entry (3,1)(3,1) has i=3>j=1i = 3 > j = 1, so A31=(23+1)(21+1)=73=214.583\statemat_{31} = -\sqrt{(2\cdot3+1)(2\cdot1+1)} = -\sqrt{7\cdot3} = -\sqrt{21} \approx -4.583. The diagonal entries are Aii=(i+1)\statemat_{ii} = -(i+1) for i=0,1,2,3i = 0,1,2,3, i.e. 1,2,3,4-1, -2, -3, -4. The full matrix is lower-triangular with A10=3, A20=5, A21=15, A30=7, A32=35\statemat_{10} = -\sqrt{3},\ \statemat_{20} = -\sqrt{5},\ \statemat_{21} = -\sqrt{15},\ \statemat_{30} = -\sqrt{7},\ \statemat_{32} = -\sqrt{35}, and zeros above the diagonal. The companion hippo_matrix.py prints exactly A31=4.582576\statemat_{31} = -4.582576.

Exercise 7.3 (computation + code)

Run companions/ch07/jax/hippo_reconstruction.py. Confirm that the relative L2L^2 reconstruction error of the two-sinusoid test signal falls sharply from N=4N=4 to N=16N=16, then plateaus. Why does it plateau rather than continue to zero?

Solution

The companion prints errors 0.80 (N=4), 0.39 (N=8), 0.011 (N=16), 0.0072 (N=32), 0.0072 (N=64)\approx 0.80\ (N{=}4),\ 0.39\ (N{=}8),\ 0.011\ (N{=}16),\ 0.0072\ (N{=}32),\ 0.0072\ (N{=}64). The sharp drop happens once NN 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 (L=1000L=1000), 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 A\statemat is lower-triangular, and that its diagonal entries are Aii=(i+1)\statemat_{ii} = -(i+1), 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 {gn}\{g_n\} is orthonormal in L2(ω)L^2(\omega) and cn=u,gnωc_n = \langle u, g_n\rangle_\omega, then ncngn\sum_n c_n g_n is the unique minimizer of uvω\lVert u - v\rVert_\omega over vspan{gn}v \in \operatorname{span}\{g_n\}.

Exercise 7.6 (theory) — solution in §7.10

Using lower-triangularity, prove that the eigenvalues of A\statemat are exactly {1,,N}\{-1, \dots, -N\}. Then show that ZOH discretization sends every eigenvalue strictly inside the unit disk for all Δ>0\stepsize > 0, and identify the per-step decay rate of the nn-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 PnP_{n} is expressed through Pn1P_{n-1} and Pn2P_{n-2} — never through higher-degree polynomials. When the projection coefficient cn(t)=u,P~nωtc_n(t) = \langle u, \tilde P_n\rangle_{\omega_t} is differentiated in tt, the time-derivative of the (scaled) measure couples cnc_n to the inner products of uu against P~m\tilde P_m — and the recurrence guarantees only mnm \le n appear. Hence ddtcn\ddt c_n depends on c0,,cnc_0, \dots, c_n but not on cn+1,c_{n+1}, \dots, which is exactly the statement that A\statemat has zero entries above the diagonal: Aij=0\statemat_{ij} = 0 for i<ji < j. For the diagonal, the “self-coupling” term of the nn-th coefficient under the scaled measure contributes (n+1)-(n+1) (the normalization 2n+1\sqrt{2n+1} and the measure’s 1/t1/t scaling combine to the integer n+1n+1); this is the Aii=(i+1)\statemat_{ii} = -(i+1) claim. The off-diagonal entries (2i+1)(2j+1)-\sqrt{(2i+1)(2j+1)} for i>ji>j follow from the same expansion carried through the normalization, given here without the full bookkeeping. \blacksquare

Solution to Exercise 7.5

Let p=ncngnp = \sum_n c_n g_n with cn=u,gnωc_n = \langle u, g_n\rangle_\omega, and let v=nangnv = \sum_n a_n g_n be any element of V=span{gn}V = \operatorname{span}\{g_n\}. First, the residual upu - p is orthogonal to every gmg_m:

up,gmω=u,gmωncngn,gmω=cmcm=0,\langle u - p, g_m\rangle_\omega = \langle u, g_m\rangle_\omega - \sum_n c_n \langle g_n, g_m\rangle_\omega = c_m - c_m = 0,

using orthonormality gn,gm=δnm\langle g_n, g_m\rangle = \delta_{nm}. Therefore upVu - p \perp V, and in particular up(pv)u - p \perp (p - v) since pvVp - v \in V. Now decompose and apply the Pythagorean theorem:

uvω2=(up)+(pv)ω2=upω2+pvω2upω2,\lVert u - v\rVert_\omega^2 = \lVert (u - p) + (p - v)\rVert_\omega^2 = \lVert u - p\rVert_\omega^2 + \lVert p - v\rVert_\omega^2 \ge \lVert u - p\rVert_\omega^2,

with equality iff pvω=0\lVert p - v\rVert_\omega = 0, i.e. v=pv = p. So pp is the unique minimizer. \blacksquare

Solution to Exercise 7.6

A lower-triangular matrix has its eigenvalues on the diagonal: det(AλI)\det(\statemat - \lambda I) is the product of diagonal entries i(Aiiλ)\prod_i (\statemat_{ii} - \lambda) because the determinant of a triangular matrix is the product of its diagonal, and subtracting λI\lambda I keeps it triangular. With Aii=(i+1)\statemat_{ii} = -(i+1) the roots are λi=(i+1)\lambda_i = -(i+1) for i=0,,N1i = 0,\dots,N-1, i.e. exactly {1,2,,N}\{-1, -2, \dots, -N\} — real, distinct, strictly negative.

Under ZOH the discrete dynamics matrix is Aˉ=eAΔ\discA = e^{\statemat\stepsize}, whose eigenvalues are μi=eλiΔ\mu_i = e^{\lambda_i \stepsize}. Then

μi=eλiΔ=eRe(λi)Δ=e(i+1)Δ<1for all Δ>0,\abs{\mu_i} = \abs{e^{\lambda_i \stepsize}} = e^{\operatorname{Re}(\lambda_i)\stepsize} = e^{-(i+1)\stepsize} < 1 \quad\text{for all } \stepsize > 0,

so every discrete eigenvalue lies strictly inside the unit disk (A-stability, Chapter 4). The per-step decay rate of mode nn is e(n+1)Δe^{-(n+1)\stepsize}: higher-index modes decay faster, giving the graded multi-timescale memory of §7.7. \blacksquare

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 A,B\statemat, \inputmat from the §7.3 closed form with a vectorized jnp.where (no Python loop, jit-fusable); emits hippo_matrix_structure.png and hippo_eigenvalues.png.
  • hippo_reconstruction.py — runs the time-varying LegS recurrence via jax.lax.scan and reconstructs the history; emits legendre_basis.png and hippo_reconstruction.png.
  • tests/test_hippo.py — pins the closed form, the integer spectrum, the reconstruction-error decay, and a lax.scan-vs-naive-loop equivalence guard.

Julia (companions/ch07/julia/):

  • hippo_legendre.jl — the same operator with stdlib LinearAlgebra only; the encoder is an explicit for loop (the contrast to lax.scan), and the Legendre basis is built from the three-term recurrence.
  • runtests.jl@testset assertions including an orthonormality Gram-matrix check.

PyTorch (companions/ch07/torch/):

  • hippo_operator.py — HiPPO-LegS wrapped as an nn.Module projection 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/.