Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 40 ยท Arrays and NumPy API

Indexing, Slicing and Broadcasting

Master JAX array indexing: slices, negative indexes, fancy and boolean indexing, jnp.take, clamped reads, and NumPy broadcasting rules with practical examples.

Reading parts of arrays and combining arrays of different shapes are everyday tasks. JAX follows NumPy's rules closely, with a few differences that come from running on accelerators and inside jit. This lesson walks through slicing, fancy indexing, boolean masks, and broadcasting.

Runnable editors use NumPy, which has the same semantics for everything except the out-of-bounds rule that we point out explicitly (JAX itself cannot run in the in-browser runtime; see What Is JAX?).

Basic indexing and slicing

Python
import jax.numpy as jnp

m = jnp.arange(12).reshape(3, 4)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

m[1, 2]        # 6
m[0]           # first row
m[:, 1]        # second column
m[::2, ::2]    # every other row and column
m[-1, -1]      # 11
m[..., 0]      # first column using Ellipsis

Slices are start:stop:step, and the stop is exclusive. In NumPy a slice returns a view that shares memory; in JAX there are no views, since arrays are immutable and each slice is simply a new value (the compiler removes needless copies).

Python Slicing a matrix (NumPy semantics)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fancy (integer array) indexing

Use an integer array to pick elements in any order, with repeats allowed:

Python
v = jnp.array([10, 20, 30, 40, 50])
idx = jnp.array([4, 0, 0, 2])
v[idx]                  # [50 10 10 30]

m[jnp.array([0, 2]), jnp.array([1, 3])]   # elements (0,1) and (2,3) -> [1 11]

The shape of the result follows the shape of the index array. jnp.take(v, idx) does the same and also takes an axis, a mode, and a fill_value, which is handy for controlling out-of-bounds behavior.

Boolean masks

A boolean array the same shape as the data selects the True positions:

Python
x = jnp.array([3, -1, 4, -1, 5])
x[x > 0]                         # [3 4 5]  (works outside jit)
jnp.where(x > 0, x, 0)           # [3 0 4 0 5]  (works anywhere)

Selecting with a mask gives a result whose length depends on the data. JAX can do this in plain Python, but not inside jit, because compiled code needs fixed shapes. The jit-friendly alternative is jnp.where with a replacement value, a mask used in a sum ((x * (x > 0)).sum()), or jnp.where(mask, x, 0) followed by weighting.

Python Masks, where and fixed shapes (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Out-of-bounds reads

NumPy raises IndexError when an index is too large. JAX cannot raise from compiled code on an accelerator, so for reads it clamps the index into range, and for updates it drops out-of-range writes:

Python
v = jnp.array([10, 20, 30])
v[5]                 # 30 : clamped to the last element, no error
v[jnp.array([0, 7])] # [10 30]
v[-10]               # 10 : clamped to the first element
jnp.take(v, 5, mode="fill", fill_value=-1)   # -1

The stand-in below emulates this behavior with np.clip so you can see the difference from NumPy's error.

Python Stand-in: JAX-style clamped reads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Broadcasting

Broadcasting lets operations work on arrays of different shapes without copying data. The rule: line up the shapes from the right; two dimensions are compatible if they are equal or one of them is 1 (a missing dimension counts as 1). Dimensions of size 1 are "stretched".

flowchart LR
    A["A shape (3, 1)"] --> R["result (3, 4)"]
    B["B shape (4,) treated as (1, 4)"] --> R

A column of 3 and a row of 4 combine into a 3 by 4 table.

Python
col = jnp.array([[1], [2], [3]])      # (3, 1)
row = jnp.array([10, 20, 30, 40])     # (4,)
col + row                             # (3, 4)
# [[11 21 31 41]
#  [12 22 32 42]
#  [13 23 33 43]]

You can add a size-1 axis with None (an alias for np.newaxis) or reshape: v[:, None] turns shape (n,) into (n, 1). This is the tool for building pairwise tables, such as all distances between two sets of points, without loops. It also pairs naturally with vmap, the transformation that takes the idea of broadcasting over a batch and automates it.

Python Broadcasting: tables and pairwise distances (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last case fails because the trailing dimensions are 2 and 3, which are neither equal nor 1. The usual fix is to reshape so the dimensions line up: np.ones((3, 2)) + np.ones((3, 1)).

Rank promotion

JAX can warn or error when arrays of different rank (number of dimensions) are broadcast, because that is a common source of silent bugs. The setting jax_numpy_rank_promotion accepts "allow" (default), "warn" or "raise". Setting it to "raise" in a research codebase catches accidental shape mix-ups early.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the result shape of an array of shape (3, 1) plus an array of shape (4,)?

    Shapes align from the right: (3, 1) and (1, 4) broadcast to (3, 4).
  2. 2. In JAX, what does v[5] return for v = jnp.array([10, 20, 30])?

    Out-of-range reads are clamped to the nearest valid index because compiled code cannot raise exceptions.
  3. 3. Why does x[x > 0] not work under jit?

    Shapes must be known at compile time; use jnp.where or masked arithmetic instead.
  4. 4. What does v[:, None] do to a vector of shape (5,)?

    None inserts a new axis of size 1 at that position, here as the second dimension.

Summary

  • Slicing a[start:stop:step] and negative indexes work as in NumPy; there are no views in JAX.
  • Integer-array indexing and jnp.take pick arbitrary elements; masks select data-dependent lengths and do not work under jit.
  • Out-of-bounds reads clamp and writes drop; choose jnp.take(..., mode=) for control.
  • Broadcasting aligns shapes from the right; insert axes with None.
  • Use jax_numpy_rank_promotion="raise" to catch accidental broadcasting.

Next: Reductions, Linear Algebra and Math Functions