linopy.expressions.LinearExpression.from_tuples

linopy.expressions.LinearExpression.from_tuples#

classmethod LinearExpression.from_tuples(*tuples, model=None)#

Create a linear expression by using tuples of coefficients and variables.

The function internally checks that all variables in the tuples belong to the same reference model.

Parameters:
  • tuples (A list of elements. Each element is either:) –

    • (coefficients, variables)

    • constant

    Each (coefficients, variables) tuple represents one term in the resulting linear expression, which can possibly span over multiple dimensions:

    • coefficientsint/float/array_like

      The coefficient(s) in the term, if the coefficients array contains dimensions which do not appear in the variables, the variables are broadcasted.

    • variablesstr/array_like/linopy.Variable

      The variable(s) going into the term. These may be referenced by name.

    • constant: int/float/array_like

      The constant value to add to the expression

  • model (The linopy.Model. If None this can be inferred from the provided variables)

Returns:

linopy.LinearExpression

Examples

>>> from linopy import Model
>>> import pandas as pd
>>> m = Model()
>>> x = m.add_variables(pd.Series([0, 0]), 1)
>>> y = m.add_variables(4, pd.Series([8, 10]))
>>> expr = LinearExpression.from_tuples((10, x), (1, y), 1)

This is the same as calling 10*x + y + 1 but a bit more performant.