{"record":{"id":"872ecd80ac05692f","repo":"jax-ml/jax","slug":"hankel-c-must-be-at-least-1-dimensional-got-a-sc","errorCode":null,"errorMessage":"hankel: c must be at least 1-dimensional, got a scalar.","messagePattern":"hankel: c must be at least 1-dimensional, got a scalar\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2547,"sourceCode":"\n    >>> r = jnp.array([999, 4, 5, 6]) # Note r[0] is ignored\n    >>> jax.scipy.linalg.hankel(c, r)\n    Array([[1, 2, 3, 4],\n           [2, 3, 4, 5],\n           [3, 4, 5, 6]], dtype=int32)\n\n    For N-dimensional ``c`` and/or ``r``, the result is a batch of Hankel matrices.\n  \"\"\"\n  if r is None:\n    check_arraylike(\"hankel\", c)\n    c = jnp.asarray(c)\n    r = jnp.zeros_like(c)\n  else:\n    check_arraylike(\"hankel\", c, r)\n    c = jnp.asarray(c)\n    r = jnp.asarray(r)\n  if c.ndim == 0:\n    raise ValueError(\"hankel: c must be at least 1-dimensional, got a scalar.\")\n  if r.ndim == 0:\n    raise ValueError(\"hankel: r must be at least 1-dimensional, got a scalar.\")\n\n  # Align batch ranks so jnp.vectorize doesn't need implicit rank promotion.\n  if c.ndim < r.ndim:\n    c = lax.expand_dims(c, range(r.ndim - c.ndim))\n  elif r.ndim < c.ndim:\n    r = lax.expand_dims(r, range(c.ndim - r.ndim))\n\n  return _hankel(c, r)\n\n@partial(jnp_vectorize.vectorize, signature=\"(m),(n)->(m,n)\")\ndef _hankel(c: Array, r: Array) -> Array:\n  ncols, = c.shape\n  nrows, = r.shape\n  if ncols == 0 or nrows == 0:\n    return jnp.empty((ncols, nrows), dtype=jnp.result_type(c, r))\n  v = jnp.concatenate((c, r[1:]))","sourceCodeStart":2529,"sourceCodeEnd":2565,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2529-L2565","documentation":"jax.scipy.linalg.hankel builds a Hankel matrix from a first column c and (optionally) last row r. Both must be at least 1-D; passing a Python scalar or 0-d array as c raises ValueError. The check runs after jnp.asarray, so numpy scalars are caught too.","triggerScenarios":"Calling hankel(3), hankel(jnp.asarray(5)), or hankel(np.float32(1), r) with a 0-d c.","commonSituations":"Default-parameter bugs where r=None creates zeros_like(c) and c is scalar; passing the result of a reduction (e.g. x.sum()) instead of a vector.","solutions":["Wrap scalars in a list/array: hankel([c_value], r)","Check c.ndim >= 1 before calling","Fix upstream reductions that should have kept a length-1 axis (keepdims=True)"],"exampleFix":"# before\nH = hankel(x.sum())  # scalar\n# after\nH = hankel(x.sum(keepdims=True))  # shape (1,)","handlingStrategy":"validation","validationCode":"c = jnp.asarray(c)\nif c.ndim == 0:\n    c = c.reshape(1)\nH = hankel(c, r)","typeGuard":"def is_at_least_1d(x) -> bool:\n    return jnp.asarray(x).ndim >= 1","tryCatchPattern":"try:\n    hankel(c, r)\nexcept ValueError as e:\n    if 'must be at least 1-dimensional' in str(e):\n        c = jnp.atleast_1d(c); hankel(c, r)\n    else: raise","preventionTips":["Apply jnp.atleast_1d to user inputs before matrix constructors","Use keepdims=True on reductions feeding structured-matrix APIs","Reject scalar inputs at your own API boundary with clearer messages"],"tags":["jax","hankel","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"}