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
AttachedDecisionVariable: creates a linear function with single term (only the id is used; no host borrow is taken)
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.
- evaluate_bound(bounds: Mapping[int, Bound]) Bound#
Compute an interval bound of this function given variable bounds.
Missing IDs in
boundsare treated as unbounded (Bound.unbounded()).Args:
bounds: Mapping from variable ID to itsBound.
Returns: A
Boundthat contains \([\inf f, \sup f]\) over the given variable bounds.Tightness: This evaluates the bound term by term (monomial-wise) and sums the per-term intervals. The result is a sound over-approximation of the true range \([\inf f, \sup f]\) but is not guaranteed to be tight, because it ignores dependencies between terms that share variables. For example, \(f = x^2 - x\) with \(x \in [0, 1]\) has true range \([-1/4, 0]\) (minimum at \(x = 1/2\)), but term-wise evaluation yields \([0, 1] + (-[0, 1]) = [-1, 1]\).
Examples#
>>> from ommx.v1 import Function, Linear, Bound >>> f = Function(Linear(terms={1: 2}, constant=3)) # 2*x1 + 3 >>> b = f.evaluate_bound({1: Bound(0.0, 2.0)}) >>> (b.lower, b.upper) (3.0, 7.0)
- 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]#
- 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.