pyzernike.radial_polynomial#
- pyzernike.radial_polynomial(rho: ndarray, n: Integral | Sequence[Integral] | ndarray, m: Integral | Sequence[Integral] | ndarray, rho_derivative: Integral | Sequence[Integral] | ndarray | None = 0, *, default: int | float = nan, precompute: bool = True, to_list: bool = True) ndarray | List[ndarray][source]#
Compute the radial Zernike polynomials \(R_{n}^{m}(\rho)\) and their derivatives.
The radial Zernike polynomials [1] are defined for \(0 \leq m \leq n\) and \(n - m\) even by:
\[R_{n}^{m}(\rho) = \sum_{k=0}^{\frac{n-m}{2}} \frac{(-1)^k (n-k)!}{k! \left(\frac{n+m}{2} - k\right)! \left(\frac{n-m}{2} - k\right)!} \rho^{n-2k}\]and the \(a\)-th derivative with respect to \(\rho\) is obtained by differentiating each term:
\[\frac{d^a R_{n}^{m}}{d\rho^a}(\rho) = \sum_{k} \frac{(-1)^k (n-k)!}{k! \left(\frac{n+m}{2} - k\right)! \left(\frac{n-m}{2} - k\right)!} \frac{(n-2k)!}{(n-2k-a)!} \rho^{n-2k-a}\]where the sum runs over the terms with \(n - 2k \geq a\).
The coefficients are computed exactly with integer arithmetic, and the sum is evaluated with the Horner scheme (see the page Computation of the polynomials). The radial polynomials are never normalized (the normalization factors of Noll are defined for the full Zernike polynomials only).
If \(m < 0\), \(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 is not finite, the output is set to
default.
Shape of the inputs and of the output.
n,mandrho_derivativecan 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 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, the radial polynomials can be obtained with the recurrence relation as \(R_{n}^{m}(\rho) = Z_{n}^{m}(\rho, 0)\) (\(m \geq 0\)), with
pyzernike.zernike_polynomial_up_to_order()anduse_recursive=True.See also
pyzernike.zernike_polynomial()for the full Zernike polynomials \(Z_{n}^{m}(\rho, \theta)\).pyzernike.radial_symbolic()for the symbolic expressions of the radial polynomials.The page Definition and conventions for the definition of the Zernike polynomials.
The page Computation of the polynomials for the explicit sum, the derivatives and the recurrence relation.
- Parameters:
rho (numpy.ndarray) – The radial coordinates \(\rho\) with any shape
(...). Floating point arrays keep their dtype (e.g.numpy.float32), any other input is converted tonumpy.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.
default (Union[int, float], optional) – The value set where \(\rho\) is outside \([0, 1]\) or 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) – If True, the powers of \(\rho\) 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.
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,mandrho_derivativeare integers: an array with the shape ofrho.Otherwise, if
to_listis True: a list of \(N\) arrays with the shape ofrho.Otherwise: an array of shape \((N, \dots)\), where
output[i]corresponds to(n[i], m[i], rho_derivative[i]).
The dtype is the floating point dtype of
rho(numpy.float64by default).- Return type:
Union[numpy.ndarray, List[numpy.ndarray]]
- Raises:
TypeError – If
rhocan not be converted to a floating point array, ifn,morrho_derivativeis not an integer or a sequence of integers, ifdefaultis not an integer or a floating point number, or ifprecomputeorto_listis not a boolean.ValueError – If the sequences
n,mandrho_derivativedo not have the same length, or if a derivative order is negative.
References
Examples
Compute \(R_{2}^{0}(\rho)\):
import numpy from pyzernike import radial_polynomial rho = numpy.linspace(0, 1, 100) R20 = radial_polynomial(rho, n=2, m=0) # array of shape (100,)
Compute \(R_{2}^{0}(\rho)\) and its first two derivatives (
nandmare repeated):R20, dR20, d2R20 = radial_polynomial(rho, n=2, m=0, rho_derivative=[0, 1, 2])
Compute all the radial polynomials of order 4 as a single array:
R4 = radial_polynomial(rho, n=4, m=[0, 2, 4], to_list=False) # array of shape (3, 100)