jax-ml/jax · error · ValueError

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

Error message

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

What it means

In qr_multiply with mode='left', Q has shape (..., k, k) where k = min(m, n) for a of shape (..., m, n). The matrix c must have c.shape[-2] == k so Q @ c is well-defined; otherwise a shape-mismatch ValueError is raised.

Source

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

    >>> x = jax.scipy.linalg.solve_triangular(R, Qtb)
    >>> jnp.allclose(A.T @ A @ x, A.T @ b)
    Array(True, dtype=bool)
  """
  del overwrite_a, overwrite_c  # unused
  a, c = promote_dtypes_inexact(jnp.asarray(a), jnp.asarray(c))
  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)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Reshape/transpose c so its second-to-last dimension equals min(m, n)
  2. If you intended c @ Q semantics (c's last dim == m), use mode='right' instead
  3. Verify a.shape and c.shape interactively before jitting the call

Example fix

// before
a = jnp.ones((5, 3)); c = jnp.ones((5, 2))
q, r2 = jax.scipy.linalg.qr_multiply(a, c, mode='left')
// after
a = jnp.ones((5, 3)); c = jnp.ones((3, 2))  # k = min(5,3) = 3
q, r2 = jax.scipy.linalg.qr_multiply(a, c, mode='left')
Defensive patterns

Strategy: validation

Validate before calling

m, n = a.shape[-2:]; k = min(m, n); assert mode != 'left' or c.shape[-2] == k, f'c.shape[-2] must be {k}'

Type guard

null

Prevention

When it happens

Trigger: Calling jax.scipy.linalg.qr_multiply(a, c, mode='left') where a is (m, n), k = min(m, n), and c's leading matrix dimension (c.shape[-2]) differs from k — e.g. c has m rows when m != n.

Common situations: Porting NumPy least-squares pipelines where c was shaped for a full m×m Q; mixing up which side/mode implies which dimension of c.

Related errors


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