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: int | float | DecisionVariable | Parameter | Linear) Linear#
- __add__(rhs: Quadratic) Quadratic
- __add__(rhs: Polynomial) Polynomial
- __copy__() 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: int | float) Linear#
- __mul__(rhs: DecisionVariable | Parameter | Linear) Quadratic
- __mul__(rhs: Quadratic | Polynomial) Polynomial
- __neg__() Linear#
Negation operator
- __radd__(lhs: int | float | DecisionVariable | Parameter | Linear) Linear#
- __radd__(lhs: Quadratic) Quadratic
- __radd__(lhs: Polynomial) Polynomial
- __repr__() str#
- __rmul__(lhs: int | float) Linear#
- __rmul__(lhs: DecisionVariable | Parameter | Linear) Quadratic
- __rmul__(lhs: Quadratic | Polynomial) Polynomial
- __rsub__(lhs: int | float | DecisionVariable | Parameter | Linear) Linear#
- __rsub__(lhs: Quadratic) Quadratic
- __rsub__(lhs: Polynomial) Polynomial
- __sub__(rhs: int | float | DecisionVariable | Parameter | Linear) Linear#
- __sub__(rhs: Quadratic) Quadratic
- __sub__(rhs: Polynomial) Polynomial
- add_assign(rhs: Linear) None#
- add_scalar(scalar: float) Linear#
- almost_equal(other: Linear, atol: float = 1e-06) bool#
- constant(constant: float) Linear#
- from_bytes(bytes: bytes) Linear#
- mul_scalar(scalar: float) Linear#
- random(rng: Rng, num_terms: int = 3, max_id: int = 10) Linear#
- single_term(id: int, coefficient: float) Linear#
- terms() dict#
- to_bytes() bytes#
- property constant_term: float#
Read-only property.
- property linear_terms: dict[int, float]#
Read-only property.