{"record":{"id":"ea10767f14cb7021","repo":"jax-ml/jax","slug":"k-must-be-a-scalar-or-a-rank-1-array-of-length-1-o","errorCode":null,"errorMessage":"k must be a scalar or a rank-1 array of length 1 or m.","messagePattern":"k must be a scalar or a rank-1 array of length 1 or m\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":570,"sourceCode":"    ``m`` elements. The second order integration of the polynomial\n    :math:`12 x^2 + 12 x + 6` with the constants ``k=[4, 5]`` is\n    :math:`x^4 + 2 x^3 + 3 x^2 + 4 x + 5`:\n\n    >>> jnp.polyint(p, m=2, k=jnp.array([4, 5]))\n    Array([1., 2., 3., 4., 5.], dtype=float32)\n  \"\"\"\n  m = core.concrete_or_error(operator.index, m, \"'m' argument of jnp.polyint\")\n  k = 0 if k is None else k\n  p, k = ensure_arraylike(\"polyint\", p, k)\n  p_arr, k_arr = promote_dtypes_inexact(p, k)\n  del p, k\n  if m < 0:\n    raise ValueError(\"Order of integral must be positive (see polyder)\")\n  k_arr = atleast_1d(k_arr)\n  if len(k_arr) == 1:\n    k_arr = full((m,), k_arr[0])\n  if k_arr.shape != (m,):\n    raise ValueError(\"k must be a scalar or a rank-1 array of length 1 or m.\")\n  if m == 0:\n    return p_arr\n  else:\n    grid = (arange(len(p_arr) + m, dtype=p_arr.dtype)[np.newaxis]\n            - arange(m, dtype=p_arr.dtype)[:, np.newaxis])\n    coeff = maximum(1, grid).prod(0)[::-1]\n    return true_divide(concatenate((p_arr, k_arr)), coeff)\n\n\n@export\n@api.jit(static_argnames=('m',))\ndef polyder(p: ArrayLike, m: int = 1) -> Array:\n  r\"\"\"Returns the coefficients of the derivative of specified order of a polynomial.\n\n  JAX implementation of :func:`numpy.polyder`.\n\n  Args:\n    p: Array of polynomials coefficients.","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L552-L588","documentation":"The integration constants k in jnp.polyint must be broadcastable to exactly m values (one constant per integration step): a scalar, a single-element array, or an array of length exactly m.","triggerScenarios":"Calling jnp.polyint(p, m=2, k=jnp.array([1.0])) is fine, but k=jnp.array([1.0, 2.0, 3.0]) with m=2 fails; also k of shape (m+1,) or any length != 1 and != m.","commonSituations":"Hardcoding a list of constants then changing m; passing per-order constants array computed for a different integration order; refactoring numpy code where m changed.","solutions":["Pass a scalar k (broadcast to all orders)","Make len(k) == m exactly","Recompute k when m changes"],"exampleFix":"// before\njnp.polyint(p, m=3, k=jnp.array([1.0, 2.0]))\n// after\njnp.polyint(p, m=3, k=jnp.array([1.0, 2.0, 3.0]))","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\nk_arr = jnp.atleast_1d(jnp.asarray(k))\nif k_arr.size not in (1, m):\n    k_arr = jnp.full((m,), k_arr.ravel()[0])  # or raise\nresult = jnp.polyint(p, m=m, k=k_arr)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass scalar k unless per-order constants are truly needed","Keep k length synchronized with m in config"],"tags":["jax","polyint","shape-mismatch"],"backgroundTag":"argument-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}