Function

Function#

class Function#

General mathematical function of decision variables.

__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

  • 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

__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)

__sub__(rhs: ToFunction) Function#

Subtraction

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#
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(state: ToState, atol: Optional[float] = None) float#
from_bytes(bytes: bytes) Function#
from_linear(linear: Linear) Function#
from_polynomial(polynomial: Polynomial) Function#
from_quadratic(quadratic: Quadratic) Function#
from_scalar(scalar: float) Function#
mul_linear(linear: Linear) Function#
mul_polynomial(polynomial: Polynomial) Function#
mul_quadratic(quadratic: Quadratic) Function#
mul_scalar(scalar: float) Function#
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.

partial_evaluate(state: ToState, atol: Optional[float] = None) Function#
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]#
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.