Poly¶
- class Poly¶
The polynomial class used for representing objective functions and constraints.
Methods
__init__
Return the dictionary representation of the polynomial.
Return the variable representation of the polynomial.
alias of
as_dict()Equivalent to
evaluate().Return the degree of the polynomial.
Evaluate the polynomial by substituting variables using the given values.
Return
Trueif the polynomial is linear.Return
Trueif the polynomial is a constant polynomial.Return
Trueif the polynomial is quadratic.Return
Trueif the polynomial represents a single variable.Assign numbers or other polynomials to variables in the polynomial.
Attributes
Variable ID.
Lower bound of the variable.
Variable name.
Variable type.
Upper bound of the variable.
List of all variables in the polynomial.
- __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
Polyfor a polynomial or a number, aPolyArrayfor an array, and aModelfor aConstraintor aConstraintList.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_1is equivalent toq_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:
- __eq__(self, arg: object, /) bool¶
Tell whether the two sides are the same polynomial.
This compares the terms, so it gives a
booland not a constraint. Useequal_to()to make a constraint that asks for equality.
- __float__(self) float¶
Cast a polynomial to a float value. Only valid for constant polynomials.
- Return type:
- Raises:
ValueError – The polynomial is not a constant.
- __index__(self) int¶
Cast a polynomial to an integer. Only valid for constant polynomials.
- Return type:
- 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:
- 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_0is equivalent to1 - q_0.- Return type:
- __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:
- Raises:
ValueError – The exponent is negative.
- __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 + 5is iterated as((Poly(x_0), Poly(x_1)), 3.0),((Poly(x_0),), -2.0), and((), 5.0).- Return type:
- __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 abooland not a constraint.
- __neg__(self) Poly¶
Give the polynomial in which every coefficient has the opposite sign.
- Return type:
- __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_1is 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]:
- __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:
- 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
Polyfor a polynomial or a number, aPolyArrayfor an array, and aModelfor aConstraintor aConstraintList.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_1is equivalent toq_0 * q_1.Overload 1.
- Args:
arg (bool)
- Returns:
amplify.Poly:
Overload 2.
- Args:
arg (ndarray)
- Returns:
amplify.PolyArray[Dim]:
- __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_1is 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_1is 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]:
- __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_1is 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 + 5will be represented as{(): 5, (0,): 3, (0, 1): -2}.
- as_variable(self) Variable¶
Return the variable representation of the polynomial. Only valid for polynomials that represent a single variable.
- Return type:
- Raises:
RuntimeError – The polynomial is not a single variable.
- 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:
- 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
defaultis absent, a variable thatvaluesdoes 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
0when its bounds contain0, the upper bound when they do not, and the lower bound when it has no upper bound.
If the keyword argument
defaultis specified with a float value, variables that are not specified invalueswill be substituted by that value.If the keyword argument
defaultis specified withNone, variables that are not specified invalueswill 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:
valuescomes from anotherVariableGeneratorthan 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
Noneto keep variables not specified invaluessymbolic.
- Returns:
amplify.Poly:
- property id¶
Variable ID. Only valid for polynomials that represent a single variable.
- Return type:
- 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:
- Raises:
RuntimeError – The polynomial is not a single variable.
- property type¶
Variable type. Only valid for polynomials that represent a single variable.
- Return type:
- 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.