Phase 21 of 25 · Topic 21.1

NumPy N-Dimensional Arrays (ndarray) & Memory Strides

1Concept

A NumPy `ndarray` is a contiguous block of homogeneous memory described by an element data type (`dtype`), dimensions (`shape`), and byte-stepping increments (`strides`). This contiguous layout enables hardware SIMD vectorization.

2Architecture Diagram

2D Array shape=(2, 3), dtype=int64:
Memory: [e00][e01][e02][e10][e11][e12] (Contiguous in RAM!)
Strides: (24, 8) ---> Jump 24 bytes to next row, 8 bytes to next column

3Code Example

Python 3.12
# Simulating NumPy ndarray stride architecture
shape = (2, 3)
dtype_size = 8 # 8 bytes for int64
row_stride = shape[1] * dtype_size
col_stride = dtype_size

print(f"Array Shape:   {shape}")
print(f"Memory Strides: ({row_stride}, {col_stride}) bytes")
print("C-Contiguous: Elements in a row are adjacent in memory.")

4Expected Output

Array Shape:   (2, 3)
Memory Strides: (24, 8) bytes
C-Contiguous: Elements in a row are adjacent in memory.

5Key Takeaways

  • C-contiguous arrays store rows contiguously; Fortran-contiguous arrays store columns contiguously.
  • Slicing an array creates a new View with updated strides; it does NOT copy underlying memory.
  • Views share memory with the original array; modifying a view mutates the original array.