Docs/Context
F-order vs C-order in Practice
NumPy supports two memory orderings, and FrankenNumPy preserves the contract:
- C-order (row-major,
order='C'): the rightmost dimension is contiguous. Stride for the last axis isitem_size; each axis to the left multiplies by the next axis's size. This is the default. - F-order (column-major,
order='F', "Fortran order"): the leftmost dimension is contiguous. Stride for the first axis isitem_size.
The choice matters in three concrete ways:
- Iteration speed. A reduction along the contiguous axis is dramatically faster than a reduction across a strided axis. For a C-order array,
sum(axis=-1)is the fast path; for an F-order array,sum(axis=0)is. - Interop with FORTRAN-style code. SciPy's LAPACK bindings, OpenBLAS, and most numerical-linear-algebra libraries expect F-order matrices. NPY files default to C-order;
numpy.asfortranarray(and FrankenNumPy's equivalent) makes a copy in F-order. reshapeambiguity.reshape(shape, order='C')walks elements in row-major order;reshape(shape, order='F')walks them column-major. The same shape can yield different element orderings.
SCE's contiguous_strides(shape, order) computes the correct stride vector for either order. The NdLayout type tracks which order an array currently has. A view created from a C-order array via transpose() becomes F-order at zero cost; the transpose is just a stride permutation.
The reduce-axis contiguity rule shows up directly in the performance picture: axis-0 reductions on F-order arrays hit the same contiguous fast-path as axis-(-1) reductions on C-order arrays, because in both cases the inner loop walks unit-stride memory.