Mathematical Prerequisites for Mamba
Building the math foundations you need for Mamba — first-order linear ODEs, the integrating factor method, the matrix exponential, the integral identity used in zero-order hold, the ZOH discretization itself, the unrolled time-varying recurrence, and the 1-semiseparable matrix view — all derived step by step with one consistent leaky-integrator example.
The next post in this series derives Mamba and Mamba-2 from first principles. Both architectures start from a continuous-time differential equation, discretize it, and run the discretized version as a recurrence. To follow that derivation, we need seven mathematical tools that have not appeared in any earlier prerequisite post.
The most important is the zero-order hold (ZOH) discretization formula
which converts the continuous SSM into the discrete recurrence . The Mamba paper writes these formulas down in two lines, but every symbol in them earns its place through a chain of derivations: the integrating factor solves the linear ODE, the matrix exponential generalizes to vector-valued states, an integral identity collapses the convolution integral when the input is held constant, and the substitution rule glues the pieces together. We will build that chain end to end. We will also derive the 1-semiseparable matrix view that turns the unrolled recurrence into a single matrix–vector product — the bridge that the second Mamba paper uses to expose the duality between SSMs and attention.
By the end of this post, you will have everything you need to read the boxed results in Mamba and Mamba-2 as derivations rather than as definitions.
The Running Example
A single bucket with a hole in the bottom and a tap above it. Let be the water level at time . The hole drains the bucket at a rate proportional to how full it is, and the tap pours water in at a rate . With both rate constants set to 1, this gives:
We turn the tap on at and leave it running at constant rate . By the end of Section 2 we will have derived the closed-form solution
and every numerical check in this post will land back on this curve. At this gives — those four numbers are the ground truth we will reproduce from many different angles.
In the matrix-valued sections, becomes the matrix , becomes , and the scalar formulas drop out as the one-dimensional special case.
1. First-Order Linear Ordinary Differential Equation
A differential equation is an equation that relates a function to its own derivatives. The simplest interesting example is one that says “the rate of change of depends linearly on itself, plus an external driving term”:
This is a first-order linear ordinary differential equation (ODE). “First-order” because only the first derivative appears. “Linear” because enters only to the first power, with no or . “Ordinary” because there is only one independent variable, . The coefficient is the decay rate, the coefficient is the input gain, and is the forcing term.
For our running example, , , and . The equation becomes
which says: if the bucket is empty, water rises at rate 1 (the tap dominates); if the bucket is full at , the inflow exactly cancels the outflow and the level stops changing. The fixed point is , and we expect to climb from 0 toward 1.
We do not yet know what looks like as a function. Section 2 derives the closed form.
2. The Integrating Factor Method
We want to solve
The left-hand side does not look like the derivative of anything obvious. The integrating factor method is a trick that fixes this: multiply both sides by a carefully chosen factor that turns the LHS into a single derivative.
The chosen factor is . Multiply through:
Now look at the LHS. By the product rule,
which is exactly the LHS. The equation has collapsed to
Integrate both sides from to . By the fundamental theorem of calculus, the LHS integrates to :
Multiply both sides by :
This is the variation-of-constants formula for a first-order linear ODE. The first term is the unforced response — what the bucket would do with the tap off, decaying from its starting level. The second term is the forced response — the cumulative effect of every drop that fell into the bucket, each one weighted by how much it has decayed by time .
Numerical check
Plug in , , , :
At : . At : . These are the four ground-truth numbers we promised. ✓
Why this matters
The boxed formula is the closed-form solution to every linear SSM with constant coefficients. In Mamba, is a vector and is a matrix , but the structure of the formula is unchanged. We will need a way to make sense of "" when is a matrix — that is the next section.
3. The Matrix Exponential
The scalar exponential is defined by the power series
This power series uses only addition, multiplication, and scalar division — operations that are perfectly well-defined when is replaced by a matrix . The matrix exponential is defined the same way:
Three properties that we will use repeatedly.
Scalar special case. When is the matrix , every power , and the series reduces term-for-term to . The matrix and scalar exponentials agree on matrices.
Diagonal case. When is diagonal, every power is diagonal too, and the series gives
This is why Mamba parameterizes as diagonal: every matrix exponential reduces to independent scalar exponentials, one per state channel.
Derivative rule. Differentiating the power series term by term,
So . The matrix exponential commutes with , just as commutes with in the scalar case (trivially).
Numerical check
Take . The diagonal-case formula gives . At , . The derivative rule gives , which equals at . ✓
Why this matters
The integrating factor derivation in Section 2 used three facts about : the product rule, the fact that , and the fact that we can multiply by it on both sides. All three carry over verbatim to the matrix case because of the derivative rule above. So the boxed solution from Section 2 generalizes:
with the same derivation, swapped for , swapped for , and now vector-valued. We will use this in Section 5.
4. The Integral Identity for Matrix Exponentials
When the input is held constant over the integration window, we can pull it out of the integral and the remaining piece is
We claim this equals whenever is invertible.
Derivation by term-by-term integration. Substitute the power series and integrate each term:
Pull a factor of out of every term (note that , so the first term — where — becomes , exactly what we want):
where we relabeled . The sum starting from is missing only the term, which is . So it equals . Putting it together:
Numerical check
Take , .
LHS: .
RHS: . ✓
Why this matters
This is the single algebraic identity that turns the convolution integral in Section 3’s solution into a closed-form coefficient. The next section uses it.
5. Zero-Order Hold Discretization
A continuous SSM evolves at every instant, but a neural network only sees the input at discrete sample times . We need a rule that converts the continuous equation into a discrete recurrence
that produces the exact trajectory at the sample points, given some assumption about what the input does between samples.
Zero-order hold is the simplest possible assumption: between consecutive samples, the input is held constant at the value it had at the start of the interval. So for all . This is the staircase reconstruction: each new sample sets a new constant level until the next sample arrives.
Apply the matrix solution from Section 3 over the interval , with starting state :
Under ZOH, is constant in , so comes out of the integral:
Substitute in the integral. When , ; when , ; and :
By Section 4, this equals . Substituting back:
Reading off the coefficients of and gives the discrete parameters:
Numerical check
With , , :
The discrete recurrence is . With and for all :
The continuous solution at was . The discrete values match the continuous values at every sample point, off only in the third decimal due to rounding to . ✓
A useful identity in the scalar case
When and , the formula simplifies further. Compute . So and sum to 1, and the update
is a convex combination of the previous state and the current input. This is exactly the form of an exponential moving average — the same form a classical RNN gate uses. The Mamba post (Section 1.5) leans on this identity to argue that selective SSMs are equivalent to gated RNNs in the scalar case.
Why this matters
These two boxed formulas are the connection between continuous control theory and the discrete recurrence that runs on a GPU. Every selective SSM in Mamba is exactly this recurrence, with , , and made to depend on the input.
6. Unrolling a Time-Varying Recurrence
In Section 5, and are constants. In Mamba, the selection mechanism makes , , and depend on the input , which in turn makes and vary across time steps. The recurrence becomes
To express as a closed-form function of the inputs , we unroll the recurrence step by step. Starting from :
The pattern is now visible: each input contributes to multiplied by (the gain at the time the input entered) and by the cumulative product of every that came after, capturing how much that contribution has decayed by time :
with the empty-product convention (so the term is just , with no decay applied).
Numerical check
Apply the formula with the running example, where and for all (the constant-coefficient case), , :
The unrolled sum reproduces the step-by-step recurrence value from Section 5.
Why this matters
This formula is the matrix-attention bridge. The next section reads it as a single matrix–vector product and exposes its structure.
7. The 1-Semiseparable Matrix View
Stack the unrolled values into a column vector , and the inputs into a column vector . The formula from Section 6 says with
The matrix is lower-triangular by construction, because depends only on inputs at times (causality). Factoring out the values:
The matrix is the heart of the structure. A lower-triangular matrix is called 1-semiseparable (1-SS) if every entry in its lower triangle factors as a product
for some sequences and . Equivalently, every submatrix contained entirely in the lower triangle has rank at most 1. The cumulative-product form above is exactly this structure: define and , and then
So the unrolled SSM recurrence is multiplication by a 1-semiseparable matrix. We have shown:
Numerical check
Take the running example with constant and , and . The matrix has on the diagonal (empty product), and below the diagonal the entries are powers of :
(Using , .) Multiplying by scales every column by , giving . Then with :
Same four numbers as Section 5 and Section 6. ✓ Three different computations — recurrent, unrolled-sum, and matrix-multiply — all produce the same trajectory, because they are three algorithms for evaluating the same function.
Why this matters
This is the key insight of the second Mamba paper. The recurrent algorithm and the matrix-multiply algorithm compute the same output, so we can choose between them based on hardware. Recurrent is time and memory but inherently sequential. Matrix-multiply is time but uses the GPU’s matrix units. The structured state space duality (SSD) splits the sequence into chunks and uses matrix-multiply within each chunk, then connects chunks with the recurrence — getting the best of both. Every step of that algorithm starts from the boxed identity above.
Summary
All seven tools were built from a single bucket with a hole, governed by with the tap held at .
The first-order linear ODE is the continuous-time skeleton of every state space model. The integrating factor method — multiply by , recognize the LHS as , integrate, solve — gave the closed-form solution , verified at the four sample points . The matrix exponential generalizes verbatim, reduces to a per-channel scalar exponential when is diagonal (which is why Mamba parameterizes as diagonal), and satisfies the same derivative rule, which lets the integrating factor derivation extend to the matrix case unchanged. The integral identity — derived by term-by-term integration of the power series — collapses the convolution integral whenever the input is held constant. Zero-order hold discretization combines the matrix solution with the integral identity: holding constant on pulls out of the integral and applies the identity to what remains, yielding the boxed pair , . Unrolling the time-varying recurrence produced the closed form , where each input is weighted by a cumulative product of subsequent decay factors. Finally, the 1-semiseparable matrix view recognized this sum as with — a structured lower-triangular matrix whose every lower-triangular submatrix has rank at most 1. The recurrence and the matrix-multiply are two algorithms for the same function, which is the foundation of structured state space duality.
With these tools in hand, we are ready for Mamba and Mamba-2: Selective State Spaces and Structured State Space Duality, where the boxed ZOH formulas are taken as the starting point, the selection mechanism makes , , and input-dependent, and the 1-semiseparable mask becomes the structured replacement for the causal attention mask.
Previous: The Kernel Zoo: Performers, Fast Weight Programmers, and the Capacity-Approximation Tradeoff in Linear Attention Next: Mamba and Mamba-2: Selective State Spaces and Structured State Space Duality
Enjoyed this post?
Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.