Computation of the polynomials#

Explicit sum#

The radial polynomials are computed from their definition (see Definition and conventions):

\[R_{n}^{m}(\rho) = \sum_{k=0}^{(n-m)/2} \frac{(-1)^k (n-k)!}{k! ((n+m)/2 - k)! ((n-m)/2 - k)!} \rho^{n-2k}\]

The coefficients of the radial polynomial are computed exactly with integer arithmetic (math.comb), without forming any factorial, as a product of two binomial coefficients:

\[\frac{(n-k)!}{k! ((n+m)/2 - k)! ((n-m)/2 - k)!} = \binom{n-k}{k} \binom{n-2k}{(n-m)/2 - k}\]

The coefficients are exact and cannot overflow. They are rounded only once, when converted to floating point values, and the sum is evaluated with the Horner scheme in \(\rho^2\).

Note

Even with exact coefficients, the alternating sum suffers from cancellation between its large 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 recurrence relation described below must be preferred (use_recursive=True).

Derivatives#

The derivative of order \(a\) of the Zernike polynomial with respect to \(\rho\) and of order \(b\) with respect to \(\theta\) is defined as follows:

\[\begin{split}\frac{\partial^{a+b} Z_{n}^{m}}{\partial \rho^{a} \partial \theta^{b}}(\rho, \theta) = \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}\]

The phase shift \(b\frac{\pi}{2}\) is applied exactly by permuting and negating the cosine and the sine according to \(b \bmod 4\).

The derivative of order \(a\) of the radial Zernike polynomial is defined as follows:

\[\frac{d^{a}R_{n}^{m}(\rho)}{d\rho^{a}} = \sum_{k=0}^{(n-m)/2} \frac{(-1)^k (n-k)!}{k! ((n+m)/2 - k)! ((n-m)/2 - k)!} (n-2k) (n-2k-1) \ldots (n-2k-a+1) \rho^{n-2k-a}\]

The factor \((n-2k) (n-2k-1) \ldots (n-2k-a+1) = \frac{(n-2k)!}{(n-2k-a)!}\) is also computed exactly with integer arithmetic (math.perm).

Recurrence relation#

When all the polynomials up to a radial order \(N\) are needed, they are computed with the three-term recurrence relation of Shakibaei and Paramesran [Shakibaei2013]:

\[R_{n}^{m}(\rho) = \rho \, A_{n}^{m}(\rho) - R_{n-2}^{m}(\rho) \quad \text{with} \quad A_{n}^{m}(\rho) = R_{n-1}^{|m-1|}(\rho) + R_{n-1}^{m+1}(\rho)\]

for \(n \geq 1\), \(0 \leq m \leq n\) and \((n - m)\) even, with the initial condition \(R_{0}^{0}(\rho) = 1\) and the convention \(R_{n}^{m}(\rho) = 0\) whenever \(m > n\). With this convention, the same relation also gives the diagonal terms \(R_{n}^{n}(\rho) = \rho^{n}\).

The polynomials are built row by row (one row per radial order \(n\)), each row only depending on the rows \(n-1\) and \(n-2\). Each polynomial costs one multiplication and two additions whatever its order, so that all the polynomials up to the order \(N\) are computed in \(O(N^2)\) operations per point (instead of \(O(N^3)\) with the explicit sum). The recurrence only combines polynomials bounded by 1 on \([0, 1]\), so its accuracy stays close to the machine precision at any order.

Differentiating the relation \(a\) times with the Leibniz rule \(\frac{d^a}{d\rho^a}\left[\rho f(\rho)\right] = \rho f^{(a)}(\rho) + a f^{(a-1)}(\rho)\) gives a recurrence of the same form for the derivatives:

\[\frac{d^a R_{n}^{m}}{d\rho^a} = \rho \, \frac{d^a A_{n}^{m}}{d\rho^a} + a \, \frac{d^{a-1} A_{n}^{m}}{d\rho^{a-1}} - \frac{d^a R_{n-2}^{m}}{d\rho^a}\]

with the diagonal terms \(\frac{d^a R_{n}^{n}}{d\rho^a} = \frac{n!}{(n-a)!} \rho^{n-a}\) and \(\frac{d^a R_{n}^{m}}{d\rho^a} = 0\) for \(a > n\). The derivative orders \(0, 1, \dots, a\) are computed together, since the order \(a\) requires the order \(a - 1\).

Both methods are available in pyzernike.zernike_polynomial_up_to_order(), pyzernike.xy_zernike_polynomial_up_to_order(), pyzernike.zernike_evaluate() and the fit functions with the argument use_recursive. The functions computing given polynomials \((n, m)\) (pyzernike.zernike_polynomial(), pyzernike.radial_polynomial()) use the explicit sum. The gallery example Accuracy and speed at high orders compares the two methods.