{"record":{"id":"c961ec2099eb666d","repo":"jax-ml/jax","slug":"jnp-linalg-cond-input-array-must-not-be-empty-go","errorCode":null,"errorMessage":"jnp.linalg.cond: input array must not be empty; got {arr.shape=}","messagePattern":"jnp\\.linalg\\.cond: input array must not be empty; got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/linalg.py","lineNumber":2293,"sourceCode":"    Well-conditioned matrix:\n\n    >>> x = jnp.array([[1, 2],\n    ...                [2, 1]])\n    >>> jnp.linalg.cond(x)\n    Array(3., dtype=float32)\n\n    Ill-conditioned matrix:\n\n    >>> x = jnp.array([[1, 2],\n    ...                [0, 0]])\n    >>> jnp.linalg.cond(x)\n    Array(inf, dtype=float32)\n  \"\"\"\n  arr = ensure_arraylike(\"cond\", x)\n  if arr.ndim < 2:\n    raise ValueError(f\"jnp.linalg.cond: input array must be at least 2D; got {arr.shape=}\")\n  if arr.shape[-1] == 0 or arr.shape[-2] == 0:\n    raise ValueError(f\"jnp.linalg.cond: input array must not be empty; got {arr.shape=}\")\n  if p is None or p == 2:\n    s = svdvals(x)\n    return s[..., 0] / s[..., -1]\n  elif p == -2:\n    s = svdvals(x)\n    r = s[..., -1] / s[..., 0]\n  else:\n    if arr.shape[-2] != arr.shape[-1]:\n      raise ValueError(f\"jnp.linalg.cond: for {p=}, array must be square; got {arr.shape=}\")\n    r = norm(x, ord=p, axis=(-2, -1)) * norm(inv(x), ord=p, axis=(-2, -1))\n  # Convert NaNs to infs where original array has no NaNs.\n  return jnp.where(ufuncs.isnan(r) & ~ufuncs.isnan(x).any(axis=(-2, -1)), np.inf, r)\n\n\n@export\ndef trace(x: ArrayLike, /, *,\n          offset: int = 0, dtype: DTypeLike | None = None) -> Array:\n  \"\"\"Compute the trace of a matrix.","sourceCodeStart":2275,"sourceCodeEnd":2311,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/linalg.py#L2275-L2311","documentation":"For an (at least 2-D) matrix whose last or second-to-last dimension is 0, the condition number is undefined (svd of an empty matrix yields no singular values to ratio). jnp.linalg.cond explicitly rejects empty matrices with this check instead of producing NaN or crashing in SVD.","triggerScenarios":"jnp.linalg.cond(jnp.zeros((0, 3))) or jnp.zeros((3, 0)); slicing a batch with an empty selection then calling cond on the result.","commonSituations":"Empty batches after filtering/masking; code that worked on non-empty data hitting a degenerate edge case at runtime (e.g. zero valid rows).","solutions":["Filter out or skip empty matrices before calling cond.","Check arr.shape[-1] and arr.shape[-2] are non-zero beforehand.","Guard with a jnp.where on batch size if operating on variable-size batches."],"exampleFix":"// before\nc = jnp.linalg.cond(mats[valid])  # valid may be all-False -> shape (0, n, n)\n// after\nif valid.sum() > 0:\n    c = jnp.linalg.cond(mats[valid])\nelse:\n    c = jnp.full((), jnp.nan)","handlingStrategy":"validation","validationCode":"if arr.shape[-1] == 0 or arr.shape[-2] == 0:\n    c = jnp.nan\nelse:\n    c = jnp.linalg.cond(arr)","typeGuard":"def nonempty_matrix(x) -> bool:\n    return x.ndim >= 2 and x.shape[-1] > 0 and x.shape[-2] > 0","tryCatchPattern":null,"preventionTips":["Skip empty selections before cond","Guard variable-size batches for zero rows"],"tags":["jax","linalg","condition-number","empty-array","edge-case"],"backgroundTag":"empty-array-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}