{"record":{"id":"9f037d2a14fd3af7","repo":"jax-ml/jax","slug":"b-matrix-must-have-rank-1-got-shape-b-shape","errorCode":null,"errorMessage":"b matrix must have rank >= 1, got shape {b.shape}","messagePattern":"b matrix must have rank >= 1, got shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/linalg.py","lineNumber":1833,"sourceCode":"    conj = trans == 2\n    x = triangular_solve(lu, x, left_side=True, lower=False, transpose_a=True,\n                         conjugate_a=conj)\n    x = triangular_solve(lu, x, left_side=True, lower=True, unit_diagonal=True,\n                         transpose_a=True, conjugate_a=conj)\n    _, ind = lax.sort_key_val(permutation, lax.iota('int32', permutation.shape[0]))\n    x = x[ind, :]\n  else:\n    raise ValueError(f\"'trans' value must be 0, 1, or 2, got {trans}\")\n  return lax.reshape(x, b.shape)\n\n\n@api.jit(static_argnums=(3,))\ndef _lu_solve(lu: Array, permutation: Array, b: Array, trans: int) -> Array:\n  if len(lu.shape) < 2 or lu.shape[-1] != lu.shape[-2]:\n    raise ValueError(\"last two dimensions of LU decomposition must be equal, \"\n                     \"got shape {}\".format(lu.shape))\n  if len(b.shape) < 1:\n    raise ValueError(\"b matrix must have rank >= 1, got shape {}\"\n                     .format(b.shape))\n  # Broadcasting follows NumPy's convention for linalg.solve: the RHS is\n  # treated as a (batched) vector if the number of dimensions differ by 1.\n  # Otherwise, broadcasting rules apply.\n  rhs_vector = lu.ndim == b.ndim + 1\n  if rhs_vector:\n    if b.shape[-1] != lu.shape[-1]:\n      raise ValueError(\"When LU decomposition matrix and b have the same \"\n                       \"number of dimensions, last axis of LU decomposition \"\n                       \"matrix (shape {}) and b array (shape {}) must match\"\n                       .format(lu.shape, b.shape))\n    b = b[..., np.newaxis]\n  else:\n    if b.shape[-2] != lu.shape[-1]:\n      raise ValueError(\"When LU decomposition matrix and b different \"\n                       \"numbers of dimensions, last axis of LU decomposition \"\n                       \"matrix (shape {}) and second to last axis of b array \"\n                       \"(shape {}) must match\"","sourceCodeStart":1815,"sourceCodeEnd":1851,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/linalg.py#L1815-L1851","documentation":"jax/_src/lax/linalg.py:1833 in _lu_solve (public lu_solve). The right-hand side b must be at least rank 1 (a vector). A 0-d scalar b has no axis to solve along, so the ValueError fires immediately before broadcasting logic runs.","triggerScenarios":"Calling jax.lax.linalg.lu_solve(lu, permutation, b) with b = jnp.scalar or a Python float, e.g. accidentally reducing b with .sum() or indexing b[i] to a scalar before passing.","commonSituations":"Loop-refactor bugs where b[i] should be b[i:i+1]; aggressive squeezing (jnp.squeeze) collapsing a (1,) RHS to 0-d; mixing scalar coefficients with matrix solves in solvers for ODE roots.","solutions":["Keep b at least 1-d: b = jnp.atleast_1d(b) or b = b[None]","Replace accidental reductions (e.g. use keepdims=True on sums feeding b)","Add an assert b.ndim >= 1 during development"],"exampleFix":"// before\nx = jax.lax.linalg.lu_solve(lu, piv, b)  # b is 0-d scalar\n// after\nx = jax.lax.linalg.lu_solve(lu, piv, jnp.atleast_1d(b))","handlingStrategy":"validation","validationCode":"b = jnp.atleast_1d(b)","typeGuard":"def valid_rhs(b: jax.Array) -> bool:\n    return b.ndim >= 1","tryCatchPattern":null,"preventionTips":["Beware jnp.squeeze collapsing (1,) RHS to scalar","Use keepdims=True on reductions feeding b"],"tags":["jax","lu-solve","shape-validation","rank"],"backgroundTag":"invalid-matrix-dimensions","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}