jax.lax.polynomial

Contents

jax.lax.polynomial#

jax.lax.polynomial(x, coeffs, *, unroll=None)[source]#

Evaluates a polynomial using Horner’s rule:

\[P(x) = \sum_{k=0}^{N} a_k x^k = a_0 + x (a_1 + x (a_2 + \dots + x a_N))\]

The coefficients coeffs = (a_0, a_1, ..., a_N) are ordered from lowest degree (\(a_0\), the constant term) to highest degree (\(a_N\), the coefficient of \(x^N\)). For floating-point inputs, each Horner step is evaluated using jax.lax.fmuladd().

Derivatives with respect to x are computed as lower-degree polynomials (\(\sum_{k=1}^{N} k a_k x^{k-1}\)), making first- and higher-order differentiation cheaper than differentiating an open-coded Horner loop.

Parameters:
  • x (ArrayLike) – Input array at which to evaluate the polynomial.

  • coeffs (Sequence[ArrayLike]) – Sequence of polynomial coefficients (a_0, a_1, ..., a_N) from lowest to highest degree. Non-scalar coefficients must have the same rank as x (or broadcastable size-1 dimensions).

  • unroll (int | None) – Maximum number of terms to unroll directly in the lowered Horner recurrence before falling back to jax.lax.scan(). Defaults to None (which uses an unroll threshold of 8).

Returns:

An array with the broadcast shape and dtype of x and coeffs containing the evaluated polynomial.

Return type:

Array

Examples

Evaluate \(1 + 5x + 2x^2\) at \(x = 3.0\):

>>> from jax import lax
>>> lax.polynomial(3.0, [1.0, 5.0, 2.0])
Array(34., dtype=float32, weak_type=True)