jax-ml/jax · error · ValueError

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

Error message

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

What it means

The companion check to error 3390: the optional last-row argument r to hankel must also be at least 1-D. Since hankel(c, r) requires r explicitly to reach this branch, the ValueError fires only for explicitly passed scalar r.

Source

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

    >>> 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:]))
  return lax.conv_general_dilated_patches(
      v.reshape((1, ncols + nrows - 1, 1)),

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass r as a 1-D array: hankel(c, [r0, r1, ...])
  2. Use r=None (omitted) to let r default to zeros_like(c)
  3. Validate r.ndim >= 1 when r is user-supplied

Example fix

# before
H = hankel(c, 5)
# after
H = hankel(c, [5, 0, 0])
Defensive patterns

Strategy: validation

Validate before calling

if r is not None:
    r = jnp.asarray(r)
    if r.ndim == 0:
        r = r.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 'r must be at least 1-dimensional' in str(e):
        r = jnp.atleast_1d(r); hankel(c, r)
    else: raise

Prevention

When it happens

Trigger: Calling hankel(c, 0) or hankel(c, jnp.asarray(2)) with a valid vector c but 0-d r.

Common situations: Passing a scalar 'fill' value for r by analogy with other APIs (e.g. toeplitz-style defaults); variables collapsed to scalars by earlier computation.

Related errors


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