linopy.expressions.LinearExpression.from_tuples

linopy.expressions.LinearExpression.from_tuples#

classmethod LinearExpression.from_tuples(*tuples, model=None, chunk=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 (tuples of (coefficients, variables)) – Each 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.

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

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