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)assign
([variables])Wrapper for the xarray <function Dataset.assign at 0x7fbd830c2430> function for linopy.Variable
assign_attrs
(*args, **kwargs)Wrapper for the xarray <function DataWithCoords.assign_attrs at 0x7fbd83275280> function for linopy.Variable
assign_coords
([coords])Wrapper for the xarray <function DataWithCoords.assign_coords at 0x7fbd832751f0> function for linopy.Variable
astype
(dtype, *[, order, casting, subok, ...])Wrapper for the xarray <function DataWithCoords.astype at 0x7fbd83275820> function for linopy.Variable
bfill
(dim[, limit])Wrapper for the xarray <function Dataset.bfill at 0x7fbd830c2160> function for linopy.Variable
broadcast_like
(other[, exclude])Wrapper for the xarray <function Dataset.broadcast_like at 0x7fbd830bc940> function for linopy.Variable
chunk
([chunks, name_prefix, token, lock, ...])Wrapper for the xarray <function Dataset.chunk at 0x7fbd830bc3a0> 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.
drop
([labels, dim, errors])Wrapper for the xarray <function Dataset.drop at 0x7fbd830c0c10> function for linopy.Variable
drop_isel
([indexers])Wrapper for the xarray <function Dataset.drop_isel at 0x7fbd830c0d30> function for linopy.Variable
drop_sel
([labels, errors])Wrapper for the xarray <function Dataset.drop_sel at 0x7fbd830c0ca0> function for linopy.Variable
empty
()Get whether the linear expression is empty.
equals
(other)ffill
(dim[, limit])Wrapper for the xarray <function Dataset.ffill at 0x7fbd830c20d0> function for linopy.Variable
fillna
(value)Wrapper for the xarray <function Dataset.fillna at 0x7fbd830c0f70> function for linopy.Variable with default arguments: {'value': {'vars': -1, 'coeffs': nan, 'const': 0}}
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 <function Dataset.isel at 0x7fbd830bc5e0> function for linopy.Variable
print
([display_max_rows, display_max_terms])Print the linear expression.
reindex
([indexers, method, tolerance, copy, ...])Wrapper for the xarray <function Dataset.reindex at 0x7fbd830bcaf0> function for linopy.Variable with default arguments: {'fill_value': {'vars': -1, 'coeffs': nan, 'const': 0}}
reindex_like
(other[, method, tolerance, ...])Wrapper for the xarray <function Dataset.reindex_like at 0x7fbd830bca60> function for linopy.Variable with default arguments: {'fill_value': {'vars': -1, 'coeffs': nan, 'const': 0}}
rename
([name_dict])Wrapper for the xarray <function Dataset.rename at 0x7fbd830c0040> function for linopy.Variable
rename_dims
([dims_dict])Wrapper for the xarray <function Dataset.rename_dims at 0x7fbd830c00d0> function for linopy.Variable
reset_const
()Reset the constant of the linear expression to zero.
roll
([shifts, roll_coords])Wrapper for the xarray <function Dataset.roll at 0x7fbd830c2ee0> 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 <function Dataset.sel at 0x7fbd830bc700> function for linopy.Variable
shift
([shifts, fill_value])Wrapper for the xarray <function Dataset.shift at 0x7fbd830c2e50> function for linopy.Variable
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
coords
data
dims
fill_value
flat
Convert the expression to a pandas DataFrame.
indexes
loc
mask
model
ndim
non_helper_dims
Deprecated since version Use: coord_dims instead
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
type
vars