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(dims="dim_0")) <class 'linopy.expressions.LinearExpression'>
- __init__(data, model)#
Methods
__init__
(data, model)add
(other)Add an expression to others.
assign
([variables])Wrapper for the xarray Dataset.assign 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
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)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
empty
()Get whether the linear expression is empty.
equals
(other)ffill
(dim[, limit])Wrapper for the xarray Dataset.ffill function for linopy.Variable
fillna
(value)Fill missing values with a given value.
from_rule
(model, rule, coords)Create a linear expression from a rule and a set of coordinates.
from_tuples
(*tuples[, model, chunk])Create a linear expression by using tuples of coefficients and variables.
groupby
(group[, squeeze, 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.
mul
(other)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.
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
shift
([shifts, fill_value])Wrapper for the xarray Dataset.shift function for linopy.Variable
sub
(other)Subtract others from expression.
sum
([dims, drop_zeros])Sum the expression over all or a subset of dimensions.
to_constraint
(sign, rhs)Convert a linear expression to a constraint.
to_quadexpr
()Convert LinearExpression to QuadraticExpression.
where
(cond[, other])Filter variables based on a condition.
Attributes
attrs
coeffs
const
coord_dims
coord_sizes
coords
data
dims
fill_value
flat
Convert the expression to a pandas DataFrame.
has_constant
indexes
loc
mask
model
ndim
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
solution
Get the optimal values of the expression.
type
vars