pyzernike.zernike_symbolic#

pyzernike.zernike_symbolic(n: array | Sequence[Integral], m: array | Sequence[Integral], rho_derivative: array | Sequence[Integral] | None = None, theta_derivative: array | Sequence[Integral] | None = None) List[Expr][source]#

Compute the symbolic expression of 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 polynomial is zero.

See also

The function allows to display several Zernike polynomials for different sets of (order, azimuthal frequency, derivative orders) given as sequences.

  • The parameters n, m, rho_derivative and theta_derivative must be sequences of integers with the same length.

The output is a list of sympy expressions, each containing the symbolic expression of the Zernike polynomial for the corresponding order and azimuthal frequency. The list has the same length as the input sequences.

Note

  • The symbol r is used to represent the radial coordinate \(\rho\) in the symbolic expression.

  • The symbol t is used to represent the angular coordinate \(\theta\) in the symbolic expression.

Parameters:
  • 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.

Returns:

A list of symbolic expressions containing the Zernike polynomial values for each order and azimuthal frequency. Each expression is a sympy expression that can be evaluated for specific values of \(\rho\) and \(\theta\).

Return type:

List[sympy.Expr]

Raises:
  • TypeError – If n, m, rho_derivative or theta_derivative (if not None) are not sequences of integers.

  • ValueError – If the lengths of n, m, rho_derivative or theta_derivative (if not None) are not the same.

Examples

Compute the expression of the radial Zernike polynomial \(Z_{2}^{1}(\rho, \theta)\):

from pyzernike import zernike_symbolic
result = zernike_symbolic(n=[2], m=[1])
expression = result[0]  # result is a list, we take the first element
print(expression)
(2*r**2 - 1)*cos(t)

Then evaluate the expression for a specific value of \(\rho\) and \(\theta\):

import numpy
import sympy
rho = numpy.linspace(0, 1, 100)
theta = numpy.linspace(0, 2 * numpy.pi, 100)

# `r` represents the radial coordinate in the symbolic expression
# `t` represents the angular coordinate in the symbolic expression

func = sympy.lambdify(['r', 't'], expression, 'numpy')
evaluated_result = func(rho, theta)