pyzernike.zernike_polynomial#

pyzernike.zernike_polynomial(rho: ndarray, theta: ndarray, n: Integral | Sequence[Integral] | ndarray, m: Integral | Sequence[Integral] | ndarray, rho_derivative: Integral | Sequence[Integral] | ndarray | None = 0, theta_derivative: Integral | Sequence[Integral] | ndarray | None = 0, default: int | float = nan, *, precompute: bool = True, normalize: bool = False, to_list: bool = True) → ndarray | List[ndarray][source]#

Compute the Zernike polynomials \(Z_{n}^{m}(\rho, \theta)\) 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}\]

where \(R_{n}^{m}\) is the radial polynomial (see pyzernike.radial_polynomial()). The polynomials are not normalized by default: \(Z_{n}^{m}(1, \theta)\) has an amplitude of 1. With normalize=True, they are multiplied by the normalization factors of Noll [2] \(\sqrt{2(n+1)}\) (\(m \neq 0\)) or \(\sqrt{n+1}\) (\(m = 0\)), and are then orthonormal on the unit disk.

The derivative of order \(a\) with respect to \(\rho\) and \(b\) with respect to \(\theta\) is:

\[\begin{split}\frac{\partial^{a+b} Z_{n}^{m}}{\partial \rho^{a} \partial \theta^{b}} = \begin{cases} \frac{d^a R_{n}^{m}}{d\rho^a}(\rho) \, m^{b} \cos\left(m\theta + b\frac{\pi}{2}\right) & \text{if } m > 0 \\ \frac{d^a R_{n}^{0}}{d\rho^a}(\rho) \, \delta_{b,0} & \text{if } m = 0 \\ \frac{d^a R_{n}^{|m|}}{d\rho^a}(\rho) \, |m|^{b} \sin\left(|m|\theta + b\frac{\pi}{2}\right) & \text{if } m < 0 \end{cases}\end{split}\]

The coefficients of the radial part are computed exactly with integer arithmetic, and the sum is evaluated with the Horner scheme (see the page Computation of the polynomials).

  • If \(|m| > n\) or \(n - m\) is odd, the polynomial is zero.

  • If \(a > n\), the derivative is zero.

  • 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. n, m, rho_derivative and theta_derivative can each be a single integer or a sequence of integers (list, tuple or 1D numpy array):

  • If all of them are integers, a single polynomial is computed and returned as an array with the shape of rho.

  • If at least one of them is a sequence, the integers are repeated to the length \(N\) of the sequences (which must all have the same length), and \(N\) polynomials are computed. They are returned as a list of \(N\) arrays with the shape of rho (to_list=True), or as a single array of shape \((N, \dots)\) (to_list=False).

Note

The explicit sum of the radial part suffers from cancellation between its large alternating terms for high orders. In double precision, the absolute error stays below \(10^{-10}\) up to \(n \approx 20\) for \(m = 0\), and grows quickly beyond. For high orders, use pyzernike.zernike_polynomial_up_to_order() with use_recursive=True.

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.

  • n (Union[Integral, Sequence[Integral], numpy.ndarray]) – The radial order(s) \(n\).

  • m (Union[Integral, Sequence[Integral], numpy.ndarray]) – The azimuthal frequency(ies) \(m\).

  • 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) – If True, the powers of \(\rho\) and the cosine and sine terms are cached and shared between the polynomials, which is faster when several polynomials are computed at once. If False, they are recomputed for each polynomial, which uses less memory. Default is True.

  • 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) – Only used when several polynomials are computed. If True, the output is a list of arrays. If False, the output is a single array of shape \((N, \dots)\). Default is True.

Returns:

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

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

  • Otherwise: an array of shape \((N, \dots)\), where output[i] corresponds to (n[i], m[i], rho_derivative[i], theta_derivative[i]).

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 rho or theta can not be converted to a floating point array, if n, m, 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, normalize or to_list is not a boolean.

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

References

Examples

Compute the defocus \(Z_{2}^{0}(\rho, \theta)\) on a polar grid:

import numpy
from pyzernike import zernike_polynomial

rho, theta = numpy.meshgrid(numpy.linspace(0, 1, 100), numpy.linspace(0, 2 * numpy.pi, 100))
Z20 = zernike_polynomial(rho, theta, n=2, m=0)  # array of shape (100, 100)

Compute \(Z_{2}^{2}\) and its gradient in polar coordinates (n and m are repeated):

Z, dZ_drho, dZ_dtheta = zernike_polynomial(rho, theta, n=2, m=2, rho_derivative=[0, 1, 0], theta_derivative=[0, 0, 1])

Compute the first 15 polynomials in the OSA/ANSI ordering as a single array:

from pyzernike import zernike_index_to_order

n, m = zernike_index_to_order(range(15))
Z = zernike_polynomial(rho, theta, n=n, m=m, to_list=False)  # array of shape (15, 100, 100)