pyzernike.zernike_symbolic#

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

Compute the symbolic expressions of the Zernike polynomials \(Z_{n}^{m}(\rho, \theta)\) and of 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}\]

where \(R_{n}^{m}\) is the radial polynomial (see pyzernike.radial_symbolic()). The polynomials are not normalized: multiply an expression by the factor of pyzernike.zernike_normalization() to get the normalized polynomial.

The derivative of order \(a\) with respect to \(\rho\) and \(b\) with respect to \(\theta\) is:

\[\begin{split}\frac{\partial^{a+b} Z_{n}^{m}}{\partial \rho^{a} \partial \theta^{b}} = \begin{cases} \frac{d^a R_{n}^{m}}{d\rho^a}(\rho) \, m^{b} \cos\left(m\theta + b\frac{\pi}{2}\right) & \text{if } m > 0 \\ \frac{d^a R_{n}^{0}}{d\rho^a}(\rho) \, \delta_{b,0} & \text{if } m = 0 \\ \frac{d^a R_{n}^{|m|}}{d\rho^a}(\rho) \, |m|^{b} \sin\left(|m|\theta + b\frac{\pi}{2}\right) & \text{if } m < 0 \end{cases}\end{split}\]

where the phase shift \(b\frac{\pi}{2}\) is written exactly as a sign and a swap between cos and sin. The expressions use the symbols r for \(\rho\) and t for \(\theta\).

  • If \(|m| > n\) or \(n - m\) is odd, the expression is 0.

  • If \(a > n\), or \(m = 0\) and \(b > 0\), the expression is 0.

Shape of the inputs and of the output. n, m, rho_derivative and theta_derivative can each be a single integer or a sequence of integers (list, tuple or 1D numpy array):

  • If all of them are integers, a single expression is returned.

  • 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 a list of \(N\) expressions is returned.

See also

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

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

Returns:

  • If n, m, rho_derivative and theta_derivative are integers: a single expression in the symbols r and t.

  • Otherwise: a list of \(N\) expressions, where output[i] corresponds to (n[i], m[i], rho_derivative[i], theta_derivative[i]).

Return type:

Union[sympy.Expr, List[sympy.Expr]]

Raises:
  • TypeError – If n, m, rho_derivative or theta_derivative is not an integer or a sequence of integers.

  • ValueError – If the sequences do not have the same length, or if a derivative order is negative.

References

Examples

Get the expression of the coma \(Z_{3}^{1}(\rho, \theta)\) and of its derivative with respect to \(\theta\):

from pyzernike import zernike_symbolic

print(zernike_symbolic(n=3, m=1))
print(zernike_symbolic(n=3, m=1, theta_derivative=1))
(3*r**3 - 2*r)*cos(t)
-(3*r**3 - 2*r)*sin(t)

Evaluate the gradient in polar coordinates with numpy:

import numpy
import sympy

expressions = zernike_symbolic(n=3, m=1, rho_derivative=[1, 0], theta_derivative=[0, 1])
rho, theta = numpy.meshgrid(numpy.linspace(0, 1, 100), numpy.linspace(0, 2 * numpy.pi, 100))
dZ_drho, dZ_dtheta = [sympy.lambdify(['r', 't'], expression, 'numpy')(rho, theta) for expression in expressions]

Note

For an expression equal to a constant (e.g. 0), sympy.lambdify returns a scalar and not an array. Use numpy.broadcast_to(value, rho.shape) if an array is needed.