{"record":{"id":"4baf1f634ea4579e","repo":"jax-ml/jax","slug":"array-shapes-are-not-compatible-for-q-c-operatio","errorCode":null,"errorMessage":"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}).","messagePattern":"Array shapes are not compatible for Q @ c operation: a has shape (.+?) so Q has (.+?) columns, but c has (.+?) rows \\(expected (.+?)\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":1129,"sourceCode":"    >>> x = jax.scipy.linalg.solve_triangular(R, Qtb)\n    >>> jnp.allclose(A.T @ A @ x, A.T @ b)\n    Array(True, dtype=bool)\n  \"\"\"\n  del overwrite_a, overwrite_c  # unused\n  a, c = promote_dtypes_inexact(jnp.asarray(a), jnp.asarray(c))\n  if mode not in ('right', 'left'):\n    raise ValueError(f\"mode must be 'right' or 'left', got {mode!r}\")\n\n  onedim = c.ndim == 1\n  if onedim:\n    c = c[:, None] if mode == 'left' else c[None, :]\n\n  m, n = a.shape[-2:]\n  k = min(m, n)\n\n  if mode == 'left':\n    if c.shape[-2] != k:\n      raise ValueError(\n          f\"Array shapes are not compatible for Q @ c operation: \"\n          f\"a has shape {tuple(a.shape)} so Q has {k} columns, \"\n          f\"but c has {c.shape[-2]} rows (expected {k}).\")\n  else:\n    if c.shape[-1] != m:\n      raise ValueError(\n          f\"Array shapes are not compatible for c @ Q operation: \"\n          f\"a has shape {tuple(a.shape)} so Q has {m} rows, \"\n          f\"but c has {c.shape[-1]} columns (expected {m}).\")\n\n  batch = jnp.broadcast_shapes(a.shape[:-2], c.shape[:-2])\n  a = jnp.broadcast_to(a, batch + a.shape[-2:])\n  c = jnp.broadcast_to(c, batch + c.shape[-2:])\n\n  p: Array | None = None\n  if pivoting:\n    jpvt = jnp.zeros(a.shape[:-2] + (n,), dtype=jnp.int32)\n    r, p, taus = lax_linalg.geqp3(a, jpvt)","sourceCodeStart":1111,"sourceCodeEnd":1147,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L1111-L1147","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reshape/transpose c so its second-to-last dimension equals min(m, n)","If you intended c @ Q semantics (c's last dim == m), use mode='right' instead","Verify a.shape and c.shape interactively before jitting the call"],"exampleFix":"// before\na = jnp.ones((5, 3)); c = jnp.ones((5, 2))\nq, r2 = jax.scipy.linalg.qr_multiply(a, c, mode='left')\n// after\na = jnp.ones((5, 3)); c = jnp.ones((3, 2))  # k = min(5,3) = 3\nq, r2 = jax.scipy.linalg.qr_multiply(a, c, mode='left')","handlingStrategy":"validation","validationCode":"m, n = a.shape[-2:]; k = min(m, n); assert mode != 'left' or c.shape[-2] == k, f'c.shape[-2] must be {k}'","typeGuard":"null","tryCatchPattern":null,"preventionTips":["Compute k = min(m, n) and check c's rows against it before calling","Document which mode implies which dimension of c in wrapper functions"],"tags":["jax","linalg","shape-mismatch","qr-decomposition"],"backgroundTag":"matrix-dimension-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}