linopy.expressions.LinearExpression

linopy.expressions.LinearExpression#

class linopy.expressions.LinearExpression(data, model)#

A linear expression consisting of terms of coefficients and variables.

The LinearExpression class is a subclass of xarray.Dataset which allows to apply most xarray functions on it. However most arithmetic operations are overwritten. Like this you can easily expand and modify the linear expression.

Examples

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

Combining expressions:

>>> expr = 3 * x
>>> type(expr)
<class 'linopy.expressions.LinearExpression'>
>>> other = 4 * y
>>> type(expr + other)
<class 'linopy.expressions.LinearExpression'>

Multiplying:

>>> type(3 * expr)
<class 'linopy.expressions.LinearExpression'>

Summation over dimensions

>>> type(expr.sum(dim="dim_0"))
<class 'linopy.expressions.LinearExpression'>
__init__(data, model)#

Methods

__init__(data, model)

add(other[, join])

Add an expression to others.

assign(**fields)

Wrapper for the xarray assign_multiindex_safe function for linopy.Variable

assign_attrs(*args, **kwargs)

Wrapper for the xarray DataWithCoords.assign_attrs function for linopy.Variable

assign_coords([coords])

Wrapper for the xarray DataWithCoords.assign_coords function for linopy.Variable

assign_multiindex_safe(**fields)

Wrapper for the xarray assign_multiindex_safe function for linopy.Variable

astype(dtype, *[, order, casting, subok, ...])

Wrapper for the xarray DataWithCoords.astype function for linopy.Variable

bfill(dim[, limit])

Wrapper for the xarray Dataset.bfill function for linopy.Variable

broadcast_like(other[, exclude])

Wrapper for the xarray Dataset.broadcast_like function for linopy.Variable

chunk([chunks, name_prefix, token, lock, ...])

Wrapper for the xarray Dataset.chunk function for linopy.Variable

cumsum([dim, skipna, keep_attrs])

Cumulated sum along a given axis.

densify_terms()

Move all non-zero term entries to the front and cut off all-zero entries in the term-axis.

diff(dim[, n])

Calculate the n-th order discrete difference along given axis.

div(other[, join])

Divide the expr by a factor.

dot(other)

Matrix multiplication with other, similar to xarray dot.

drop([labels, dim, errors])

Wrapper for the xarray Dataset.drop function for linopy.Variable

drop_isel([indexers])

Wrapper for the xarray Dataset.drop_isel function for linopy.Variable

drop_sel([labels, errors])

Wrapper for the xarray Dataset.drop_sel function for linopy.Variable

drop_vars(names, *[, errors])

Wrapper for the xarray Dataset.drop_vars function for linopy.Variable

eq(rhs[, join])

Equality constraint.

equals(other)

expand_dims([dim, axis, ...])

Wrapper for the xarray Dataset.expand_dims function for linopy.Variable

ffill(dim[, limit])

Wrapper for the xarray Dataset.ffill function for linopy.Variable

fillna(value)

Fill missing values with a given value.

from_constant(model, constant)

Create a linear expression from a constant value or series

from_rule(model, rule[, coords])

Create a linear expression from a rule and a set of coordinates.

from_tuples(*tuples[, model])

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

ge(rhs[, join])

Greater than or equal constraint.

groupby(group[, restore_coord_dims])

Returns a LinearExpressionGroupBy object for performing grouped operations.

isel([indexers, drop, missing_dims])

Wrapper for the xarray Dataset.isel function for linopy.Variable

isnull()

Get a boolean mask with true values where there is only missing values in an expression.

iterate_slices([slice_size, slice_dims])

Generate slices of an xarray Dataset or DataArray with a specified soft maximum size.

le(rhs[, join])

Less than or equal constraint.

mul(other[, join])

Multiply the expr by a factor.

pow(other)

Power of the expression with a coefficient.

print([display_max_rows, display_max_terms])

Print the linear expression.

reindex([indexers, method, tolerance, copy, ...])

Wrapper for the xarray Dataset.reindex function for linopy.Variable with default arguments: {'fill_value': {'vars': -1, 'coeffs': nan, 'const': nan}}

reindex_like(other[, method, tolerance, ...])

Wrapper for the xarray Dataset.reindex_like function for linopy.Variable with default arguments: {'fill_value': {'vars': -1, 'coeffs': nan, 'const': nan}}

rename([name_dict])

Wrapper for the xarray Dataset.rename function for linopy.Variable

rename_dims([dims_dict])

Wrapper for the xarray Dataset.rename_dims function for linopy.Variable

reset_const()

Reset the constant of the linear expression to zero.

reset_index(dims_or_levels, *[, drop])

Wrapper for the xarray Dataset.reset_index function for linopy.Variable

roll([shifts, roll_coords])

Wrapper for the xarray Dataset.roll function for linopy.Variable

rolling([dim, min_periods, center])

Rolling window object.

sanitize()

Sanitize LinearExpression by ensuring int dtype for variables.

sel([indexers, method, tolerance, drop])

Wrapper for the xarray Dataset.sel function for linopy.Variable

set_index([indexes, append])

Wrapper for the xarray Dataset.set_index function for linopy.Variable

shift([shifts, fill_value])

Wrapper for the xarray Dataset.shift function for linopy.Variable

simplify()

Simplify the linear expression by combining terms with the same variable.

stack([dim, create_index, index_cls])

Wrapper for the xarray Dataset.stack function for linopy.Variable

sub(other[, join])

Subtract others from expression.

sum([dim, drop_zeros])

Sum the expression over all or a subset of dimensions.

swap_dims([dims_dict])

Wrapper for the xarray Dataset.swap_dims function for linopy.Variable

to_constraint(sign, rhs[, join])

Convert a linear expression to a constraint.

to_polars()

Convert the expression to a polars DataFrame.

to_quadexpr()

Convert LinearExpression to QuadraticExpression.

unstack([dim, fill_value, sparse])

Wrapper for the xarray Dataset.unstack function for linopy.Variable

where(cond[, other])

Filter variables based on a condition.

Attributes

attrs

Get the attributes of the expression

coeffs

const

coord_dims

coord_names

coord_sizes

coords

Get the coordinates of the expression

data

dims

empty

Get whether the linear expression is empty.

flat

Convert the expression to a pandas DataFrame.

has_constant

indexes

Get the indexes of the expression

is_constant

True if the expression contains no variables.

loc

mask

model

ndim

Get the number of dimensions.

nterm

Get the number of terms in the linear expression.

shape

Get the total shape of the linear expression.

size

Get the total size of the linear expression.

sizes

Get the sizes of the expression

solution

Get the optimal values of the expression.

type

vars