pyzernike.zernike_polynomial_up_to_order#

pyzernike.zernike_polynomial_up_to_order(rho: ndarray, theta: ndarray, order: Integral, 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[List[ndarray]][source]#

Compute all the Zernike polynomials \(Z_{n}^{m}(\rho, \theta)\) up to a radial order \(N\), and their derivatives.

The Zernike polynomials [1] are defined on the unit disk for \(|m| \leq n\) and \(n - m\) even by:

\[\begin{split}Z_{n}^{m}(\rho, \theta) = \begin{cases} R_{n}^{m}(\rho) \cos(m \theta) & \text{if } m \geq 0 \\ R_{n}^{|m|}(\rho) \sin(|m| \theta) & \text{if } m < 0 \end{cases}\end{split}\]

and their derivatives are given in pyzernike.zernike_polynomial(). 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()).

Two methods are available to compute the radial polynomials:

  • use_recursive=False: the explicit sum with exact integer coefficients, evaluated with the Horner scheme. The cost is \(O(N^3)\) operations per point, and the cancellation between the large alternating terms of the sum limits the accuracy for high orders (the absolute error in double precision grows quickly beyond \(n \approx 20\)).

  • use_recursive=True: the three-term recurrence relation of Shakibaei and Paramesran [2]. The cost is \(O(N^2)\) operations per point, and the accuracy stays close to the machine precision at any order. This method is recommended for high orders.

Both methods are described in the page Computation of the polynomials.

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). The integer is repeated to the length \(D\) of the sequence (the sequences must have the same length), and each pair (rho_derivative[d], theta_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 rho.

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

See also

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

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

  • 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. If False, they are recomputed for each polynomial, which uses less memory. Default is True.

  • use_recursive (bool, optional) – If True, the radial polynomials are computed with the recurrence relation instead of the explicit sum (faster and more accurate for 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)}\), so that they are orthonormal on the unit disk (see pyzernike.zernike_normalization()). 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.

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 (rho_derivative[d], theta_derivative[d]), with the shape of rho. The dtype is the common floating point dtype of rho and theta (numpy.float64 by default).

Return type:

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

Raises:
  • TypeError – If rho or theta can not be converted to a floating point array, if order is not an integer, 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 rho and theta do not have the same shape, if order is negative, 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 polar grid:

import numpy
from pyzernike import zernike_polynomial_up_to_order, zernike_index_to_order

rho, theta = numpy.meshgrid(numpy.linspace(0, 1, 100), numpy.linspace(0, 2 * numpy.pi, 100))
polynomials = zernike_polynomial_up_to_order(rho, theta, order=3)[0]  # list of 10 arrays of shape (100, 100)

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 = {Z.max():.3f}")

Compute all the polynomials up to the order 40 and their gradient in polar coordinates, as a single array, with the recurrence relation:

Z = zernike_polynomial_up_to_order(rho, theta, order=40, rho_derivative=[0, 1, 0], theta_derivative=[0, 0, 1], use_recursive=True, to_list=False)
# Z.shape == (3, 861, 100, 100)