xy_zernike_evaluate#

pyzernike.xy_zernike_evaluate(coefficients: Sequence[float] | ndarray, x: ndarray, y: ndarray, 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[ndarray][source]#

Evaluate a sum of Zernike polynomials on an elliptic annulus \(f = \sum_j c_j Zxy_j\) and its derivatives with respect to the Cartesian coordinates \(x\) and \(y\).

../_images/elliptic_annulus_domain.png

The parameters of the elliptic annulus domain.#

\[\frac{\partial^{a+b} f}{\partial x^{a} \partial y^{b}}(x, y) = \sum_{j=0}^{P-1} c_j \frac{\partial^{a+b} Zxy_j}{\partial x^{a} \partial y^{b}}(x, y)\]

where \(Zxy_j\) is the Zernike polynomial of OSA/ANSI index \(j\) on the elliptic annulus (see pyzernike.xy_zernike_polynomial()). The derivatives of any order are exact, also at the center of the ellipse. The coefficients must contain all the polynomials up to a maximum radial order \(N\), so their number must be \(P = \frac{(N+1)(N+2)}{2}\) (1, 3, 6, 10, 15, …): pad them with zeros if needed.

The polynomials are computed by chunks of points, so that the \(P\) arrays of the polynomials are never stored for all the points at once.

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):

  • If both are integers, the output is an array with the shape of x.

  • If at least one of them is a sequence, the integer is repeated to the length \(D\) of the sequence (the sequences must have the same length), and the output is a list of \(D\) arrays with the shape of x (to_list=True), or a single array of shape \((D, \dots)\) (to_list=False).

See also

Parameters:
  • coefficients (Union[Sequence[float], numpy.ndarray]) – The coefficients \(c_j\) in the OSA/ANSI ordering, as a 1D array of \(P = \frac{(N+1)(N+2)}{2}\) finite real numbers.

  • 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.

  • 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 and the cosine and sine terms are cached and shared between the polynomials. Default is True.

  • use_recursive (bool, optional) – If True, the polynomials are computed with the recurrence relation (recommended for high orders, see pyzernike.xy_zernike_polynomial_up_to_order()). Default is False.

  • normalize (bool, optional) – If True, the coefficients are those of the polynomials normalized by the factors of Noll \(N_{n}^{m} = \sqrt{(2 - \delta_{m0})(n + 1)}\) (see pyzernike.zernike_normalization()). Default is False.

  • to_list (bool, optional) – Only used when several derivatives are computed. If True, the output is a list of arrays. If False, the output is a single array of shape \((D, \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:

  • If x_derivative and y_derivative are integers: an array with the shape of x.

  • Otherwise, if to_list is True: a list of \(D\) arrays with the shape of x.

  • Otherwise: an array of shape \((D, \dots)\), where output[d] is the derivative (x_derivative[d], y_derivative[d]).

The dtype is the common floating point dtype of x and y (numpy.float64 by default).

Return type:

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

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

  • ValueError – If coefficients is not a 1D array of finite values whose length is \(\frac{(N+1)(N+2)}{2}\), if x and y do not have the same shape, 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.

Examples

Evaluate a wavefront and its slopes on an elliptic pupil:

import numpy
from pyzernike import xy_zernike_evaluate

coefficients = numpy.zeros(15)  # all the polynomials up to the order 4
coefficients[[4, 5, 8]] = [0.3, -0.15, 0.1]  # Z_2^0, Z_2^2 and Z_3^1

pupil = dict(Rx=1.0, Ry=0.7, alpha=numpy.deg2rad(30.0))
X, Y = numpy.meshgrid(numpy.linspace(-1, 1, 200), numpy.linspace(-1, 1, 200))
W = xy_zernike_evaluate(coefficients, X, Y, **pupil)
dW_dx, dW_dy = xy_zernike_evaluate(coefficients, X, Y, x_derivative=[1, 0], y_derivative=[0, 1], **pupil)