jax-ml/jax · error · ValueError

N must be nonnegative

Error message

N must be nonnegative

What it means

Raised by jnp.vander when the explicitly passed number of columns N is negative, since a Vandermonde matrix cannot have a negative column count.

Source

Thrown at jax/_src/numpy/lax_numpy.py:8153

           [3, 1],
           [4, 1]], dtype=int32)

    Generates the Vandermonde matrix in increasing order of powers, when
    ``increasing=True``.

    >>> jnp.vander(x, increasing=True)
    Array([[ 1,  1,  1,  1],
           [ 1,  2,  4,  8],
           [ 1,  3,  9, 27],
           [ 1,  4, 16, 64]], dtype=int32)
  """
  x = util.ensure_arraylike("vander", x)
  if x.ndim != 1:
    raise ValueError("x must be a one-dimensional array")
  N = x.shape[0] if N is None else core.concrete_or_error(
    operator.index, N, "'N' argument of jnp.vander()")
  if N < 0:
    raise ValueError("N must be nonnegative")

  iota = lax.iota(x.dtype, N)
  if not increasing:
    iota = lax.sub(lax._const(iota, N - 1), iota)

  return ufuncs.power(x[..., None], expand_dims(iota, tuple(range(x.ndim))))


### Misc

@export
def argwhere(
    a: ArrayLike,
    *,
    size: int | None = None,
    fill_value: ArrayLike | None = None,
) -> Array:
  """Find the indices of nonzero array elements

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Clamp or validate N >= 0 before calling (max(N, 0) or an explicit check)
  2. Fix the formula computing N so it cannot be negative
  3. Omit N entirely to default to N = len(x)

Example fix

// before
jnp.vander(x, N=n - k)  # n - k may be negative
// after
N = max(n - k, 0)
jnp.vander(x, N=N)
Defensive patterns

Strategy: validation

Validate before calling

N = max(int(N), 0) if N is not None else len(x)

Prevention

When it happens

Trigger: jnp.vander(x, N=-3); N computed from a subtraction that can go negative (e.g. N = len(x) - k with k > len(x)).

Common situations: Sliding-window or polynomial-degree arithmetic producing negative N; passing a signed expression intended as an unsigned count.

Related errors


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