Linear

Linear#

class Linear#

Linear function of decision variables.

A linear function has the form: \(c_0 + \sum_i c_i x_i\) where \(x_i\) are decision variables and \(c_i\) are coefficients.

Examples#

Create a linear function f(x₁, x₂) = 2x₁ + 3x₂ + 1:

>>> f = Linear(terms={1: 2, 2: 3}, constant=1)

Or create via DecisionVariable arithmetic:

>>> x1 = DecisionVariable.integer(1)
>>> x2 = DecisionVariable.integer(2)
>>> g = 2*x1 + 3*x2 + 1

Compare two linear functions with tolerance:

>>> f.almost_equal(g, atol=1e-12)
True

Note that == creates an equality Constraint, not a boolean:

>>> constraint = f == g  # Returns Constraint, not bool
__add__(rhs: ScalarLike | LinearLike | Parameter) → Linear#
__add__(rhs: Quadratic) → Quadratic
__add__(rhs: Polynomial) → Polynomial
__add__(rhs: Function) → Function
__copy__() → Linear#
__deepcopy__(_memo: Any) → Linear#
__eq__(other: ToFunction) → Constraint#

Create an equality constraint: self == other → Constraint with EqualToZero

__ge__(other: ToFunction) → Constraint#

Create a greater-than-or-equal constraint: self >= other → Constraint

__iadd__(rhs: Linear) → Linear#
__le__(other: ToFunction) → Constraint#

Create a less-than-or-equal constraint: self <= other → Constraint

__mul__(rhs: ScalarLike) → Linear#
__mul__(rhs: LinearLike | Parameter) → Quadratic
__mul__(rhs: Quadratic | Polynomial) → Polynomial
__mul__(rhs: Function) → Function
__neg__() → Linear#

Negation operator

__new__(terms: Mapping[int, float], constant: float = 0.0) → Linear#
__radd__(lhs: ScalarLike | LinearLike | Parameter) → Linear#
__radd__(lhs: Quadratic) → Quadratic
__radd__(lhs: Polynomial) → Polynomial
__radd__(lhs: Function) → Function
__repr__() → str#
__rmul__(lhs: ScalarLike) → Linear#
__rmul__(lhs: LinearLike | Parameter) → Quadratic
__rmul__(lhs: Quadratic | Polynomial) → Polynomial
__rmul__(lhs: Function) → Function
__rsub__(lhs: ScalarLike | LinearLike | Parameter) → Linear#
__rsub__(lhs: Quadratic) → Quadratic
__rsub__(lhs: Polynomial) → Polynomial
__rsub__(lhs: Function) → Function
__sub__(rhs: ScalarLike | LinearLike | Parameter) → Linear#
__sub__(rhs: Quadratic) → Quadratic
__sub__(rhs: Polynomial) → Polynomial
__sub__(rhs: Function) → Function
add_assign(rhs: Linear) → None#
add_scalar(scalar: float) → Linear#
almost_equal(other: Linear, atol: float = 1e-06) → bool#
constant(constant: float) → Linear#
evaluate(state: ToState, *, atol: Optional[float] = None) → float#
mul_scalar(scalar: float) → Linear#
partial_evaluate(state: ToState, *, atol: Optional[float] = None) → Linear#
random(rng: Rng, num_terms: int = 3, max_id: int = 10) → Linear#
single_term(id: int, coefficient: float) → Linear#
terms() → dict#
property constant_term: float#

Read-only property.

property linear_terms: dict[int, float]#

Read-only property.