{"record":{"id":"0b58c8efc4f0efed","repo":"jax-ml/jax","slug":"the-input-a-must-be-at-least-a-2-d-array","errorCode":null,"errorMessage":"The input `a` must be at least a 2-D array.","messagePattern":"The input `a` must be at least a 2-D array\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2132,"sourceCode":"    P is positive-semidefinite Matrix:\n\n    >>> with jnp.printoptions(precision=2, suppress=True):\n    ...     print(P)\n    [[4.79 3.25 1.23]\n     [3.25 3.06 2.01]\n     [1.23 2.01 2.91]]\n\n    The original matrix can be reconstructed by multiplying the U and P:\n\n    >>> a_reconstructed = U @ P\n    >>> jnp.allclose(a, a_reconstructed)\n    Array(True, dtype=bool)\n\n  .. _QDWH: https://epubs.siam.org/doi/abs/10.1137/090774999\n  \"\"\"\n  arr = jnp.asarray(a)\n  if arr.ndim < 2:\n    raise ValueError(\"The input `a` must be at least a 2-D array.\")\n\n  if side not in [\"right\", \"left\"]:\n    raise ValueError(\"The argument `side` must be either 'right' or 'left'.\")\n\n  sig = \"(m,n)->(m,n),(n,n)\" if side == \"right\" else \"(m,n)->(m,n),(m,m)\"\n  return jnp_vectorize.vectorize(\n      partial(_polar_2d, side=side, method=method, eps=eps,\n              max_iterations=max_iterations),\n      signature=sig)(arr)\n\n\n@jit\ndef _sqrtm_triu(T: Array) -> Array:\n  \"\"\"\n  Implements Björck, Å., & Hammarling, S. (1983).\n      \"A Schur method for the square root of a matrix\". Linear algebra and\n      its applications\", 52, 127-140.\n  \"\"\"","sourceCodeStart":2114,"sourceCodeEnd":2150,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2114-L2150","documentation":"jax.scipy.linalg.polar computes the polar decomposition of matrices and requires input with at least 2 dimensions. Scalars, 1-D vectors, or 0/1-d arrays fail the ndim check and raise ValueError. The function applies a vectorized 2-D kernel with signature '(m,n)->...', so a matrix (possibly batched) is mandatory.","triggerScenarios":"Passing a Python number, a 0-d jnp array, or a 1-D array (e.g. a vector of shape (n,)) to polar().","commonSituations":"Feeding a flattened vector or an unwrapped scalar from an upstream pipeline; batched code where a leading axis was accidentally squeezed.","solutions":["Reshape to 2-D: a.reshape(-1, 1) or a[:, None] for column vectors","Check arr.ndim >= 2 before calling polar","Ensure upstream slicing keeps the matrix rank (avoid np.squeeze on all axes)"],"exampleFix":"# before\nU, H = polar(v)  # v shape (3,)\n# after\nU, H = polar(v.reshape(-1, 1))  # shape (3, 1)","handlingStrategy":"validation","validationCode":"a = jnp.asarray(a)\nif a.ndim < 2:\n    a = a.reshape(1, -1) if a.ndim == 1 else a.reshape(1, 1)\nU, H = jax.scipy.linalg.polar(a)","typeGuard":"def is_matrix_like(a) -> bool:\n    import jax.numpy as jnp\n    return jnp.asarray(a).ndim >= 2","tryCatchPattern":"try:\n    polar(a)\nexcept ValueError as e:\n    if 'must be at least a 2-D' in str(e):\n        a = jnp.asarray(a).reshape(-1, 1); polar(a)\n    else: raise","preventionTips":["Wrap matrix APIs in helpers that assert ndim >= 2","Avoid np.squeeze without an axis argument","Log shapes at pipeline boundaries during development"],"tags":["jax","polar-decomposition","input-validation","shape-error"],"backgroundTag":"expected-ndim-array","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}