Function

Function#

class Function#

General mathematical function of decision variables.

__abs__() Function#

Absolute value operator

__add__(rhs: ToFunction) Function#

Addition

__copy__() Function#
__deepcopy__(_memo: Any) Function#
__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.

__mul__(rhs: ToFunction) Function#

Multiplication

__neg__() Function#

Negation operator

__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#
__radd__(lhs: ToFunction) Function#

Reverse addition (lhs + self)

__repr__() str#
__rmul__(lhs: ToFunction) Function#

Reverse multiplication (lhs * self)

__rsub__(lhs: ToFunction) Function#

Reverse subtraction (lhs - self)

__rtruediv__(lhs: ToFunction) Function#

Reverse division (lhs / self)

__sub__(rhs: ToFunction) Function#

Subtraction

__truediv__(rhs: ToFunction) Function#

Division

add_assign(rhs: ToFunction) None#
add_linear(linear: Linear) Function#
add_polynomial(polynomial: Polynomial) Function#
add_quadratic(quadratic: Quadratic) Function#
add_scalar(scalar: float) Function#
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 TypeError for 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(state: ToState, *, atol: Optional[float] = None) float#
evaluate_bound(bounds: Mapping[int, Bound], *, atol: Optional[float] = None) Bound#

Compute an interval bound of this function given variable bounds.

Missing IDs in bounds are treated as unbounded (Bound.unbounded()).

Args:

  • bounds: Mapping from variable ID to its Bound.

  • atol: Absolute tolerance used by operations whose semantics depend on whether a value is zero. If omitted, DEFAULT_ATOL is used. Use the same tolerance when point-evaluating this function.

Returns: A Bound that 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: RuntimeError when an interval contains a value treated as zero by atol in a denominator or as the base of a negative integer power. Raises ValueError if 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
from_linear(linear: Linear) Function#
from_polynomial(polynomial: Polynomial) Function#
from_quadratic(quadratic: Quadratic) Function#
from_scalar(scalar: float) Function#
maximum(rhs: ToFunction) Function#

Pointwise maximum

minimum(rhs: ToFunction) Function#

Pointwise minimum

mul_linear(linear: Linear) Function#
mul_polynomial(polynomial: Polynomial) Function#
mul_quadratic(quadratic: Quadratic) Function#
mul_scalar(scalar: float) Function#
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.

partial_evaluate(state: ToState, *, atol: Optional[float] = None) Function#
powi(exponent: int) Function#

Raise this function to a signed 32-bit integer power.

random(rng: Rng, num_terms: int = 5, max_degree: int = 3, max_id: int = 10) Function#
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: True if any reduction was performed, False otherwise

required_ids() set[int]#
signum() Function#

Sign of the function value

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.