jax-ml/jax · error · ValueError

Array shapes are not compatible for c @ Q operation: a has s

Error message

Array shapes are not compatible for c @ Q operation: a has shape {tuple(a.shape)} so Q has {m} rows, but c has {c.shape[-1]} columns (expected {m}).

What it means

In qr_multiply with mode='right', Q is m×m for a of shape (..., m, n), so c @ Q requires c.shape[-1] == m. If c's last dimension differs from m, a shape-mismatch ValueError is raised.

Source

Thrown at jax/_src/scipy/linalg.py:1135

  if mode not in ('right', 'left'):
    raise ValueError(f"mode must be 'right' or 'left', got {mode!r}")

  onedim = c.ndim == 1
  if onedim:
    c = c[:, None] if mode == 'left' else c[None, :]

  m, n = a.shape[-2:]
  k = min(m, n)

  if mode == 'left':
    if c.shape[-2] != k:
      raise ValueError(
          f"Array shapes are not compatible for Q @ c operation: "
          f"a has shape {tuple(a.shape)} so Q has {k} columns, "
          f"but c has {c.shape[-2]} rows (expected {k}).")
  else:
    if c.shape[-1] != m:
      raise ValueError(
          f"Array shapes are not compatible for c @ Q operation: "
          f"a has shape {tuple(a.shape)} so Q has {m} rows, "
          f"but c has {c.shape[-1]} columns (expected {m}).")

  batch = jnp.broadcast_shapes(a.shape[:-2], c.shape[:-2])
  a = jnp.broadcast_to(a, batch + a.shape[-2:])
  c = jnp.broadcast_to(c, batch + c.shape[-2:])

  p: Array | None = None
  if pivoting:
    jpvt = jnp.zeros(a.shape[:-2] + (n,), dtype=jnp.int32)
    r, p, taus = lax_linalg.geqp3(a, jpvt)
    p -= 1  # Convert geqp3's 1-based indices to 0-based indices by subtracting 1.
  else:
    r, taus = lax_linalg.geqrf(a)

  if m > n and mode == 'left':
    zeros = jnp.zeros(c.shape[:-2] + (m - k,) + c.shape[-1:], dtype=c.dtype)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pad or slice c so its last dimension equals m
  2. If you actually want Q_k (k columns) multiplication, use mode='left' with appropriately transposed/shaped c
  3. Double-check m = a.shape[-2] vs n = a.shape[-1] for wide inputs

Example fix

// before
a = jnp.ones((3, 5)); c = jnp.ones((2, 5))  # m=3, c.shape[-1]=5
q, r2 = jax.scipy.linalg.qr_multiply(a, c, mode='right')
// after
a = jnp.ones((3, 5)); c = jnp.ones((2, 3))  # last dim must equal m=3
q, r2 = jax.scipy.linalg.qr_multiply(a, c, mode='right')
Defensive patterns

Strategy: validation

Validate before calling

m = a.shape[-2]; assert mode != 'right' or c.shape[-1] == m, f'c.shape[-1] must be {m}'

Type guard

null

Prevention

When it happens

Trigger: Calling jax.scipy.linalg.qr_multiply(a, c, mode='right') where c's trailing dimension != a.shape[-2] (m) — e.g. c has n columns when m != n.

Common situations: Assuming Q is the thin/economic factor (n columns) instead of the full square one; porting code written for tall matrices to wide matrices (or vice versa).

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/d4effdb84cfaf1b8. Report an issue: GitHub.