PolyΒΆ

class PolyΒΆ

The polynomial class used for representing objective functions and constraints.

Methods

__init__

__init__

as_dict

Return the dictionary representation of the polynomial.

as_variable

Return the variable representation of the polynomial.

asdict

alias of as_dict()

decode

Equivalent to evaluate().

degree

Return the degree of the polynomial.

evaluate

Evaluate the polynomial by substituting variables using the given values.

is_linear

Return True if the polynomial is linear.

is_number

Return True if the polynomial is a constant polynomial.

is_quadratic

Return True if the polynomial is quadratic.

is_variable

Return True if the polynomial represents a single variable.

substitute

Assign numbers or other polynomials to variables in the polynomial.

Attributes

id

Variable ID.

lower_bound

Lower bound of the variable.

name

Variable name.

type

Variable type.

upper_bound

Upper bound of the variable.

variables

List of all variables in the polynomial.

class IteratorΒΆ
__init__(*args, **kwargs)ΒΆ
__iter__(self) → IteratorΒΆ
Return type:

Iterator

__next__(self) → tuple[tuple[Poly, ...], float]ΒΆ
Return type:

tuple[tuple[Poly, …], float]

__add__(self, arg: Poly | float, /) → PolyΒΆ
__add__(self, arg: ndarray, /) → PolyArray[Dim]
__add__(self, arg: Constraint | ConstraintList, /) → Model

Add a polynomial, a number, an array, or a constraint to the polynomial.

The result is a Poly for a polynomial or a number, a PolyArray for an array, and a Model for a Constraint or a ConstraintList.

Overload 1.

Args:
  • arg (amplify.Poly | float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

Overload 3.

Args:
  • arg (amplify.Constraint | amplify.ConstraintList)

Returns:

amplify.Model:

__and__(self, arg: Poly | bool, /) → PolyΒΆ
__and__(self, arg: ndarray[dtype=bool], /) → PolyArray[Dim]

Logical AND of two polynomials. This is meaningful only for polynomials that take values 0 or 1.

q_0 & q_1 is equivalent to q_0 * q_1.

Overload 1.

Args:
  • arg (amplify.Poly | bool)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__bool__(self) → boolΒΆ

Cast a polynomial to a boolean value. Only valid for constant polynomials that take values 0 or 1.

Return type:

bool

__eq__(self, arg: object, /) → boolΒΆ

Tell whether the two sides are the same polynomial.

This compares the terms, so it gives a bool and not a constraint. Use equal_to() to make a constraint that asks for equality.

Parameters:

arg (object)

Return type:

bool

__float__(self) → floatΒΆ

Cast a polynomial to a float value. Only valid for constant polynomials.

Return type:

float

Raises:

ValueError – The polynomial is not a constant.

__iadd__(self, arg: Poly | float, /) → PolyΒΆ
Parameters:

arg (Poly | float)

Return type:

Poly

__iand__(self, arg: Poly | bool, /) → PolyΒΆ
Parameters:

arg (Poly | bool)

Return type:

Poly

__imul__(self, arg: Poly | float, /) → PolyΒΆ
Parameters:

arg (Poly | float)

Return type:

Poly

__index__(self) → intΒΆ

Cast a polynomial to an integer. Only valid for constant polynomials.

Return type:

int

Raises:

ValueError – The polynomial is not a constant.

__init__(self) → NoneΒΆ
__init__(self, arg: float, /) → None
__init__(self, arg: Variable, /) → None

__init__

Overload 2.

Args:
  • arg (float)

Overload 3.

Args:
  • arg (amplify.Variable)

__int__(self) → intΒΆ

Cast a polynomial to an integer. Only valid for constant polynomials.

Return type:

int

Raises:

ValueError – The polynomial is not a constant.

__invert__(self) → PolyΒΆ

Logical NOT of a polynomial. This is meaningful only for polynomials that take values 0 or 1.

~q_0 is equivalent to 1 - q_0.

Return type:

Poly

__ior__(self, arg: Poly | bool, /) → PolyΒΆ
Parameters:

arg (Poly | bool)

Return type:

Poly

__ipow__(self, arg: int, /) → PolyΒΆ

Raise the polynomial to a power in place. The exponent must be an integer that is 0 or larger.

Parameters:

arg (int)

Return type:

Poly

Raises:

ValueError – The exponent is negative.

__isub__(self, arg: Poly | float, /) → PolyΒΆ
Parameters:

arg (Poly | float)

Return type:

Poly

__iter__(self) → IteratorΒΆ

Iterate over the terms in the polynomial.

Each term is represented as a tuple of a tuple of variables and a coefficient. For example, the polynomial 3 * x_0 * x_1 - 2 * x_0 + 5 is iterated as ((Poly(x_0), Poly(x_1)), 3.0), ((Poly(x_0),), -2.0), and ((), 5.0).

Return type:

Iterator

__itruediv__(self, arg: float, /) → PolyΒΆ
Parameters:

arg (float)

Return type:

Poly

__ixor__(self, arg: Poly | bool, /) → PolyΒΆ
Parameters:

arg (Poly | bool)

Return type:

Poly

__len__(self) → intΒΆ

Return the number of terms in the polynomial.

Return type:

int

__mul__(self, arg: Poly | float, /) → PolyΒΆ
__mul__(self, arg: ndarray, /) → PolyArray[Dim]

Multiply the polynomial by a polynomial or by a number.

Overload 1.

Args:
  • arg (amplify.Poly | float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__ne__(self, arg: object, /) → boolΒΆ

Tell whether the two sides are different polynomials.

This is the negation of ==. It gives a bool and not a constraint.

Parameters:

arg (object)

Return type:

bool

__neg__(self) → PolyΒΆ

Give the polynomial in which every coefficient has the opposite sign.

Return type:

Poly

__or__(self, arg: Poly | bool, /) → PolyΒΆ
__or__(self, arg: ndarray[dtype=bool], /) → PolyArray[Dim]

Logical OR of two polynomials. This is meaningful only for polynomials that take values 0 or 1.

q_0 | q_1 is equivalent to -q_0 * q_1 + q_0 + q_1.

Overload 1.

Args:
  • arg (amplify.Poly | bool)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__pos__(self) → PolyΒΆ

Give a copy of the polynomial.

Return type:

Poly

__pow__(self, arg: int, /) → PolyΒΆ

Raise the polynomial to a power. The exponent must be an integer that is 0 or larger.

Parameters:

arg (int)

Return type:

Poly

Raises:

ValueError – The exponent is negative.

__radd__(self, arg: float, /) → PolyΒΆ
__radd__(self, arg: ndarray, /) → PolyArray[Dim]
__radd__(self, arg: Constraint | ConstraintList, /) → Model

Add a polynomial, a number, an array, or a constraint to the polynomial.

The result is a Poly for a polynomial or a number, a PolyArray for an array, and a Model for a Constraint or a ConstraintList.

Overload 1.

Args:
  • arg (float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

Overload 3.

Args:
  • arg (amplify.Constraint | amplify.ConstraintList)

Returns:

amplify.Model:

__rand__(self, arg: bool, /) → PolyΒΆ
__rand__(self, arg: ndarray[dtype=bool], /) → PolyArray[Dim]

Logical AND of two polynomials. This is meaningful only for polynomials that take values 0 or 1.

q_0 & q_1 is equivalent to q_0 * q_1.

Overload 1.

Args:
  • arg (bool)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__repr__(self) → strΒΆ
Return type:

str

__rmul__(self, arg: float, /) → PolyΒΆ
__rmul__(self, arg: ndarray, /) → PolyArray[Dim]

Multiply the polynomial by a polynomial or by a number.

Overload 1.

Args:
  • arg (float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__ror__(self, arg: bool, /) → PolyΒΆ
__ror__(self, arg: ndarray[dtype=bool], /) → PolyArray[Dim]

Logical OR of two polynomials. This is meaningful only for polynomials that take values 0 or 1.

q_0 | q_1 is equivalent to -q_0 * q_1 + q_0 + q_1.

Overload 1.

Args:
  • arg (bool)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__rsub__(self, arg: float, /) → PolyΒΆ
__rsub__(self, arg: ndarray, /) → PolyArray[Dim]

Subtract the polynomial from a number.

Overload 1.

Args:
  • arg (float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__rxor__(self, arg: bool, /) → PolyΒΆ
__rxor__(self, arg: ndarray[dtype=bool], /) → PolyArray[Dim]

Logical XOR of two polynomials. This is meaningful only for polynomials that take values 0 or 1.

q_0 ^ q_1 is equivalent to -2 * q_0 * q_1 + q_0 + q_1.

Overload 1.

Args:
  • arg (bool)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__str__(self) → strΒΆ
Return type:

str

__sub__(self, arg: Poly | float, /) → PolyΒΆ
__sub__(self, arg: ndarray, /) → PolyArray[Dim]

Subtract a polynomial or a number from the polynomial.

Overload 1.

Args:
  • arg (amplify.Poly | float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__truediv__(self, arg: float, /) → PolyΒΆ
__truediv__(self, arg: ndarray, /) → PolyArray[Dim]

Divide the polynomial by a number.

Overload 1.

Args:
  • arg (float)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

__xor__(self, arg: Poly | bool, /) → PolyΒΆ
__xor__(self, arg: ndarray[dtype=bool], /) → PolyArray[Dim]

Logical XOR of two polynomials. This is meaningful only for polynomials that take values 0 or 1.

q_0 ^ q_1 is equivalent to -2 * q_0 * q_1 + q_0 + q_1.

Overload 1.

Args:
  • arg (amplify.Poly | bool)

Returns:

amplify.Poly:

Overload 2.

Args:
  • arg (ndarray)

Returns:

amplify.PolyArray[Dim]:

as_dict(self) → dict[tuple[int, ...], float]ΒΆ

Return the dictionary representation of the polynomial.

The polynomial is represented as a dictionary where keys are tuples of variable indices and values are coefficients. For example, the polynomial 3 * x_0 - 2 * x_0 * x_1 + 5 will be represented as {(): 5, (0,): 3, (0, 1): -2}.

Return type:

dict[tuple[int, …], float]

as_variable(self) → VariableΒΆ

Return the variable representation of the polynomial. Only valid for polynomials that represent a single variable.

Return type:

Variable

Raises:

RuntimeError – The polynomial is not a single variable.

asdict(self) → dict[tuple[int, ...], float]ΒΆ

alias of as_dict()

Return type:

dict[tuple[int, …], float]

decode(self, values: Values) → floatΒΆ
decode(self, values: Values, default: float) → float
decode(self, values: Values, default: None) → Poly

Equivalent to evaluate().

This method is deprecated since Amplify v1.0.0 and will be removed in a future version. Please see the migration guide for details: https://amplify.fixstars.com/docs/amplify/v1/migration.html

Overload 1.

Args:
  • values (amplify.Values)

Returns:

float:

Overload 2.

Args:
  • values (amplify.Values)

  • default (float)

Returns:

float:

Overload 3.

Args:
  • values (amplify.Values)

  • default (None)

Returns:

amplify.Poly:

degree(self) → intΒΆ

Return the degree of the polynomial.

Return type:

int

evaluate(self, values: Values) → floatΒΆ
evaluate(self, values: Values, default: float) → float
evaluate(self, values: Values, default: None) → Poly

Evaluate the polynomial by substituting variables using the given values.

This method is usually used to obtain optimization results in array form.

When the keyword argument default is absent, a variable that values does not contain takes the default value of its type:

  • a binary variable takes 0;

  • an Ising variable takes -1;

  • an integer or a real variable takes 0 when its bounds contain 0, the upper bound when they do not, and the lower bound when it has no upper bound.

If the keyword argument default is specified with a float value, variables that are not specified in values will be substituted by that value.

If the keyword argument default is specified with None, variables that are not specified in values will remain as symbolic variables without substitution.

Overload 1.

Args:
  • values (amplify.Values): Values obtained from optimization results, e.g. result.best.values.

Returns:

float:

Raises:

ValueError: values comes from another VariableGenerator than the one that made the variables of this polynomial.

Overload 2.

Args:
  • values (amplify.Values): Values that can be obtained from optimization results, e.g. result.best.values.

  • default (float): The default value for variables not specified in values.

Returns:

float:

Overload 3.

Args:
  • values (amplify.Values): Values obtained from optimization results, e.g. result.best.values.

  • default (None): Set to None to keep variables not specified in values symbolic.

Returns:

amplify.Poly:

is_linear(self) → boolΒΆ

Return True if the polynomial is linear.

Return type:

bool

is_number(self) → boolΒΆ

Return True if the polynomial is a constant polynomial.

Return type:

bool

is_quadratic(self) → boolΒΆ

Return True if the polynomial is quadratic.

Return type:

bool

is_variable(self) → boolΒΆ

Return True if the polynomial represents a single variable.

Return type:

bool

substitute(
self,
mapping: collections.abc.Mapping[Poly, Poly | float | int] | collections.abc.Mapping[Variable, Poly | float | int],
) → PolyΒΆ

Assign numbers or other polynomials to variables in the polynomial.

Parameters:

mapping (collections.abc.Mapping[Poly, Poly | float | int] | collections.abc.Mapping[Variable, Poly | float | int]) – Key-value pairs of variables and replacement values. Each value can be a number or another polynomial.

Return type:

Poly

property idΒΆ

Variable ID. Only valid for polynomials that represent a single variable.

Return type:

int

Raises:

RuntimeError – The polynomial is not a single variable.

property lower_boundΒΆ

Lower bound of the variable. Only valid for polynomials that represent a single variable.

Return type:

float | None

Raises:

RuntimeError – The polynomial is not a single variable.

property nameΒΆ

Variable name. Only valid for polynomials that represent a single variable.

Return type:

str

Raises:

RuntimeError – The polynomial is not a single variable.

property typeΒΆ

Variable type. Only valid for polynomials that represent a single variable.

Return type:

VariableType

Raises:

RuntimeError – The polynomial is not a single variable.

property upper_boundΒΆ

Upper bound of the variable. Only valid for polynomials that represent a single variable.

Return type:

float | None

Raises:

RuntimeError – The polynomial is not a single variable.

property variablesΒΆ

List of all variables in the polynomial.

Return type:

list[Variable]