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
- __pow__(exponent: int, modulo: None = None) Function#
- __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#
Return the minimal positive factor that makes all coefficients integers.
Raises
TypeErrorfor a composed, non-polynomial Function.
- degree() Optional[int]#
Get the degree of this function.
Returns the highest degree of any term in a polynomial function. Zero function has degree 0, constant function has degree 0, linear function has degree 1, quadratic function has degree 2, etc. Returns None for non-polynomial expression functions.
- evaluate_bound(bounds: Mapping[int, Bound], *, atol: Optional[float] = None) 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.atol: Absolute tolerance used by operations whose semantics depend on whether a value is zero. If omitted,DEFAULT_ATOLis used. Use the same tolerance when point-evaluating this function.
Returns: A
Boundthat contains \([\inf f, \sup f]\) over the given variable bounds.Tightness: Polynomial leaves are bounded term by term (monomial-wise), and composed expression operations combine their operand bounds with interval arithmetic. 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 or operands 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]\).
Raises:
RuntimeErrorwhen an interval contains a value treated as zero byatolin a denominator or as the base of a negative integer power. RaisesValueErrorif valid bound endpoints cannot be constructed after numeric overflow.Examples#
>>> from ommx 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 <= 3.0 and b.upper >= 7.0 True
- num_terms() Optional[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. Returns None for non-polynomial expression functions.
- 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. Raises TypeError if this is a non-polynomial expression function.
- 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. Raises TypeError if this is a non-polynomial expression function.
- 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. Raises TypeError if this is a non-polynomial expression function.
- property terms: dict#
Read-only property.
Get all polynomial terms as a dictionary mapping monomial tuples to coefficients.
Raises TypeError if this is a non-polynomial expression function.
- property type_name: str#
Read-only property.