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}\) (seepyzernike.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_derivativeandtheta_derivativecan 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 ofrho.to_list=False: a single array of shape \((D, P, \dots)\).
See also
pyzernike.zernike_polynomial()to compute given polynomials \((n, m)\).pyzernike.zernike_index_to_order()to convert the OSA/ANSI indices to orders \((n, m)\).pyzernike.zernike_polynomials_count()for the number \(P\) of polynomials up to an order.pyzernike.zernike_evaluate()to evaluate a sum of polynomials from its coefficients, without storing all the polynomials.The page Computation of the polynomials for the explicit sum and the recurrence relation.
- 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. Ifrhoandthetaare floating point arrays of the same dtype (e.g.numpy.float32), this dtype is kept. Otherwise, both are converted tonumpy.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.nanandnumpy.inf) are accepted and converted to float. Booleans are not accepted. Default isnumpy.nan.precompute (bool, optional) – Only used if
use_recursiveis 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 valuedefaultis 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) oroutput[d, j](to_list=False) is the polynomial of OSA/ANSI indexjfor the pair(rho_derivative[d], theta_derivative[d]), with the shape ofrho. The dtype is the common floating point dtype ofrhoandtheta(numpy.float64by default).- Return type:
Union[List[List[numpy.ndarray]], numpy.ndarray]
- Raises:
TypeError – If
rhoorthetacan not be converted to a floating point array, iforderis not an integer, ifrho_derivativeortheta_derivativeis not an integer or a sequence of integers, ifdefaultis not an integer or a floating point number, or ifprecompute,use_recursive,normalizeorto_listis not a boolean.ValueError – If
rhoandthetado not have the same shape, iforderis 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)