jax-ml/jax · error · ValueError

convolution_matrix: a must be at least 1-dimensional, got a

Error message

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

What it means

convolution_matrix requires the filter kernel a to be at least 1-D; a Python scalar or 0-d array fails the a_arr.ndim == 0 check. The matrix is built by vectorizing over the last axis of a, which a scalar lacks.

Source

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

  See also:
    :func:`jax.scipy.linalg.toeplitz`

  Examples:
    >>> jax.scipy.linalg.convolution_matrix(jnp.array([-1, 4, -2]), 5, mode='same')
    Array([[ 4, -1,  0,  0,  0],
           [-2,  4, -1,  0,  0],
           [ 0, -2,  4, -1,  0],
           [ 0,  0, -2,  4, -1],
           [ 0,  0,  0, -2,  4]], dtype=int32)
  """
  n = operator.index(n)
  if n <= 0:
    raise ValueError(f"n must be a positive integer; got {n}.")
  check_arraylike("convolution_matrix", a)
  a_arr = jnp.asarray(a)
  if a_arr.ndim == 0:
    raise ValueError(
        "convolution_matrix: a must be at least 1-dimensional, got a scalar.")
  m = a_arr.shape[-1]
  if m < 1:
    raise ValueError(f"len(a) must be at least 1; got shape {a_arr.shape}.")
  if mode not in ('full', 'valid', 'same'):
    raise ValueError(
        f"mode must be one of 'full', 'valid', 'same'; got {mode!r}.")
  pad_widths = [(0, 0)] * (a_arr.ndim - 1) + [(0, n - 1)]
  az = jnp.pad(a_arr, pad_widths)
  raz = jnp.pad(jnp.flip(a_arr, axis=-1), pad_widths)
  L = m + n - 1
  if mode == 'same':
    trim = min(n, m) - 1
    tb = trim // 2
    te = trim - tb
  elif mode == 'valid':
    tb = min(n, m) - 1
    te = tb

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Wrap in a list/array: convolution_matrix([2.0], n)
  2. Fix scalar-producing indexing: use a[i:i+1] instead of a[i]
  3. Validate a.ndim >= 1 in caller code

Example fix

# before
C = convolution_matrix(kernel[0], n)
# after
C = convolution_matrix(kernel[0:1], n)
Defensive patterns

Strategy: validation

Validate before calling

a_arr = jnp.asarray(a)
if a_arr.ndim == 0:
    a_arr = a_arr.reshape(1)
C = convolution_matrix(a_arr, n)

Type guard

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

Try / catch

try:
    convolution_matrix(a, n)
except ValueError as e:
    if 'at least 1-dimensional' in str(e):
        a = jnp.atleast_1d(a); convolution_matrix(a, n)
    else: raise

Prevention

When it happens

Trigger: Calling convolution_matrix(2.0, n) or convolution_matrix(jnp.asarray(3), n).

Common situations: Passing a single tap of a filter as a bare number; indexing that accidentally extractss a scalar (a[0] instead of a[0:1]).

Related errors


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