Function#
- class Function#
General mathematical function of decision variables.
- __eq__(other: ToFunction) Constraint#
Create an equality constraint: self == other → Constraint with EqualToZero
Returns a Constraint where (self - other) == 0. Note: This does NOT return bool, it creates a Constraint object.
- __ge__(other: ToFunction) Constraint#
Create a greater-than-or-equal constraint: self >= other → Constraint with LessThanOrEqualToZero
Returns a Constraint where (other - self) <= 0.
- __iadd__(rhs: ToFunction) Function#
- __le__(other: ToFunction) Constraint#
Create a less-than-or-equal constraint: self <= other → Constraint with LessThanOrEqualToZero
Returns a Constraint where (self - other) <= 0.
- __new__(inner: ToFunction) Function#
Create a Function from various types.
Accepts:
int or float: creates a constant function
DecisionVariable: creates a linear function with single term
Parameter: creates a linear function with single term
Linear: creates a linear function
Quadratic: creates a quadratic function
Polynomial: creates a polynomial function
Function: returns a copy
- __repr__() str#
- add_assign(rhs: ToFunction) None#
- almost_equal(other: ToFunction, atol: float = 1e-06) bool#
- as_linear() Optional[Linear]#
Try to convert this function to a linear function.
Returns Some(Linear) if the function can be represented as linear, None otherwise. This is useful for checking if a function is suitable for linear programming solvers.
- as_quadratic() Optional[Quadratic]#
Try to convert this function to a quadratic function.
Returns Some(Quadratic) if the function can be represented as quadratic, None otherwise.
- content_factor() float#
- degree() int#
Get the degree of this function.
Returns the highest degree of any term in the function. Zero function has degree 0, constant function has degree 0, linear function has degree 1, quadratic function has degree 2, etc.
- num_terms() int#
Get the number of terms in this function.
Zero function has 0 terms, constant function has 1 term, and polynomial functions have the number of non-zero coefficient terms.
- reduce_binary_power(binary_ids: set[int]) bool#
Reduce binary powers in the function.
For binary variables, \(x^n = x\) for any \(n \geq 1\), so we can reduce higher powers to linear terms.
Args:
binary_ids: Set of binary variable IDs to reduce powers for
Returns:
Trueif any reduction was performed,Falseotherwise
- required_ids() set[int]#
- to_bytes() bytes#
- property constant_term: float#
Read-only property.
Get the constant term of the function.
Returns the constant term. Returns 0.0 if function has no constant term. Works for all polynomial functions by filtering the degree-0 term.
- property linear_terms: dict[int, float]#
Read-only property.
Get linear terms as a dictionary mapping variable id to coefficient.
Returns dictionary mapping variable IDs to their linear coefficients. Returns empty dict if function has no linear terms. Works for all polynomial functions by filtering only degree-1 terms.
- property quadratic_terms: dict[tuple[int, int], float]#
Read-only property.
Get quadratic terms as a dictionary mapping (row, col) to coefficient.
Returns dictionary mapping variable ID pairs to their quadratic coefficients. Returns empty dict if function has no quadratic terms. Works for all polynomial functions by filtering only degree-2 terms.
- property terms: dict#
Read-only property.
- property type_name: str#
Read-only property.