Docs/Guarantees and recipes
SCE Invariants in Detail
On this page 5 sections
The Stride Calculus Engine enforces five hard contracts. fnp-ufunc, fnp-linalg, fnp-random and fnp-conformance build on them; fnp-python mostly reads NumPy's own shape / strides (about 450 attribute reads) and calls fnp-ndarray in a few places, so on the Python surface NumPy's layout rules are the ones in force.
Invariant 1: element-count conservation
For any shape s = [d0, d1, …, dn], the element count is prod(d_i) with checked multiplication. Overflow returns ShapeError::Overflow. This is the first check in any reshape, broadcast, or view-construction path: a shape that would address more elements than usize::MAX is rejected before any allocation is attempted.
element_count([2, 3, 4]) = 24
element_count([0, 5]) = 0 (zero is legal: empty axis)
element_count([usize::MAX, 2]) = Err(Overflow)
Invariant 2: stride-from-shape is deterministic
Given shape and MemoryOrder, contiguous_strides returns the unique stride vector for a contiguous layout. The function is a pure const fn-style computation: same inputs, same outputs, on every platform, regardless of compiler optimization level. C-order is the default; F-order is selected explicitly.
contiguous_strides([2, 3, 4], C) = [12, 4, 1] (rightmost = 1)
contiguous_strides([2, 3, 4], F) = [1, 2, 6] (leftmost = 1)
Invariant 3: broadcast legality is symmetric and associative
Two shapes broadcast-compatible iff, aligned from the right, every pair of dimensions is equal or one is 1. The output shape takes the max at each position. The relation is symmetric: broadcast_shape(a, b) == broadcast_shape(b, a). It is associative: broadcast_shapes([a, b, c]) reduces deterministically regardless of pairing order.
broadcast_shape([3, 1, 5], [4, 5]) = [3, 4, 5] (right-align, mixed-rank)
broadcast_shape([3, 4], [3, 5]) = Err(IncompatibleBroadcast)
broadcast_shape([], [3]) = [3] (0-D scalar broadcasts to anything)
Invariant 4: -1 inference has at most one degree of freedom
fix_unknown_dimension(shape, total_elements) accepts a shape with at most one -1 and infers it from the remaining dimensions. Two -1s return ShapeError::MultipleUnknownDimensions. A -1 that doesn't divide evenly returns ShapeError::IncompatibleElementCount.
fix_unknown_dim([-1, 4], 24) = [6, 4]
fix_unknown_dim([-1, 5], 24) = Err(IncompatibleElementCount { old: 24, new: 25 })
fix_unknown_dim([-1, -1], 24) = Err(MultipleUnknownDimensions)
Invariant 5: view byte-spans must fit in the backing allocation
as_strided(shape, strides, offset) and sliding_window_view(window_shape) compute the minimum and maximum reachable byte offset and verify the span fits in the buffer. A view that would address beyond the allocation is rejected (ShapeError::OutOfBoundsView), not silently zero-filled. Negative strides across multiple axes are handled by the same byte-span check.
These five invariants are why SCE is called the compatibility kernel of the Rust engine. The engine crates' shape handling rests on them, and the conformance gates verify them on every full CI run (G8 validates the FNP-P2C-001 shape/reshape and FNP-P2C-006 stride-tricks packets among the nine).