prysm.polynomials

Various polynomials of optics.

prysm.polynomials.separable_2d_sequence(ns, ms, x, y, fx, fy=None, greedy=True)

Sequence of separable (x,y) orthogonal polynomials.

Parameters
  • ns (Iterable of int) – sequence of orders to evaluate in the X dimension

  • ms (Iterable of int) – sequence of orders to evaluate in the Y dimension

  • x (numpy.ndarray) – array of shape (m, n) or (n,) containing the X points

  • y (numpy.ndarray) – array of shape (m, n) or (m,) containing the Y points

  • fx (callable) – function which returns a generator or other sequence of modes, given args (ns, x)

  • fy (callable, optional) – function which returns a generator or other sequence of modes, given args (ns, x); y equivalent of fx, fx is used if None

  • greedy (bool, optional) – if True, consumes any generators returned by fx or fy and returns lists.

Returns

sequence of x modes (1D) and y modes (1D)

Return type

Iterable, Iterable

prysm.polynomials.mode_1d_to_2d(mode, x, y, which='x')

Expand a 1D representation of a mode to 2D.

Notes

You likely only want to use this function for plotting or similar, it is much faster to use sum_of_xy_modes to produce 2D surfaces described by a sum of modes which are separable in x and y.

Parameters
  • mode (numpy.ndarray) – mode, representing a separable mode in X, Y along {which} axis

  • x (numpy.ndarray) – x dimension, either 1D or 2D

  • y (numpy.ndarray) – y dimension, either 1D or 2D

  • which (str, {‘x’, ‘y’}) – which dimension the mode is produced along

Returns

2D version of the mode

Return type

numpy.ndarray

prysm.polynomials.sum_of_xy_modes(modesx, modesy, x, y, weightsx=None, weightsy=None)

Weighted sum of separable x and y modes projected over the 2D aperture.

Parameters
  • modesx (iterable) – sequence of x modes

  • modesy (iterable) – sequence of y modes

  • x (numpy.ndarray) – x points

  • y (numpy.ndarray) – y points

  • weightsx (iterable, optional) – weights to apply to modesx. If None, [1]*len(modesx)

  • weightsy (iterable, optional) – weights to apply to modesy. If None, [1]*len(modesy)

Returns

modes summed over the 2D aperture

Return type

numpy.ndarray

prysm.polynomials.sum_of_2d_modes(modes, weights)

Compute a sum of 2D modes.

Parameters
  • modes (iterable) – sequence of ndarray of shape (k, m, n); a list of length k with elements of shape (m,n) works

  • weights (numpy.ndarray) – weight of each mode

Returns

ndarray of shape (m, n) that is the sum of modes as given

Return type

numpy.ndarry

prysm.polynomials.hopkins(a, b, c, r, t, H)

Hopkins’ aberration expansion.

This function uses the “W020” or “W131” like notation, with Wabc separating into the a, b, c arguments. To produce a sine term instead of cosine, make a the negative of the order. In other words, for W222S you would use hopkins(2, 2, 2, …) and for W222T you would use hopkins(-2, 2, 2, …).

Parameters
  • a (int) – azimuthal order

  • b (int) – radial order

  • c (int) – order in field (“H-order”)

  • r (numpy.ndarray) – radial pupil coordinate

  • t (numpy.ndarray) – azimuthal pupil coordinate

  • H (numpy.ndarray) – field coordinate

Returns

polynomial evaluated at this point

Return type

numpy.ndarray

prysm.polynomials.lstsq(modes, data)

Least-Squares fit of modes to data.

Parameters
  • modes (iterable) – modes to fit; sequence of ndarray of shape (m, n)

  • data (numpy.ndarray) – data to fit, of shape (m, n) place NaN values in data for points to ignore

Returns

fit coefficients

Return type

numpy.ndarray

Zernike polynomials.

prysm.polynomials.zernike.zernike_norm(n, m)

Norm of a Zernike polynomial with n, m indexing.

prysm.polynomials.zernike.zero_separation(n)

Zero separation in normalized r based on radial order n.

prysm.polynomials.zernike.zernike_nm(n, m, r, t, norm=True)

Zernike polynomial of radial order n, azimuthal order m at point r, t.

Parameters
  • n (int) – radial order

  • m (int) – azimuthal order

  • r (numpy.ndarray) – radial coordinates

  • t (numpy.ndarray) – azimuthal coordinates

  • norm (bool, optional) – if True, orthonormalize the result (unit RMS) else leave orthogonal (zero-to-peak = 1)

Returns

zernike mode of order n,m at points r,t

Return type

numpy.ndarray

prysm.polynomials.zernike.zernike_nm_sequence(nms, r, t, norm=True)

Zernike polynomial of radial order n, azimuthal order m at point r, t.

Parameters
  • nms (iterable of tuple of int,) – sequence of (n, m); looks like [(1,1), (3,1), …]

  • r (numpy.ndarray) – radial coordinates

  • t (numpy.ndarray) – azimuthal coordinates

  • norm (bool, optional) – if True, orthonormalize the result (unit RMS) else leave orthogonal (zero-to-peak = 1)

Returns

yields one mode at a time of nms

Return type

generator

prysm.polynomials.zernike.nm_to_fringe(n, m)

Convert (n,m) two term index to Fringe index.

prysm.polynomials.zernike.nm_to_ansi_j(n, m)

Convert (n,m) two term index to ANSI single term index.

prysm.polynomials.zernike.ansi_j_to_nm(idx)

Convert ANSI single term to (n,m) two-term index.

prysm.polynomials.zernike.noll_to_nm(idx)

Convert Noll Z to (n, m) two-term index.

prysm.polynomials.zernike.fringe_to_nm(idx)

Convert Fringe Z to (n, m) two-term index.

prysm.polynomials.zernike.zernikes_to_magnitude_angle_nmkey(coefs)

Convert Zernike polynomial set to a magnitude and phase representation.

Parameters

coefs (list of tuples) – a list looking like[(1,2,3),] where (1,2) are the n, m indices and 3 the coefficient

Returns

dict keyed by tuples of (n, |m|) with values of (rho, phi) where rho is the magnitudes, and phi the phase

Return type

dict

prysm.polynomials.zernike.zernikes_to_magnitude_angle(coefs)

Convert Zernike polynomial set to a magnitude and phase representation.

This function is identical to zernikes_to_magnitude_angle_nmkey, except its keys are strings instead of (n, |m|)

Parameters

coefs (list of tuples) – a list looking like[(1,2,3),] where (1,2) are the n, m indices and 3 the coefficient

Returns

dict keyed by friendly name strings with values of (rho, phi) where rho is the magnitudes, and phi the phase

Return type

dict

prysm.polynomials.zernike.nm_to_name(n, m)

Convert an (n,m) index into a human readable name.

Parameters
  • n (int) – radial polynomial order

  • m (int) – azimuthal polynomial order

Returns

a name, np.g. Piston or Primary Spherical

Return type

str

prysm.polynomials.zernike.top_n(coefs, n=5)

Identify the top n terms in the wavefront expansion.

Parameters
  • coefs (dict) – keys of (n,m), values of magnitudes, e.g. {(3,1): 2} represents 2 of primary coma

  • n (int, optional) – identify the top n terms.

Returns

list of tuples (magnitude, index, term)

Return type

list

prysm.polynomials.zernike.barplot(coefs, names=None, orientation='h', buffer=1, zorder=3, number=True, offset=0, width=0.8, fig=None, ax=None)

Create a barplot of coefficients and their names.

Parameters
  • coefs (dict) – with keys of Zn, values of numbers

  • names (dict) – with keys of Zn, values of names (e.g. Primary Coma X)

  • orientation (str, {‘h’, ‘v’, ‘horizontal’, ‘vertical’}) – orientation of the plot

  • buffer (float, optional) – buffer to use around the left and right (or top and bottom) bars

  • zorder (int, optional) – zorder of the bars. Use zorder > 3 to put bars in front of gridlines

  • number (bool, optional) – if True, plot numbers along the y=0 line showing indices

  • offset (float, optional) – offset to apply to bars, useful for before/after Zernike breakdowns

  • width (float, optional) – width of bars, useful for before/after Zernike breakdowns

  • fig (matplotlib.figurnp.Figure) – Figure containing the plot

  • ax (matplotlib.axes.Axis) – Axis containing the plot

Returns

  • fig (matplotlib.figurnp.Figure) – Figure containing the plot

  • ax (matplotlib.axes.Axis) – Axis containing the plot

prysm.polynomials.zernike.barplot_magnitudes(magnitudes, orientation='h', sort=False, buffer=1, zorder=3, offset=0, width=0.8, fig=None, ax=None)

Create a barplot of magnitudes of coefficient pairs and their names.

e.g., astigmatism will get one bar.

Parameters
  • magnitudes (dict) – keys of names, values of magnitudes. E.g., {‘Primary Coma’: 1234567}

  • orientation (str, {‘h’, ‘v’, ‘horizontal’, ‘vertical’}) – orientation of the plot

  • sort (bool, optional) – whether to sort the zernikes in descending order

  • buffer (float, optional) – buffer to use around the left and right (or top and bottom) bars

  • zorder (int, optional) – zorder of the bars. Use zorder > 3 to put bars in front of gridlines

  • offset (float, optional) – offset to apply to bars, useful for before/after Zernike breakdowns

  • width (float, optional) – width of bars, useful for before/after Zernike breakdowns

  • fig (matplotlib.figure.Figure) – Figure containing the plot

  • ax (matplotlib.axes.Axis) – Axis containing the plot

Returns

  • fig (matplotlib.figure.Figure) – Figure containing the plot

  • ax (matplotlib.axes.Axis) – Axis containing the plot

Tools for working with Q (Forbes) polynomials.

prysm.polynomials.qpoly.g_qbfs(n_minus_1)

g(m-1) from oe-18-19-19700 eq. (A.15).

prysm.polynomials.qpoly.h_qbfs(n_minus_2)

h(m-2) from oe-18-19-19700 eq. (A.14).

prysm.polynomials.qpoly.f_qbfs(n)

f(m) from oe-18-19-19700 eq. (A.16).

prysm.polynomials.qpoly.Qbfs(n, x)

Qbfs polynomial of order n at point(s) x.

Parameters
  • n (int) – polynomial order

  • x (numpy.array) – point(s) at which to evaluate

Returns

Qbfs_n(x)

Return type

numpy.ndarray

prysm.polynomials.qpoly.Qbfs_sequence(ns, x)

Qbfs polynomials of orders ns at point(s) x.

Parameters
  • ns (Iterable of int) – polynomial orders

  • x (numpy.array) – point(s) at which to evaluate

Returns

yielding one order of ns at a time

Return type

generator of numpy.ndarray

prysm.polynomials.qpoly.Qcon(n, x)

Qcon polynomial of order n at point(s) x.

Parameters
  • n (int) – polynomial order

  • x (numpy.array) – point(s) at which to evaluate

Returns

Qcon_n(x)

Return type

numpy.ndarray

Notes

The argument x is notionally uniformly spaced 0..1. The Qcon polynomials are obtained by computing c = x^4. A transformation is then made, x => 2x^2 - 1 and the Qcon polynomials are defined as the jacobi polynomials with alpha=0, beta=4, the same order n, and the transformed x. The result of that is multiplied by c to yield a Qcon polynomial. Sums can more quickly be calculated by deferring the multiplication by c.

prysm.polynomials.qpoly.Qcon_sequence(ns, x)

Qcon polynomials of orders ns at point(s) x.

Parameters
  • ns (Iterable of int) – polynomial orders

  • x (numpy.array) – point(s) at which to evaluate

Returns

yielding one order of ns at a time

Return type

generator of numpy.ndarray

prysm.polynomials.qpoly.abc_q2d(n, m)

A, B, C terms for 2D-Q polynomials. oe-20-3-2483 Eq. (A.3).

Parameters
  • n (int) – radial order

  • m (int) – azimuthal order

Returns

A, B, C

Return type

float, float, float

prysm.polynomials.qpoly.G_q2d(n, m)

G term for 2D-Q polynomials. oe-20-3-2483 Eq. (A.15).

Parameters
  • n (int) – radial order

  • m (int) – azimuthal order

Returns

G

Return type

float

prysm.polynomials.qpoly.F_q2d(n, m)

F term for 2D-Q polynomials. oe-20-3-2483 Eq. (A.13).

Parameters
  • n (int) – radial order

  • m (int) – azimuthal order

Returns

F

Return type

float

prysm.polynomials.qpoly.g_q2d(n, m)

Lowercase g term for 2D-Q polynomials. oe-20-3-2483 Eq. (A.18a).

Parameters
  • n (int) – radial order less one (n - 1)

  • m (int) – azimuthal order

Returns

g

Return type

float

prysm.polynomials.qpoly.f_q2d(n, m)

Lowercase f term for 2D-Q polynomials. oe-20-3-2483 Eq. (A.18b).

Parameters
  • n (int) – radial order

  • m (int) – azimuthal order

Returns

f

Return type

float

prysm.polynomials.qpoly.Q2d(n, m, r, t)

2D Q polynomial, aka the Forbes polynomials.

Parameters
  • n (int) – radial polynomial order

  • m (int) – azimuthal polynomial order

  • r (numpy.ndarray) – radial coordinate, slope orthogonal in [0,1]

  • t (numpy.ndarray) – azimuthal coordinate, radians

Returns

array containing Q2d_n^m(r,t) the leading coefficient u^m or u^2 (1 - u^2) and sines/cosines are included in the return

Return type

numpy.ndarray

prysm.polynomials.qpoly.Q2d_sequence(nms, r, t)

Sequence of 2D-Q polynomials.

Parameters
  • nms (iterable of tuple) – (n,m) for each desired term

  • r (numpy.ndarray) – radial coordinates

  • t (numpy.ndarray) – azimuthal coordinates

Returns

yields one term for each element of nms

Return type

generator

High performance / recursive jacobi polynomial calculation.

prysm.polynomials.jacobi.weight(alpha, beta, x)

The weight function of the jacobi polynomials for a given alpha, beta value.

prysm.polynomials.jacobi.recurrence_ac_startb(n, alpha, beta)

a and c terms of the recurrence relation from Wikipedia, * P_n^(a,b).

Also computes partial b term; all components without x

prysm.polynomials.jacobi.jacobi(n, alpha, beta, x)

Jacobi polynomial of order n with weight parameters alpha and beta.

Notes

This function is faster than scipy.special.jacobi when Pnm1 and Pnm2 are supplied and is stable to high order. Performance benefit ranges from 2-5x.

Parameters
  • n (int) – polynomial order

  • alpha (float) – first weight parameter

  • beta (float) – second weight parameter

  • x (numpy.ndarray) – x coordinates to evaluate at

Returns

jacobi polynomial evaluated at the given points

Return type

numpy.ndarray

prysm.polynomials.jacobi.jacobi_sequence(ns, alpha, beta, x)

Jacobi polynomials of order 0..n_max with weight parameters alpha and beta.

Parameters
  • ns (iterable) – sorted polynomial orders to return, e.g. [1, 3, 5, 7, …]

  • alpha (float) – first weight parameter

  • beta (float) – second weight parameter

  • x (numpy.ndarray) – x coordinates to evaluate at

Returns

array of shape (n_max+1, len(x))

Return type

numpy.ndarray

Chebyshev polynomials.

prysm.polynomials.cheby.cheby1(n, x)

Chebyshev polynomial of the first kind of order n.

Parameters
  • n (int) – order to evaluate

  • x (numpy.ndarray) – point(s) at which to evaluate, orthogonal over [-1,1]

prysm.polynomials.cheby.cheby1_sequence(ns, x)

Chebyshev polynomials of the first kind of orders ns.

Faster than chevy1 in a loop.

Parameters
  • ns (Iterable of int) – orders to evaluate

  • x (numpy.ndarray) – point(s) at which to evaluate, orthogonal over [-1,1]

prysm.polynomials.cheby.cheby2(n, x)

Chebyshev polynomial of the second kind of order n.

Parameters
  • n (int) – order to evaluate

  • x (numpy.ndarray) – point(s) at which to evaluate, orthogonal over [-1,1]

prysm.polynomials.cheby.cheby2_sequence(ns, x)

Chebyshev polynomials of the second kind of orders ns.

Faster than chevy1 in a loop.

Parameters
  • ns (Iterable of int) – orders to evaluate

  • x (numpy.ndarray) – point(s) at which to evaluate, orthogonal over [-1,1]

Legendre polynomials.

prysm.polynomials.legendre.legendre(n, x)

Legendre polynomial of order n.

Parameters
  • n (int) – order to evaluate

  • x (numpy.ndarray) – point(s) at which to evaluate, orthogonal over [-1,1]

prysm.polynomials.legendre.legendre_sequence(ns, x)

Legendre polynomials of orders ns.

Faster than chevy1 in a loop.

Parameters
  • ns (int) – orders to evaluate

  • x (numpy.ndarray) – point(s) at which to evaluate, orthogonal over [-1,1]