zernike_evaluate#

pyzernike.zernike_evaluate(coefficients: Sequence[float] | ndarray, rho: ndarray, theta: ndarray, rho_derivative: Integral | Sequence[Integral] | ndarray | None = 0, theta_derivative: Integral | Sequence[Integral] | ndarray | None = 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 \(f = \sum_j c_j Z_j\) and its derivatives in polar coordinates.

\[\frac{\partial^{a+b} f}{\partial \rho^{a} \partial \theta^{b}}(\rho, \theta) = \sum_{j=0}^{P-1} c_j \frac{\partial^{a+b} Z_j}{\partial \rho^{a} \partial \theta^{b}}(\rho, \theta)\]

where \(Z_j\) is the Zernike polynomial of OSA/ANSI index \(j\) (see pyzernike.zernike_index_to_order()). 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. Coefficients in the Noll or Fringe indexing can be converted with pyzernike.zernike_coefficients_convert().

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 (unlike pyzernike.zernike_polynomial_up_to_order() followed by a sum).

Where \(\rho\) is outside \([0, 1]\), or \(\rho\) or \(\theta\) is not finite, the output is set to default.

Shape of the inputs and of the output. rho_derivative and theta_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 rho.

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

  • rho (numpy.ndarray) – The radial coordinates \(\rho\) with any shape (...).

  • theta (numpy.ndarray) – The angular coordinates \(\theta\) in radians, with the same shape as rho. If rho and theta are floating point arrays of the same dtype (e.g. numpy.float32), this dtype is kept. Otherwise, both are converted to numpy.float64.

  • rho_derivative (Optional[Union[Integral, Sequence[Integral], numpy.ndarray]], optional) – The order(s) \(a \geq 0\) of the derivative with respect to \(\rho\). Default is 0 (no derivative). None is accepted and means 0.

  • theta_derivative (Optional[Union[Integral, Sequence[Integral], numpy.ndarray]], optional) – The order(s) \(b \geq 0\) of the derivative with respect to \(\theta\). Default is 0 (no derivative). None is accepted and means 0.

  • default (Union[int, float], optional) – The value set where \(\rho\) is outside \([0, 1]\), or \(\rho\) or \(\theta\) is 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\) 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.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.

Returns:

  • If rho_derivative and theta_derivative are integers: an array with the shape of rho.

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

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

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

Return type:

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

Raises:
  • TypeError – If coefficients, rho or theta can not be converted to a floating point array, if rho_derivative or theta_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 rho and theta do not have the same shape, if the sequences do not have the same length, or if a derivative order is negative.

Examples

Evaluate a sum of defocus and coma on a polar grid:

import numpy
from pyzernike import zernike_evaluate, zernike_order_to_index

coefficients = numpy.zeros(10)  # all the polynomials up to the order 3
coefficients[zernike_order_to_index(n=[2, 3], m=[0, 1])] = [0.5, -0.2]

rho, theta = numpy.meshgrid(numpy.linspace(0, 1, 100), numpy.linspace(0, 2 * numpy.pi, 100))
W = zernike_evaluate(coefficients, rho, theta)  # array of shape (100, 100)

Compute its gradient in polar coordinates:

dW_drho, dW_dtheta = zernike_evaluate(coefficients, rho, theta, rho_derivative=[1, 0], theta_derivative=[0, 1])