pyzernike.zernike_polynomial#
- pyzernike.zernike_polynomial(rho: ndarray, theta: ndarray, n: array | Sequence[Integral], m: array | Sequence[Integral], rho_derivative: array | Sequence[Integral] | None = None, theta_derivative: array | Sequence[Integral] | None = None, default: Real = nan, precompute: bool = True) List[ndarray][source]#
Computes the Zernike polynomial \(Z_{n}^{m}(\rho, \theta)\) for \(\rho \leq 1\) and \(\theta \in [0, 2\pi]\).
The Zernike polynomial is defined as follows:
\[Z_{n}^{m}(\rho, \theta) = R_{n}^{m}(\rho) \cos(m \theta) \quad \text{if} \quad m \geq 0\]\[Z_{n}^{m}(\rho, \theta) = R_{n}^{-m}(\rho) \sin(-m \theta) \quad \text{if} \quad m < 0\]If \(n < 0\), \(n < |m|\), or \((n - m)\) is odd, the output is a zeros array with the same shape as \(\rho\). If \(\rho\) is not in \(0 \leq \rho \leq 1\) or \(\rho\) is numpy.nan, the output is set to the default value (numpy.nan by default).
See also
pyzernike.radial_polynomial()for computing the radial part of the Zernike polynomial \(R_{n}^{m}(\rho)\).pyzernike.core.core_polynomial()to inspect the core implementation of the computation.The page Mathematical description in the documentation for the detailed mathematical description of the Zernike polynomials.
This function allows to compute several Zernike polynomials at once for different sets of (order, azimuthal frequency, derivative orders) given as sequences, which can be more efficient than calling the polynomial function multiple times.
The parameters
rhoandthetamust be numpy arrays of the same shape.The parameters
n,m,rho_derivativeandtheta_derivativemust be sequences of integers with the same length.
The \(\rho\) and \(\theta\) values are the same for all the polynomials. The output is a list of numpy arrays, each containing the values of the Zernike polynomial for the corresponding order and azimuthal frequency. The list has the same length as the input sequences and the arrays have the same shape as
rho.Note
If the input
rhoorthetaare not floating point numpy arrays, it is converted to one withnumpy.float64dtype. If the inputrhoorthetaare floating point numpy arrays (ex:numpy.float32), the computation will be done innumpy.float32. If the inputrhoandthetaare not of the same dtype, they are both converted tonumpy.float64.- Parameters:
rho (numpy.ndarray (N-D array)) – The radial coordinate values with shape (…,).
theta (numpy.ndarray (N-D array)) – The angular coordinate values with shape (…,). Same shape as
rho.n (Sequence[Integral] or numpy.array) – A sequence (List, Tuple) or 1D numpy array of the radial order(s) of the Zernike polynomial(s) to compute. Must be non-negative integers.
m (Sequence[Integral] or numpy.array) – A sequence (List, Tuple) or 1D numpy array of the azimuthal frequency(ies) of the Zernike polynomial(s) to compute. Must be non-negative integers.
rho_derivative (Optional[Union[Sequence[Integral], numpy.array]], optional) – A sequence (List, Tuple) or 1D numpy array of the order(s) of the radial derivative(s) to compute. Must be non-negative integers. If None, is it assumed that rho_derivative is 0 for all polynomials.
theta_derivative (Optional[Union[Sequence[Integral], numpy.array]], optional) – A sequence (List, Tuple) or 1D numpy array of the order(s) of the angular derivative(s) to compute. Must be non-negative integers. If None, is it assumed that theta_derivative is 0 for all polynomials.
default (Real, optional) – The default value for invalid rho values. The default is numpy.nan. If the radial coordinate values are not in the valid domain (0 <= rho <= 1) or if they are numpy.nan, the output is set to this value.
precompute (bool, optional) – If True, precomputes the useful terms for better performance when computing multiple polynomials with the same rho values. If False, computes the useful terms on the fly for each polynomial to avoid memory overhead. The default is True.
- Returns:
A list of numpy arrays containing the Zernike polynomial values for each order and azimuthal frequency. Each array has the same shape as
rhoand the list has the same length as the input sequences. The dtype of the arrays is the same as the dtype ofrhoandthetais given, otherwisenumpy.float64.- Return type:
List[numpy.ndarray]
- Raises:
TypeError – If the rho or theta values can not be converted to a numpy array of floating points values. If n, m, rho_derivative or theta_derivative (if not None) are not sequences of integers.
ValueError – If the rho and theta do not have the same shape. If the lengths of n, m, rho_derivative and theta_derivative (if not None) are not the same.
Examples
Compute the Zernike polynomial \(Z_{2}^{0}(\rho, \theta)\) for \(\rho \leq 1\):
import numpy from pyzernike import zernike_polynomial rho = numpy.linspace(0, 1, 100) theta = numpy.linspace(0, 2*numpy.pi, 100) result = zernike_polynomial(rho, theta, n=[2], m=[0]) polynomial = result[0] # result is a list, we take the first element
Compute the radial Zernike polynomial \(Z_{2}^{0}(\rho, \theta)\) and its first derivative for \(\rho \leq 1\):
import numpy from pyzernike import zernike_polynomial rho = numpy.linspace(0, 1, 100) theta = numpy.linspace(0, 2*numpy.pi, 100) result = zernike_polynomial(rho, theta, n=[2,2], m=[0,0], rho_derivative=[0, 1]) polynomial = result[0] # result is a list, we take the first element derivative = result[1] # result is a list, we take the second element