{"record":{"id":"f46949b04e4ab1fd","repo":"jax-ml/jax","slug":"jnp-linalg-cond-input-array-must-be-at-least-2d","errorCode":null,"errorMessage":"jnp.linalg.cond: input array must be at least 2D; got {arr.shape=}","messagePattern":"jnp\\.linalg\\.cond: input array must be at least 2D; got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/linalg.py","lineNumber":2291,"sourceCode":"  Examples:\n\n    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, /, *,","sourceCodeStart":2273,"sourceCodeEnd":2309,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/linalg.py#L2273-L2309","documentation":"jnp.linalg.cond computes the condition number, which is defined via singular values or matrix norms of a matrix (its last two axes). A 0-D or 1-D input has no matrix structure, so cond refuses it rather than returning something meaningless.","triggerScenarios":"jnp.linalg.cond(x) with x a scalar, vector, or 1-D array; passing a list of numbers that becomes shape (n,).","commonSituations":"Indexing a batch of matrices with a single index but landing on a vector (e.g. mats[i] on shape (B, N) storage); assuming cond of a vector means max/min abs ratio (it doesn't — that's a different computation).","solutions":["Ensure input has ndim >= 2; use jnp.atleast_2d(x) if a matrix was intended.","If you meant the ratio of largest to smallest magnitude element of a vector, compute jnp.max(jnp.abs(x)) / jnp.min(jnp.abs(x)) yourself.","Check the shape your data actually has (print x.shape) before calling."],"exampleFix":"// before\nc = jnp.linalg.cond(v)  # v: (n,)\n// after\nc = jnp.linalg.cond(jnp.atleast_2d(v))  # or diag/v2d as appropriate","handlingStrategy":"validation","validationCode":"x = jnp.asarray(x)\nif x.ndim < 2:\n    x = jnp.atleast_2d(x)\nc = jnp.linalg.cond(x)","typeGuard":"def is_at_least_2d(x) -> bool:\n    return getattr(x, 'ndim', 0) >= 2","tryCatchPattern":null,"preventionTips":["Verify .shape before conditioning checks","Watch indexing ops that drop a dim off batches"],"tags":["jax","numpy","linalg","condition-number","shape-validation"],"backgroundTag":"invalid-shape-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}