xarray.polyval#

xarray.polyval(coord, coeffs, degree_dim='degree')[source]#

Evaluate a polynomial at specific values.

Parameters:
  • coord (DataArray or Dataset) – Values at which to evaluate the polynomial.

  • coeffs (DataArray or Dataset) – Coefficients of the polynomial.

  • degree_dim (Hashable, default: "degree") – Name of the polynomial degree dimension in coeffs.

Returns:

DataArray or Dataset – Evaluated polynomial.

Examples

Evaluate the polynomial 1 + 2 * x + 3 * x**2:

>>> x = xr.DataArray(np.arange(5.0), dims="x")
>>> coeffs = xr.DataArray(
...     [1.0, 2.0, 3.0], dims="degree", coords={"degree": [0, 1, 2]}
... )
>>> xr.polyval(x, coeffs)
<xarray.DataArray (x: 5)> Size: 40B
array([ 1.,  6., 17., 34., 57.])
Dimensions without coordinates: x

Evaluate the result of DataArray.polyfit() along its coordinate:

>>> y = xr.DataArray(
...     [1.0, 6.0, 17.0, 34.0, 57.0], dims="x", coords={"x": np.arange(5.0)}
... )
>>> fit = y.polyfit("x", deg=2)
>>> xr.polyval(y.x, fit.polyfit_coefficients).round(6)
<xarray.DataArray (x: 5)> Size: 40B
array([ 1.,  6., 17., 34., 57.])
Coordinates:
  * x        (x) float64 40B 0.0 1.0 2.0 3.0 4.0