ToolJoltTools

NumPy Broadcasting Shape Checker

Check whether two tensor shapes broadcast together and get the result shape — with the rule applied step by step.

Result shape
Result elements

The default — (8,1,6,1) with (7,1,5) → (8,7,6,5) — is NumPy's own documentation example, deliberately weird to show that broadcasting aligns from the RIGHT, not the left.

Formula

Align shapes right; for each dim: sizes must be equal, or one of them 1 (stretched). Missing leading dims count as 1.
References: NumPy broadcasting documentation; PyTorch broadcasting semantics

About NumPy Broadcasting Shape Checker

Broadcasting is the most useful rule in array programming and the source of its most silent bugs: shapes that should clash instead 'work', producing a huge unintended result — the classic (n,) plus (n,1) accidentally making (n,n). This checker applies the exact NumPy/PyTorch algorithm: align shapes from the right, demand equal-or-one in every dimension, stretch the ones. It tells you both whether the shapes combine and what comes out, including the element count that warns you when a stray axis just exploded your memory.

How to use NumPy Broadcasting Shape Checker

  1. 1Enter your values into NumPy Broadcasting Shape Checker — sensible, domain-typical defaults are pre-filled so you see a real result immediately.
  2. 2The result recomputes live using the formula shown on the page; there is no button to press.
  3. 3Adjust any input to compare scenarios, then read the worked example to see the substituted numbers.

Why use NumPy Broadcasting Shape Checker?

  • Computes NumPy Broadcasting Shape Checker instantly in your browser — no sign-up, no upload, no server round-trip.
  • 100% free and unlimited, with the exact formula shown: Align shapes right; for each dim: sizes must be equal, or one of them 1 (stretched). Missing leading dims count as 1..
  • Runs entirely client-side, so every value you enter stays private on your device.
  • Live recompute as you type, with a worked example and authoritative references for trust.

Frequently asked questions

Why does (3,) + (3,1) give (3,3) instead of an error?+

Right-alignment: (3,) is treated as (1,3), and against (3,1) every dimension pairs a 1 with a 3 — legal, stretching both. You get a 3×3 outer-product-shaped result instead of the elementwise op you wanted. Fix: match dims explicitly with reshape/unsqueeze/keepdims=True.

Does broadcasting copy the stretched data?+

No — the size-1 axis gets stride 0, so the same memory is read repeatedly; no allocation happens for inputs. The OUTPUT is fully materialized though, which is where the (n,n) accident hurts: a million-element vector pair becomes a trillion-element result.

How do I broadcast on the LEFT instead?+

Insert explicit axes: a[:, None] (NumPy/PyTorch) adds a trailing-aligned dimension where you need it. There is no left-alignment mode — by design; explicit unsqueezing keeps intent visible. einops' rearrange makes the intent even more readable for complex cases.

Are matmul's batch dimensions broadcast too?+

Yes — for A @ B the last two dims follow matrix rules while all leading (batch) dims broadcast by exactly this checker's rule. That is how a (1, H, S, D) query attends against (B, H, D, S) keys without a tile/repeat in sight.

Related tools

Related ML & AI tools

Sponsored