opticks.utils.math_utils

Classes

InterpolatorWithUnitTypes(*values)

Enumerator for the interpolator types available.

InterpolatorWithUnits(ipol[, x_unit, y_unit])

Interpolator with units.

PPolyWithUnits(c, x[, extrapolate, axis, ...])

Subclass of PPoly, adding units functionality.

class InterpolatorWithUnitTypes(*values)[source]

Enumerator for the interpolator types available.

CUBICHERMITESPL = <class 'scipy.interpolate._cubic.CubicHermiteSpline'>

Cubic Hermite Spline Interpolator.

CUBICSPL = <class 'scipy.interpolate._cubic.CubicSpline'>

Cubic Spline Interpolator.

PCHIP = <class 'scipy.interpolate._cubic.PchipInterpolator'>

PCHIP Interpolator (monotonic).

class InterpolatorWithUnits(ipol, x_unit=None, y_unit=None)[source]

Interpolator with units.

For most usecases, the from_ipol_method factory is more convenient than this constructor.

Parameters:
  • ppoly (PPoly) – Interpolator subclassing PPoly

  • x_unit (UnitBase or FunctionUnitBase, optional) – unit of the x axis, by default None

  • y_unit (UnitBase or FunctionUnitBase, optional) – unit of the y axis, by default None

  • ipol (PPoly)

axis
extrapolate
classmethod from_ipol_method(ipol_type, x, y, *args, axis=0, extrapolate=None, **kwargs)[source]

Generates and interpolator with units.

The interpolator is chosen with the enum InterpolatorWithUnitTypes, among scipy cubic and monotonic interpolators. Interpolator specific parameters can be passed via *args, **kwargs, with the exception of the method parameter in Akima1DInterpolator. Use the enum MAKIMA for the Modified Akima instead.

Return type:

InterpolatorWithUnits

Parameters:
  • ipol_type (InterpolatorWithUnitTypes) – Interpolator type

  • x (array_like, shape (n,)) – 1-D array containing values of the independent variable. Values must be real, finite and in strictly increasing order.

  • y (array_like) – Array containing values of the dependent variable. It can have arbitrary number of dimensions, but the length along axis (see below) must match the length of x. Values must be finite.

  • axis (int, optional) – Axis along which y is assumed to be varying. Meaning that for x[i] the corresponding values are np.take(y, i, axis=axis). Default is 0.

  • extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. If None (default), it is set to True.

Returns:

InterpolatorWithUnits – Interpolator with units

class PPolyWithUnits(c, x, extrapolate=None, axis=0, x_unit=None, y_unit=None)[source]

Subclass of PPoly, adding units functionality.

The cubic and monotone interpolators in scipy all derive from PPoly, therefore this is equivalent to an interpolator with units. The interpolator is initialised elsewhere and its coefficients are used to initialise this object.

Piecewise polynomial in terms of coefficients and breakpoints

The polynomial between x[i] and x[i + 1] is written in the local power basis:

S = sum(c[m, i] * (xp - x[i])**(k-m) for m in range(k+1))

where k is the degree of the polynomial.

Parameters:
  • c (ndarray, shape (k, m, )) – Polynomial coefficients, order k and m intervals.

  • x (ndarray, shape (m+1,)) – Polynomial breakpoints. Must be sorted in either increasing or decreasing order.

  • extrapolate (bool or 'periodic', optional) – If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. Default is True.

  • axis (int, optional) – Interpolation axis. Default is zero.

  • x_unit (UnitBase or FunctionUnitBase, optional) – unit of the x axis, by default None

  • y_unit (UnitBase or FunctionUnitBase, optional) – unit of the y axis, by default None

antiderivative(nu=1)[source]

Construct a new piecewise polynomial representing the antiderivative.

Antiderivative is also the indefinite integral of the function, and derivative is its inverse operation.

Return type:

PPolyWithUnits

Parameters:

nu (int, optional) – Order of antiderivative to evaluate. Default is 1, i.e., compute the first integral. If negative, the derivative is returned.

Returns:

pp (PPoly) – Piecewise polynomial of order k2 = k + n representing the antiderivative of this polynomial.

Notes

The antiderivative returned by this function is continuous and continuously differentiable to order n-1, up to floating point rounding error.

If antiderivative is computed and self.extrapolate='periodic', it will be set to False for the returned instance. This is done because the antiderivative is no longer periodic and its correct evaluation outside of the initially given x interval is difficult.

axis
derivative(nu=1)[source]

Construct a new piecewise polynomial representing the derivative.

Return type:

PPolyWithUnits

Parameters:

nu (int, optional) – Order of derivative to evaluate. Default is 1, i.e., compute the first derivative. If negative, the antiderivative is returned.

Returns:

pp (PPoly) – Piecewise polynomial of order k2 = k - n representing the derivative of this polynomial.

Notes

Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open, [a, b), except for the last interval which is closed [a, b].

extrapolate
classmethod from_ppoly(ppoly, x_unit=None, y_unit=None)[source]

Upgrade an existing PPoly object (or from a subclass) with units.

Reinitialises a new object by shallow copying the coefficients, breakpoints and other properties.

Return type:

PPolyWithUnits

Parameters:
  • ppoly (PPoly) – PPoly object (or from a subclass)

  • x_unit (UnitBase or FunctionUnitBase, optional) – unit of the x axis, by default None

  • y_unit (UnitBase or FunctionUnitBase, optional) – unit of the y axis, by default None

Returns:

PPolyWithUnits – PPoly object with units

integrate(a, b, extrapolate=None, equivalencies=None)[source]

Compute a definite integral over a piecewise polynomial.

Parameters:
  • a (float) – Lower integration bound

  • b (float) – Upper integration bound

  • extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. If None (default), use self.extrapolate.

Returns:

ig (array_like) – Definite integral of the piecewise polynomial over [a, b]

roots(discontinuity=True, extrapolate=None)[source]

Find real roots of the piecewise polynomial.

Return type:

ndarray

Parameters:
  • discontinuity (bool, optional) – Whether to report sign changes across discontinuities at breakpoints as roots.

  • extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to return roots from the polynomial extrapolated based on first and last intervals, ‘periodic’ works the same as False. If None (default), use self.extrapolate.

Returns:

roots (ndarray) – Roots of the polynomial(s).

If the PPoly object describes multiple polynomials, the return value is an object array whose each element is an ndarray containing the roots.

See also

PPoly.solve

solve(y, discontinuity=True, extrapolate=None, equivalencies=None)[source]

Find real solutions of the equation pp(x) == y.

Return type:

ndarray

Parameters:
  • y (float, optional) – Right-hand side. Default is zero.

  • discontinuity (bool, optional) – Whether to report sign changes across discontinuities at breakpoints as roots.

  • extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to return roots from the polynomial extrapolated based on first and last intervals, ‘periodic’ works the same as False. If None (default), use self.extrapolate.

Returns:

roots (ndarray) – Roots of the polynomial(s).

If the PPoly object describes multiple polynomials, the return value is an object array whose each element is an ndarray containing the roots.

Notes

This routine works only on real-valued polynomials.

If the piecewise polynomial contains sections that are identically zero, the root list will contain the start point of the corresponding interval, followed by a nan value.

If the polynomial is discontinuous across a breakpoint, and there is a sign change across the breakpoint, this is reported if the discont parameter is True.

Examples

Finding roots of [x**2 - 1, (x - 1)**2] defined on intervals [-2, 1], [1, 2]:

>>> import numpy as np
>>> from scipy.interpolate import PPoly
>>> pp = PPoly(np.array([[1, -4, 3], [1, 0, 0]]).T, [-2, 1, 2])
>>> pp.solve()
array([-1.,  1.])