Project documentation

Fixed-size C++23 state estimation with a thin Python interface.

LinearKalmanFilterBank — K filters sharing dynamics

A bank holds K independent linear Kalman filters that share F, Q, H and carry their own x, P, R. Internally it stores structure of arrays, k-inner: instead of K separate LinearKalmanFilter instances laid out as an array of structs, every per-symbol vector is interleaved so the K axis is the innermost dimension.

x_data[i * K + k]               // component i of filter k
P_data[(i * NX + j) * K + k]    // P_ij of filter k
R_data[(i * NZ + j) * K + k]    // R_ij of filter k

This is the right layout when you want the compiler to autovectorize across the K axis. predict() is then K independent F·P·Fᵀ + Q updates that the compiler is happy to SIMD over.

When to use it

Wrong tool when:

Construction

bank = kalman.LinearKalmanFilterBank(nx=2, nz=1, K=64)
bank.F = list(np.eye(2).flatten())          # shared (nx, nx)
bank.Q = list((1e-4 * np.eye(2)).flatten())
bank.H = [1.0, 0.0]                          # shared (nz, nx)

bank.x = x0.flatten()                        # (K, nx) flat, k-major
bank.P = P0.flatten()                        # (K, nx, nx) flat, k-major
bank.R = R0.flatten()                        # (K, nz, nz) flat, k-major

bank.nx, bank.nz, bank.K are read-only ints.

Per-tick API

bank.predict(delta_t=None)
bank.correct(z, eps=1e-15)           # Dyn only; templated uses update()
bank.update(z, eps=1e-15)            # Dyn only; templated uses update_many

z is shape (K, nz) flat. The templated bank doesn't expose correct on its own — call update_many with N=1 if you need a single batched step.

Batched API

result = bank.update_many(
    measurements,                   # (N, K, nz) flat
    delta_t=None,                   # (N,) for kinematic templated bank only
    measurement_variance=None,      # (N, K) for nz=1 templated, or (N, K, nz, nz)
    record_states=False,
    eps=1e-15,
)
result["x"]        # flat (K, nx)
result["states"]   # (N, K, nx) numpy array if record_states=True

On the templated bank delta_t requires nx == 2*nz or nx == 3*nz and applies a kinematic predict per tick. On the Dyn bank both kwargs raise ValueError.

Layout quick reference

Buffer Python view Internal C++ (SoA)
x (K, nx) flat, k-major x_data[i * K + k]
P (K, nx, nx) flat, k-major P_data[(i * NX + j) * K + k]
R (K, nz, nz) flat, k-major R_data[(i * NZ + j) * K + k]
F, Q, H shared, like LinearKalmanFilter one copy

The Python getters transpose AoS → SoA on read; the setters transpose back on write. If you want zero-copy access to the internal buffers, drop into C++.

NZ=1 fast path

When nz == 1 the correction loop reduces to a rank-1 update per filter:

v[i] = sum_j H[j] · P[i, j]     # per filter
S    = R + sum_i H[i] · v[i]    # per filter
K    = v / S
x   += K · (z − H · x)
P   -= K · vᵀ                   # rank-1 outer

Over the K axis this is one fused dot-product + one rank-1 outer, both of which the compiler turns into wide SIMD. Per-tick cost scales like O(NX² · K) with the K loop fully vectorized.

NZ > 1 path

The Dyn bank's NZ>1 correct falls back to a per-symbol classic Joseph-form update — every filter is corrected individually using the same DynLinearKF math the standalone filter uses. The templated bank for the kept pairs has a specialized full Joseph form that fans out across K.