zernike_evaluate#
- pyzernike.zernike_evaluate(coefficients: Sequence[float] | ndarray, rho: ndarray, theta: ndarray, 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[ndarray][source]#
Evaluate a sum of Zernike polynomials \(f = \sum_j c_j Z_j\) and its derivatives in polar coordinates.
\[\frac{\partial^{a+b} f}{\partial \rho^{a} \partial \theta^{b}}(\rho, \theta) = \sum_{j=0}^{P-1} c_j \frac{\partial^{a+b} Z_j}{\partial \rho^{a} \partial \theta^{b}}(\rho, \theta)\]where \(Z_j\) is the Zernike polynomial of OSA/ANSI index \(j\) (see
pyzernike.zernike_index_to_order()). The coefficients must contain all the polynomials up to a maximum radial order \(N\), so their number must be \(P = \frac{(N+1)(N+2)}{2}\) (1, 3, 6, 10, 15, …): pad them with zeros if needed. Coefficients in the Noll or Fringe indexing can be converted withpyzernike.zernike_coefficients_convert().The polynomials are computed by chunks of points, so that the \(P\) arrays of the polynomials are never stored for all the points at once (unlike
pyzernike.zernike_polynomial_up_to_order()followed by a sum).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):If both are integers, the output is an array with the shape of
rho.If at least one of them is a sequence, the integer is repeated to the length \(D\) of the sequence (the sequences must have the same length), and the output is a list of \(D\) arrays with the shape of
rho(to_list=True), or a single array of shape \((D, \dots)\) (to_list=False).
See also
pyzernike.xy_zernike_evaluate()to evaluate a sum of polynomials on an elliptic annulus.pyzernike.zernike_fit()to compute the coefficients from sampled values.pyzernike.zernike_coefficients_convert()to convert coefficients between conventions.The page Coefficients of a sum of polynomials for the operations on the coefficients.
- Parameters:
coefficients (Union[Sequence[float], numpy.ndarray]) – The coefficients \(c_j\) in the OSA/ANSI ordering, as a 1D array of \(P = \frac{(N+1)(N+2)}{2}\) finite real numbers.
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.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. Default is True.use_recursive (bool, optional) – If True, the polynomials are computed with the recurrence relation (recommended for high orders, see
pyzernike.zernike_polynomial_up_to_order()). Default is False.normalize (bool, optional) – If True, the coefficients are those of the polynomials normalized by the factors of Noll \(N_{n}^{m} = \sqrt{(2 - \delta_{m0})(n + 1)}\) (see
pyzernike.zernike_normalization()). Default is False.to_list (bool, optional) – Only used when several derivatives are computed. If True, the output is a list of arrays. If False, the output is a single array of shape \((D, \dots)\). Default is True.
- Returns:
If
rho_derivativeandtheta_derivativeare integers: an array with the shape ofrho.Otherwise, if
to_listis True: a list of \(D\) arrays with the shape ofrho.Otherwise: an array of shape \((D, \dots)\), where
output[d]is the derivative(rho_derivative[d], theta_derivative[d]).
The dtype is the common floating point dtype of
rhoandtheta(numpy.float64by default).- Return type:
Union[numpy.ndarray, List[numpy.ndarray]]
- Raises:
TypeError – If
coefficients,rhoorthetacan not be converted to a floating point array, 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
coefficientsis not a 1D array of finite values whose length is \(\frac{(N+1)(N+2)}{2}\), ifrhoandthetado not have the same shape, if the sequences do not have the same length, or if a derivative order is negative.
Examples
Evaluate a sum of defocus and coma on a polar grid:
import numpy from pyzernike import zernike_evaluate, zernike_order_to_index coefficients = numpy.zeros(10) # all the polynomials up to the order 3 coefficients[zernike_order_to_index(n=[2, 3], m=[0, 1])] = [0.5, -0.2] rho, theta = numpy.meshgrid(numpy.linspace(0, 1, 100), numpy.linspace(0, 2 * numpy.pi, 100)) W = zernike_evaluate(coefficients, rho, theta) # array of shape (100, 100)
Compute its gradient in polar coordinates:
dW_drho, dW_dtheta = zernike_evaluate(coefficients, rho, theta, rho_derivative=[1, 0], theta_derivative=[0, 1])