jax-ml/jax · error · ValueError

hankel: c must be at least 1-dimensional, got a scalar.

Error message

hankel: c must be at least 1-dimensional, got a scalar.

What it means

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.

Source

Thrown at jax/_src/scipy/linalg.py:2547

    >>> r = jnp.array([999, 4, 5, 6]) # Note r[0] is ignored
    >>> jax.scipy.linalg.hankel(c, r)
    Array([[1, 2, 3, 4],
           [2, 3, 4, 5],
           [3, 4, 5, 6]], dtype=int32)

    For N-dimensional ``c`` and/or ``r``, the result is a batch of Hankel matrices.
  """
  if r is None:
    check_arraylike("hankel", c)
    c = jnp.asarray(c)
    r = jnp.zeros_like(c)
  else:
    check_arraylike("hankel", c, r)
    c = jnp.asarray(c)
    r = jnp.asarray(r)
  if c.ndim == 0:
    raise ValueError("hankel: c must be at least 1-dimensional, got a scalar.")
  if r.ndim == 0:
    raise ValueError("hankel: r must be at least 1-dimensional, got a scalar.")

  # Align batch ranks so jnp.vectorize doesn't need implicit rank promotion.
  if c.ndim < r.ndim:
    c = lax.expand_dims(c, range(r.ndim - c.ndim))
  elif r.ndim < c.ndim:
    r = lax.expand_dims(r, range(c.ndim - r.ndim))

  return _hankel(c, r)

@partial(jnp_vectorize.vectorize, signature="(m),(n)->(m,n)")
def _hankel(c: Array, r: Array) -> Array:
  ncols, = c.shape
  nrows, = r.shape
  if ncols == 0 or nrows == 0:
    return jnp.empty((ncols, nrows), dtype=jnp.result_type(c, r))
  v = jnp.concatenate((c, r[1:]))

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Wrap scalars in a list/array: hankel([c_value], r)
  2. Check c.ndim >= 1 before calling
  3. Fix upstream reductions that should have kept a length-1 axis (keepdims=True)

Example fix

# before
H = hankel(x.sum())  # scalar
# after
H = hankel(x.sum(keepdims=True))  # shape (1,)
Defensive patterns

Strategy: validation

Validate before calling

c = jnp.asarray(c)
if c.ndim == 0:
    c = c.reshape(1)
H = hankel(c, r)

Type guard

def is_at_least_1d(x) -> bool:
    return jnp.asarray(x).ndim >= 1

Try / catch

try:
    hankel(c, r)
except ValueError as e:
    if 'must be at least 1-dimensional' in str(e):
        c = jnp.atleast_1d(c); hankel(c, r)
    else: raise

Prevention

When it happens

Trigger: Calling hankel(3), hankel(jnp.asarray(5)), or hankel(np.float32(1), r) with a 0-d c.

Common situations: 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.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/872ecd80ac05692f. Report an issue: GitHub.