cyjax.polynomial.FermatPoly#

class cyjax.polynomial.FermatPoly(poly_degree=5)#

Bases: HomPoly

__init__(poly_degree=5)#

Homogeneous polynomial.

Practically, this class allows the computation of the homogeneous polynomial itself in terms of homogeneous coordinates, as well as in terms of any set of affine coordinates. The latter are well-defined up to overall scaling. Specifically, the scale ambiguity is fixed by assuming the homogeneous coordinate omitted to get the affine coordinates was rescaled to the value 1.

Just like Poly, the class splits the inputs into one or more variables and parameters.

Methods

__init__([poly_degree])

Homogeneous polynomial.

affine_poly(patch)

Get affine polynomial for given patch.

all_symbols([as_arrays, include_params])

All symbols appearing as inputs to the polynomial.

diff(var)

Differentiate with respect to a variable using sympy.

from_coeffs_and_powers(coeffs, powers[, ...])

Construct Poly object from arrays of powers and coeffs.

from_poly(poly)

Initializer with consistent signature for all Poly subclasses.

from_sympy(poly[, variable_names, ...])

Construct a Poly object from a sympy/string expression.

to_coeffs_and_powers()

Extract powers of variables and lists of coefficients.

transform_eval(**partial_kwargs)

Generate an efficient numerical evaluation function.

unique_patch_index(patch)

Convert patch indices to single unique integer index.

Attributes

domain

Complex domain of the polynomial.

parameter_position

Mapping between parameter and its index in the ordering.

sympy_poly

Representation of the polynomial as sympy.Poly object.

variable_index_dict

Mappings between positional index and symbolic index of variables.

variable_position

Mapping between variable name and its index in the ordering.

variable_names

Names of variables of the polynomial.

variable_indices

Indices for each variable or None (if scalar).

parameters

Parameters of the polynomial as sequence of sympy symbols.

degree

Polynomial degree.

affine_poly(patch)#

Get affine polynomial for given patch.

Important: The affine patch index is specified in terms of the actual numerical index into input variables. The indices used for pretty-printing is ignored. If these indices are consecutive and start at 0, there is no difference, however.

Parameters:

patch (Union[int, Sequence[int]]) – Single or multiple patch indices. Each must be an integer between zero and the length of the variable (exclusively).

Return type:

Poly

Returns:

A Poly object representing the affine polynomial.

all_symbols(as_arrays=True, include_params=True)#

All symbols appearing as inputs to the polynomial.

Parameters:
  • as_arrays – If true, group (non-scalar) variables with indices into a single numpy array. Otherwise, one symbol is added to the output for each index. Similarly, all parameters are put into a single numpy array if true.

  • include_params – If true, includes the parameters of the polynomial.

Return type:

list[Union[Symbol, ndarray]]

Returns:

A list, either containing sympy symbols representing the inputs of the polynomial, or numpy arrays of symbols which are grouped in the same way as the numerical inputs are when evaluating the polynomial.

degree: int#

Polynomial degree.

diff(var)#

Differentiate with respect to a variable using sympy.

Return type:

Poly

property domain: Domain#

Complex domain of the polynomial.

Polynomials here are defined over the field or complex numbers. The Domain object additionally tracks the parameters of the polynomial.

classmethod from_coeffs_and_powers(coeffs, powers, coeffs_params=None, variable_names=None, variable_indices=None, parameters=None)#

Construct Poly object from arrays of powers and coeffs.

The polynomial is constructed from the inputs as follows:

\[\sum_c \mathrm{coeffs}_c \prod_p \mathrm{coeffs\_params}_{cp} \prod_{v} \prod_{i \in \mathrm{variable\_indices}_v} \mathrm{variable\_names[v]}_i^{\mathrm{powers}_{vci}}\]

By default, variable indices are derived as ranges with length given by the shape of the powers. If no variable names are given, they are set to a, b, c etc. Parameters can be automatically extracted from coeffs_params.

Parameters:
  • coeffs (Sequence[Union[Array, ndarray, bool_, number, float, int]]) – Sequence of numerical coefficients of monomials in the full polynomial expression.

  • powers (Sequence[Sequence[Sequence[int]]]) – For each variable, gives the powers in each monomial and for each index of the variable (in that order of indexing).

  • coeffs_params (Optional[Sequence[Sequence[Union[Symbol, str]]]]) – Symbolic coefficients of monomial terms.

  • variable_names (Optional[Sequence[str]]) – Names of variables. If not given, pick letters from the alphabet.

  • variable_indices (Optional[Sequence[Optional[Sequence[int]]]]) – Indices of variables. If not given, can derive dimensions (number of indices) from the shape of the corresponding power array.

  • parameters (Optional[Sequence[Union[Symbol, str]]]) – Sequence of parameters. If given, sets the order of parameters that is expected when evaluating the polynomial. Important to explicitly specify if not all parameters actually appear in the polynomial.

Returns:

A Poly object representing the polynomial.

Notes

Internally, the constructed Poly object still uses a sympy expression. If desired, a subclass could be defined which internally maintains the representation in terms of power and coefficient arrays, using compute_monomials() for efficient numerical evaluation.

classmethod from_poly(poly)#

Initializer with consistent signature for all Poly subclasses.

Motivation: Allows conversion to subclasses and also allows easy inheritance of from_sympy and other construction methods. These will first construct a Poly object which is then converted to the right class using this method. In subclasses, it should be overridden if the __init__ signature is changed. Otherwise, the default implementation will work.

classmethod from_sympy(poly, variable_names=None, parameters=None, variable_dim=None, variable_indices=None)#

Construct a Poly object from a sympy/string expression.

Variables should either have integer subscripts or no subscripts.

The function tries to automatically detect variables and parameters appearing in the expression. To make sure this behaves properly, multiple things can be (optionally) specified:

  • The variable names. If given, every other symbol in the expression is interpreted as a parameter.

  • The parameters. If given, every other symbol in the expression is interpreted as a variable.

  • The variable dimensions (must also give variable names in this case). If, for example z0 and z2 appear in the expression but not z1, the latter index is by default not assumed to be part of the z variable. To be sure it is, the dimension can be specified as 3 (or higher).

  • The integer indices for each variable (or None if scalar).

If either the parameters or the variable names are given but not both, all encountered symbols that do not match the given kind are assigned to the other. If both are given, all encountered symbols must be accounted for.

Parameters:
  • poly (Union[str, Expr, Poly]) – Either a string or a sympy expression for the equation of the polynomial.

  • variable_names (Union[Sequence[str], Sequence[ndarray[Any, dtype[Symbol]]], None]) – Sequence of strings giving the names of the input variables of the polynomial. Alternatively, a sequence of numpy arrays containing sympy symbols can be passed. This will fully specify both the variable names and their indices (given by the subscripts of the symbols).

  • parameters (Optional[Sequence[Union[Symbol, str]]]) – Sequence of strings or sympy symbols specifying the (scalar) parameters of the polynomial.

  • variable_dim (Optional[Sequence[int]]) – Integer dimension for each variable. If given, the indices for the variable are set to be 0, 1, ..., dim-1.

  • variable_indices (Optional[Sequence[Optional[Sequence[int]]]]) – Sequence of integer indices (or None if scalar) for each variable.

Returns:

A Poly object representing the given expression as a parametrized polynomial.

property parameter_position: dict[sympy.core.symbol.Symbol, int]#

Mapping between parameter and its index in the ordering.

The ordering is the one the input is expected to be in when evaluating the polynomial.

parameters: Sequence[Symbol]#

Parameters of the polynomial as sequence of sympy symbols. Each parameter is a scalar and the params input when evaluating the polynomial is 1D, specifying their values in the order given here.

property sympy_poly: Poly#

Representation of the polynomial as sympy.Poly object.

Note that all variables are unpacked to be scalars in this form.

to_coeffs_and_powers()#

Extract powers of variables and lists of coefficients.

This is effectively the inverse of Poly.from_coeffs_and_powers().

Return type:

tuple[Sequence[Union[Array, ndarray, bool_, number, float, int]], Sequence[Sequence[Symbol]], Sequence[Sequence[Sequence[int]]]]

transform_eval(**partial_kwargs)#

Generate an efficient numerical evaluation function.

In contrast to calling the polynomial object directly, the inputs to the generated functions can have any leading batch dimensions but must be purely numerical.

unique_patch_index(patch)#

Convert patch indices to single unique integer index.

This is used internally to dispatch calls using affine coordinate inputs to the right affine polynomial.

Return type:

int

Example

If there are three variables with dimensions d1, d2, d3, and we have patch indices i, j, k, the unique integer index is computed as i + d1 * (j + d2 * k). Patch indices range from 0 to dimension-1.

property variable_index_dict: dict[str, dict[int, int]]#

Mappings between positional index and symbolic index of variables.

To represent e.g. the affine coordinates [z0, z2, z3] where z1=1 is omitted, the symbolic indices [0, 2, 3] are stored. For each variable name (e.g. 'z') the output maps between the numeric index of the input variable to the symbolic indices. In the example, the output would be {'z': {0: 0, 1: 2, 2: 3}}.

variable_indices: Sequence[Optional[Sequence[int]]]#

Indices for each variable or None (if scalar).

variable_names: Sequence[str]#

Names of variables of the polynomial. Each can be scalar or vectorial.

property variable_position: dict[str, int]#

Mapping between variable name and its index in the ordering.

The ordering is the one the inputs are expected to be in, if given positionally, when evaluating the polynomial.