{"record":{"id":"de8a08490490cb65","repo":"jax-ml/jax","slug":"when-lu-decomposition-matrix-and-b-different-numbe","errorCode":null,"errorMessage":"When LU decomposition matrix and b different numbers of dimensions, last axis of LU decomposition matrix (shape {lu.shape}) and second to last axis of b array (shape {b.shape}) must match","messagePattern":"When LU decomposition matrix and b different numbers of dimensions, last axis of LU decomposition matrix \\(shape (.+?)\\) and second to last axis of b array \\(shape (.+?)\\) must match","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/linalg.py","lineNumber":1848,"sourceCode":"    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\"\n                       .format(lu.shape, b.shape))\n\n  batch_shape = lax.broadcast_shapes(lu.shape[:-2], permutation.shape[:-1], b.shape[:-2])\n  lu = _broadcast_to(lu, (*batch_shape, *lu.shape[-2:]))\n  permutation = _broadcast_to(permutation, (*batch_shape, permutation.shape[-1]))\n  b = _broadcast_to(b, (*batch_shape, *b.shape[-2:]))\n  fn = _lu_solve_core\n  for _ in batch_shape:\n    fn = api.vmap(fn, in_axes=(0, 0, 0, None))\n  x = fn(lu, permutation, b, trans)\n  return x[..., 0] if rhs_vector else x\n\n# Support operation for LU decomposition: Transformation of the pivots returned\n# by LU decomposition into permutations.\n","sourceCodeStart":1830,"sourceCodeEnd":1866,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/linalg.py#L1830-L1866","documentation":"jax/_src/lax/linalg.py:1848 in _lu_solve (public lu_solve). When b is a (batched) matrix with the same ndim handling as lu (matrix RHS branch), its second-to-last axis must match the LU size: b.shape[-2] == lu.shape[-1] (i.e. b is [..., n, k]). Any other width raises this ValueError.","triggerScenarios":"Solving with multiple right-hand sides where b is (..., k, n) transposed instead of (..., n, k), or b built from a different-sized matrix; also mismatched batch broadcasting after reshaping.","commonSituations":"Transposed RHS convention differences when porting scipy.linalg.lu_solve / numpy linalg.solve code; batching where b was concatenated along the wrong axis; using a stale factorization with a new wider b.","solutions":["Transpose b so the solve dimension is -2: b = b.mT (or b.T for 2-d)","Re-factor and rebuild b together so shapes stay consistent","Assert b.shape[-2] == lu.shape[-1] before solving"],"exampleFix":"// before\nx = jax.lax.linalg.lu_solve(lu, piv, b)  # b: (k, n) but needs (n, k)\n// after\nx = jax.lax.linalg.lu_solve(lu, piv, b.mT)","handlingStrategy":"validation","validationCode":"assert b.shape[-2] == lu.shape[-1], (lu.shape, b.shape)\n# fix convention: b = b.mT if transposed","typeGuard":"def lu_matrix_rhs_ok(lu, b) -> bool:\n    return b.ndim >= 2 and b.shape[-2] == lu.shape[-1]","tryCatchPattern":null,"preventionTips":["RHS solve dim is -2; use .mT to fix transposed RHS","Keep factorization and RHS shapes in sync"],"tags":["jax","lu-solve","shape-validation","broadcasting"],"backgroundTag":"invalid-matrix-dimensions","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}