pyzernike.xy_zernike_polynomial_up_to_order#

pyzernike.xy_zernike_polynomial_up_to_order(x: ndarray, y: ndarray, order: Integral, x_derivative: Integral | Sequence[Integral] | ndarray | None = 0, y_derivative: Integral | Sequence[Integral] | ndarray | None = 0, *, Rx: Real | None = 1.0, Ry: Real | None = 1.0, x0: Real | None = 0.0, y0: Real | None = 0.0, alpha: Real | None = 0.0, h: Real | None = 0.0, default: int | float = nan, precompute: bool = True, use_recursive: bool = False, normalize: bool = False, to_list: bool = True) → ndarray | List[List[ndarray]][source]#

Compute all the Zernike polynomials up to a radial order \(N\) on an elliptic annulus for Cartesian coordinates \((x, y)\), and their derivatives of any order with respect to \(x\) and \(y\).

../_images/elliptic_annulus_domain.png

The parameters of the elliptic annulus domain.#

The Zernike polynomials are extended to an elliptic annulus [1] by evaluating them at the equivalent polar coordinates \((\rho_{eq}, \theta_{eq})\) of the point (see pyzernike.cartesian_to_elliptic_annulus()). All the \(P = \frac{(N+1)(N+2)}{2}\) polynomials with \(0 \leq n \leq N\) are computed, in the OSA/ANSI ordering: the polynomial \(Z_{n}^{m}\) is at the index \(j = \frac{n(n+2) + m}{2}\) (see pyzernike.zernike_index_to_order()).

The derivatives with respect to \(u = X / R_x\) and \(v = Y / R_y\) are built symbolically with exact coefficients, without the chain rule in \((\rho_{eq}, \theta_{eq})\) (see pyzernike.xy_zernike_polynomial()). Any derivative order is available, and the derivatives are exact at the center of the ellipse. Two methods are available to apply these coefficients:

  • use_recursive=False: the explicit sums with exact integer coefficients, evaluated with the Horner scheme. The cancellation between the large alternating terms of the sums limits the accuracy for high orders (the error grows quickly beyond \(n \approx 20\)).

  • use_recursive=True: the coefficients are applied to values computed with the recurrence relation of Shakibaei and Paramesran [2]. For \(h = 0\), the derivatives are linear combinations of Zernike polynomials of lower orders [3]; for \(h > 0\), they are combinations of the radial derivatives \(\frac{d^a R_{n}^{|m|}}{d\rho^a}(\rho_{eq})\). The accuracy stays close to the machine precision at high orders. This method is recommended for high orders.

Both methods are described in the sections Cartesian derivatives and Recursive mode of the page Extension to an elliptic annulus.

Where the point is outside the elliptic annulus (\(\rho_{eq}\) outside \([0, 1]\)), or the coordinates are not finite, the output is set to default.

Shape of the inputs and of the output. x_derivative and y_derivative can each be a single integer or a sequence of integers (list, tuple or 1D numpy array). The integer is repeated to the length \(D\) of the sequence (the sequences must have the same length), and each pair (x_derivative[d], y_derivative[d]) gives one set of \(P\) polynomials. The output always has a first dimension of length \(D\) (\(D = 1\) if both are integers):

  • to_list=True: a list of \(D\) lists of \(P\) arrays with the shape of x.

  • to_list=False: a single array of shape \((D, P, \dots)\).

See also

Parameters:
  • x (numpy.ndarray) – The x Cartesian coordinates with any shape (...).

  • y (numpy.ndarray) – The y Cartesian coordinates with the same shape as x. If x and y are floating point arrays of the same dtype (e.g. numpy.float32), this dtype is kept. Otherwise, both are converted to numpy.float64.

  • order (Integral) – The maximum radial order \(N \geq 0\).

  • x_derivative (Optional[Union[Integral, Sequence[Integral], numpy.ndarray]], optional) – The non-negative order(s) of the derivative with respect to \(x\). Default is 0 (no derivative).

  • y_derivative (Optional[Union[Integral, Sequence[Integral], numpy.ndarray]], optional) – The non-negative order(s) of the derivative with respect to \(y\). Default is 0 (no derivative).

  • Rx (Optional[Real], optional) – The semi-axis \(R_x\) of the ellipse along its x axis. Must be finite and strictly positive. Default is 1.0.

  • Ry (Optional[Real], optional) – The semi-axis \(R_y\) of the ellipse along its y axis. Must be finite and strictly positive. Default is 1.0.

  • x0 (Optional[Real], optional) – The x coordinate \(x_0\) of the center of the ellipse. Must be finite. Default is 0.0.

  • y0 (Optional[Real], optional) – The y coordinate \(y_0\) of the center of the ellipse. Must be finite. Default is 0.0.

  • alpha (Optional[Real], optional) – The rotation angle \(\alpha\) of the ellipse in radians. Must be finite. Default is 0.0.

  • h (Optional[Real], optional) – The ratio \(h\) of the inner to the outer semi-axes, in \([0, 1)\). Default is 0.0 (full ellipse).

  • default (Union[int, float], optional) – The value set outside the elliptic annulus or where the coordinates are not finite. Integers and floating point numbers (Python or numpy, including numpy.nan and numpy.inf) are accepted and converted to float. Booleans are not accepted. Default is numpy.nan.

  • precompute (bool, optional) – Only used if use_recursive is False. If True, the powers of \(\rho_{eq}\) and \(1/r\) and the cosine and sine terms are cached and shared between the polynomials. If False, they are recomputed for each polynomial, which uses less memory. Default is True.

  • use_recursive (bool, optional) – If True, the polynomials and their derivatives are computed with the recurrence relation instead of the explicit sums (accurate at high orders). Default is False.

  • normalize (bool, optional) – If True, the polynomials (and their derivatives) are multiplied by the normalization factors of Noll \(N_{n}^{m} = \sqrt{(2 - \delta_{m0})(n + 1)}\) (see pyzernike.zernike_normalization()). For \(h = 0\), the normalized polynomials are orthonormal on the ellipse for the inner product averaged over its area. The value default is not normalized. Default is False.

  • to_list (bool, optional) – If True, the output is a list of lists of arrays. If False, the output is a single array of shape \((D, P, \dots)\). Default is True.

  • orders (None is accepted for all the parameters of the ellipse and for the derivative)

  • value. (and means the default)

Returns:

The polynomials, where output[d][j] (to_list=True) or output[d, j] (to_list=False) is the polynomial of OSA/ANSI index j for the pair (x_derivative[d], y_derivative[d]), with the shape of x. The dtype is the common floating point dtype of x and y (numpy.float64 by default).

Return type:

Union[List[List[numpy.ndarray]], numpy.ndarray]

Raises:
  • TypeError – If x or y can not be converted to a floating point array, if order is not an integer, if a parameter of the ellipse is not a real number, if default is not an integer or a floating point number, if x_derivative or y_derivative is not an integer or a sequence of integers, or if precompute, use_recursive, normalize or to_list is not a boolean.

  • ValueError – If x and y do not have the same shape, if order is negative, if a parameter of the ellipse is not finite, if Rx or Ry is not strictly positive, if h is not in \([0, 1)\), if the sequences do not have the same length, or if a derivative order is negative.

References

Examples

Compute all the Zernike polynomials up to the order 3 on a circle of radius \(\sqrt{2}\) centered at the origin:

import numpy
from pyzernike import xy_zernike_polynomial_up_to_order, zernike_index_to_order

X, Y = numpy.meshgrid(numpy.linspace(-1, 1, 100), numpy.linspace(-1, 1, 100))
polynomials = xy_zernike_polynomial_up_to_order(X, Y, order=3, Rx=numpy.sqrt(2), Ry=numpy.sqrt(2))[0]  # list of 10 arrays

n, m = zernike_index_to_order(range(len(polynomials)))
for n_j, m_j, Z in zip(n, m, polynomials):
    print(f"Z_{n_j}^{m_j}: max = {numpy.nanmax(Z):.3f}")

Compute all the polynomials up to the order 10, their gradient and their Hessian matrix as a single array:

Z = xy_zernike_polynomial_up_to_order(
    X, Y, order=10, Rx=numpy.sqrt(2), Ry=numpy.sqrt(2),
    x_derivative=[0, 1, 0, 2, 0, 1], y_derivative=[0, 0, 1, 0, 2, 1], to_list=False,
)
# Z.shape == (6, 66, 100, 100)

Compute all the polynomials up to the order 40 and their gradient with the recurrence relation:

Z = xy_zernike_polynomial_up_to_order(
    X, Y, order=40, Rx=numpy.sqrt(2), Ry=numpy.sqrt(2),
    x_derivative=[0, 1, 0], y_derivative=[0, 0, 1], use_recursive=True, to_list=False,
)
# Z.shape == (3, 861, 100, 100)