cyjax.polynomial.Poly#

class cyjax.polynomial.Poly(sympy_poly, variable_names, parameters, variable_indices)#

Bases: object

__init__(sympy_poly, variable_names, parameters, variable_indices)#

Parametrized polynomial class.

Instead of using the default constructor, it is typically safer and simpler to use one of the following two class methods:

Methods

__init__(sympy_poly, variable_names, ...)

Parametrized polynomial class.

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.

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.

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.

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.

Return type:

Poly

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.

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.