polynomial¶
A collection of functions to manipulate polynomials and their coefficients.
- pysiaf.utils.polynomial.add_rotation(A, B, theta_deg)[source]¶
Add rotation after polynomial transformation.
Use when a distortion transformation using polynomials A and B is followed by a rotation.
u = A(x,y) v = B(x,y) followed by u2 = u*cos(theta) + v*sin(theta) v2 = -u*sin(theta) + v*cos(theta) This routine supplies a modified pair of polynomial which combine both steps i.e u2 = A2(x,y), v2 = B2(x,y)
- Parameters:
- Aarray
Set of polynomial coefficients converting from (x,y) to a variable u
- Barray
set of polynomial coefficients converting from(x,y) to avariable v
- theta_degfloat
The angle in degrees of a rotationin the (u,v) plane
- Returns:
- A2array
set of polynomial coefficiients providing combined steps from (x,y) to u2
- B2array
set of polynomial coefficients providing combined steps from (x,y) to v2
Notes
Function formerly named Rotate or rotate_coefficients. Ported from makeSIAF.py by J. Sahlmann 2018-01-03.
- pysiaf.utils.polynomial.choose(n, r)[source]¶
Return number of ways of choosing r items from an array with n items.
- Parameters:
- nint
number of items to choose from
- rint
number of items to choose
- Returns:
- combinationsint
The number if ways of making the choice
- pysiaf.utils.polynomial.dpdx(a, x, y)[source]¶
Differential with respect to x.
The polynomial is defined as p(x,y) = a[i,j] * x**(i-j) * y**j summed over i and j. Then dp/dx = (i-j) * a[i,j] * x**(i-j-1) * y**j.
- Parameters:
- aarray
a linear array of polynomial coefficients in JWST order.
- xarray
an integer or float variable(or an array of same) representing pixel x positions
- yarray
a variable (or an array) representing pixel y positions. x and y must be of same shape.
- Returns:
- differentialarray
float values of dp/dx for the given (x,y) point(s)
- pysiaf.utils.polynomial.dpdy(a, x, y)[source]¶
Differential with respect to y.
The polynomial is defined as p(x,y) = a[i,j] * x**(i-j) * y**j, summed over i and j Then dp/dy = j * a[i,j] * x**(i-j) * y**(j-1)
- Parameters:
- aarray
an array of polynomial coefficients in JWST arrangement. The number of coefficients must be (order+1)(order+2)/2
- xarray
an integer or float variable(or an array of same) representing pixel x positions
- yarray
a variable (or an array) representing pixel y positions
- Returns:
- differentialarray
float value of dp/dy for the given (x,y) point(s) where p(x,y) is the value of the polynomial
- pysiaf.utils.polynomial.flatten(coefficients)[source]¶
Convert triangular layout to linear array.
For many of the polynomial operations the coefficients A[i,j] are contained in an array of dimension (order+1, order+1) but with all elements where j > i set equal to zero. This is what is called the triangular layout.
The flattened layout is a one-dimensional array containing only the elements where j <= i.
- Parameters:
- coefficientsarray
a two-dimensional float array of shape (order+1, order+1) supplying the polynomial coefficients in JWST order. The coefficient at position [i,j] multiplies the value of x**(i-j) * y**j.
- Returns:
- flate_coefficientsarray
a one-dimensional array including only those terms where i <= j
- pysiaf.utils.polynomial.flip_x(A)[source]¶
Change sign of all coefficients with odd x power.
Used when we have a polynomial expansion in terms of variables x and y and we wish to obtain one in which the sign of x is reversed
- Parameters:
- Aarray
A set of polynomial coefficients given in the triangular layout as described in poly
- Returns:
- AFarray
Modified or flipped set of coefficients matching negated x values.
- pysiaf.utils.polynomial.flip_xy(A)[source]¶
Change sign for coeffs where sum of x and y powers is odd.
Used when we have a polynomial expansion in terms of variables x and y and we wish to obtain one in which the signs of x and y are reversed
- Parameters:
- Aarray
A set of polynomial coefficients given in the triangular layout as described in the function poly
- Returns:
- AFarray
Modified or flipped set of coefficients matching negated x and y values.
- pysiaf.utils.polynomial.flip_y(A)[source]¶
Change sign of all coefficients with odd y power.
Used when we have a polynomial expansion in terms of variables x and y and we wish to obtain one in which the sign of y is reversed
- Parameters:
- Aarray
A set of polynomial coefficients given in the triangular layout as described in the function poly
- Returns:
- AFarray
Modified or flipped set of coefficients matching negated y values.
- pysiaf.utils.polynomial.invert(A, B, u, v, verbose=False)[source]¶
Newton Raphson method in two dimensions.
Given that u = A[i,j] * x**(i-j) * y**j and v = B[i,j] * x**(i-j) * y**j find the values of x and y from the values of u and v
- Parameters:
- Aarray
A set of polynomial coefficients given in the linear layout as described in the function poly converting (x,y) to u
- Barray
A set of polynomial coefficients given in the linear layout as described in the function poly converting (x,y) to v
- uarray
The result of applying the A coefficients to the (x,y) position
- varray
The result of applying the B coefficients to the (x, y)position
- verbosebool
Logical variable, set True if full text output required
- Returns:
- x, ytuple of arrays
The pair of values which transform to (u,v)
- errfloat
the standard deviation of the fit
- iterationint
the number of iterations taken to determine the solution
- pysiaf.utils.polynomial.jacob(a, b, x, y)[source]¶
Calculate relative area using the Jacobian.
` `
` | da_dx db_dx | `
`Jacobian = | | `
` | da_dy db_dy | `
` `
Then the relative area is the absolute value of the determinant of the Jacobian. x and y will usually be Science coordinates while u and v are Ideal coordinates
- Parameters:
- aarray
set of polynomial coefficients converting from (x,y) to u
- barray
set of polynomial coefficients converting from (x,y) to v
- xarray
x pixel position or array of x positions
- yarray
y pixel position or array of y positions matching the y positions
- Returns:
- areaarray
area in (u,v) coordinates matching unit area in the (x,y) coordinates.
- pysiaf.utils.polynomial.number_of_coefficients(poly_degree)[source]¶
Return number of coefficients corresponding to polynomial degree.
- pysiaf.utils.polynomial.poly(a, x, y, order=4)[source]¶
Polynomial evaluation.
pol = a[i,j] * x**(i-j) * y**j summed over i and j, where i runs from 0 to order. Then for each value of i, j runs from 0 to i. For many of the polynomial operations the coefficients A[i,j] are contained in an array of dimension (order+1, order+1) but with all elements where j > i set equal to zero. This is called the triangular layout. The flattened layout is a one-dimensional array containing copies of only the elements where j <= i.
The JWST layout is a[0,0] a[1,0] a[1,1] a[2,0] a[2,1] a[2,2] … The number of coefficients will be (n+1)(n+2)/2
- Parameters:
- aarray
float array of polynomial coefficients in flattened arrangement
- xarray
x pixel position. Can be integer or float or an array of integers or floats
- yarray
y pixel position in same layout as x positions.
- orderint
integer polynomial order
- Returns:
- polfloat
result as described above
- pysiaf.utils.polynomial.polyfit(u, x, y, order)[source]¶
Fit polynomial to a set of u values on an x,y grid.
u is a function u(x,y) being a polynomial of the form u = a[i, j] x**(i-j) y**j. x and y can be on a grid or be arbitrary values This version uses scipy.linalg.solve instead of matrix inversion. u, x and y must have the same shape and may be 2D grids of values.
- Parameters:
- uarray
an array of values to be the results of applying the sought after polynomial to the values (x,y)
- xarray
an array of x values
- yarray
an array of y values
- orderint
the polynomial order
- Returns:
- coeffs: array
polynomial coefficients being the solution to the fit.
- pysiaf.utils.polynomial.polynomial_degree(number_of_coefficients)[source]¶
Return degree of the polynomial that has number_of_coefficients.
- Parameters:
- number_of_coefficientsint
Number of polynomial coefficients
- Returns:
- polynomial_degreeint
Degree of the polynomial
- pysiaf.utils.polynomial.prepend_rotation_to_polynomial(a, theta, verbose=False)[source]¶
Rotate axes of coefficients by theta degrees.
Used when a distortion transformation using polynomials A and B is preceded by a rotation. The set of polynomial coefficients a[i,j] transform (x,y) as u = a[i,j] * x**(i-j) * y**j Summation over repeated indices is implied. If now we have a set of variables (xp,yp) rotated from (x,y) so that xp = x * cos(theta) - y * sin(theta) yp = x * sin(theta) + y * cos(theta) find a set of polynomial coefficients ap so that the same value of u is obtained from (xp,yp) i.e, u = ap[i,j] * xp**(i-j) * yp**j The rotation is opposite to the usual rotation as this routine was designed for the inverse transformation between Ideal and V2V3 or tel. Effectively the angle is reversed
- Parameters:
- aarray
Set of polynomial coefficients
- thetafloat
rotation angle in degrees
- verbosebool
logical variable set True only if print-out of coefficient factors is desired.
- Returns:
- arotatearray
set of coefficients derived as described above.
Notes
Function was formerly named RotateCoeffs.
- pysiaf.utils.polynomial.print_triangle(coefficients)[source]¶
Print coefficients in triangular layout.
A[0] A[1] A[2] A[3] A[4] A[5] … equivalent to A[0,0] A[1,0] A[1,1] A[2,0] A[2,1] A[2,2] … in [i,j] terms.
See method poly for details. This is just to display the coefficients. No calculation performed.
- Parameters:
- coefficientsarray
polynomial float array in linear layout
- pysiaf.utils.polynomial.reorder(A, B)[source]¶
Change coefficient order from y**2 xy x**2 to x**2 xy y**2 in both A and B.
- Parameters:
- Aarray
polynomial coefficients
- Barray
polynomial coefficients
- Returns:
- A2, B2: numpy arrays
coefficients with changed order
- pysiaf.utils.polynomial.rescale(A, B, C, D, scale)[source]¶
Apply a scale to forward and inverse polynomial coefficients.
- Parameters:
- Aarray
Polynomial coefficients
- Barray
Polynomial coefficients
- Carray
Polynomial coefficients
- Darray
Polynomial coefficients
- scalefloat
Scale factor to apply
- Returns:
- A_scaled, B_scaled, C_scaled, D_scaledtuple of numpy arrays
the scales coefficients
Notes
Ported from makeSIAF.py by J. Sahlmann 2018-01-03. J. Sahlmann 2018-01-04: fixed side-effect on ABCD variables
- pysiaf.utils.polynomial.rotation_scale_skew_from_derivatives(b, c, e, f)[source]¶
Compute rotations, scales, and skews from polynomial derivatives.
The four partial derivatives of coefficients that transform between two frames E and R are:
- Parameters:
- bfloat
(dx_E)/(dx_R )
- cfloat
(dx_E)/(dy_R )
- efloat
(dy_E)/(dx_R )
- ffloat
(dy_E)/(dy_R )
- Returns:
- pysiaf.utils.polynomial.shift_coefficients(a, xshift, yshift, verbose=False)[source]¶
Calculate coefficients of polynomial when shifted to new origin.
Given a polynomial function such that u = a[i,j] * x**[i-j] * y**[j] summed over i and j. Find the polynomial function ashift centered at xshift, yshift i.e the same value of u = ashift[i,j] * (x-xshift)**(i-j) * (y-yshift)**j.
- Parameters:
- aarray
Set of coefficients for a polynomial of the given order in JWST order
- xshiftfloat
x position in pixels of new solution center
- yshiftfloat
y position in pixels of new solution center
- verbosebool
logical variable to choose print-out of coefficient table - defaults to False
- Returns:
- ashiftarray
shifted version of the polynomial coefficients.
- pysiaf.utils.polynomial.transform_coefficients(A, a, b, c, d, verbose=False)[source]¶
Transform polynomial coefficients.
This allows for xp = a*x + b*y yp = c*x + d*y
- Parameters:
- Aarray
Polynomial coefficients
- afloat
factor
- bfloat
factor
- cfloat
factor
- dfloat
factor
- verbosebool
verbosity
- Returns:
- ATarray
Transformed coefficients
Notes
Designed to work with Sabatke solutions which included a linear transformation of the pixel coordinates before the polynomial distortion solution was calculated.
transform_coefficients
combines the two steps into a single polynomial.
- pysiaf.utils.polynomial.triangular_layout(coefficients)[source]¶
Convert linear array to 2-D array with triangular coefficient layout.
This is the reverse of the flatten method.
- Parameters:
- coefficientsarray
float array of polynomial coefficients. Must be of dimension (order+1)(order+2)/2.
- Returns:
- triangular_coefficientsarray
coefficients in triangular layout as described in poly.
- pysiaf.utils.polynomial.two_step(A, B, a, b)[source]¶
Combine linear step followed by a polynomial step into a single polynomial.
Designed to process Sabatke polynomials which had a linear transformation ahead of the polynomial fits.
Starting from a pair of polynomial arrays A and B such that u = A[i,j[ * xp**(i-j) * yp**j v = B[i,j] * xp**(i-j) * yp**j in which xp = a[0] + a[1].x + a[2].y yp = b[0] + b[1].x + b[2].y find AP and BP such that the same u and v values are given by u = AP[i,j] * x**(i-j) * y**j v = BP[i,j] * x**(i-j) * y**j All input and output polynomials are flattened arrays of dimension (order+1)(order+2)/2 Internally they are processed as equivalent two dimensional arrays as described in the poly documentation.
- Parameters:
- Aarray
polynomial converting from secondary xp and yp pixel positions to final coordinates u
- Barray
polynomial converting from secondary xp and yp pixel positions to final coordinates v
- Aflatarray
set of linear coefficients converting (x,y) to xp
- Bflatarray
set of linear coefficients converting (x,y) to yp
- Returns:
- Aflat, Bflattuple of arrays
polynomial coefficients as calculated