Computation of the polynomials#
Explicit sum#
The radial polynomials are computed from their definition (see Definition and conventions):
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:
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:
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:
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]:
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:
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.