-
Notifications
You must be signed in to change notification settings - Fork 72
Open
Description
Version Checks
- I have confirmed this bug exists on the current
masterbranch of Linopy.
Issue Description
Multiplying variables/expressions with constants that have a subset of coordinates produces inconsistent behavior:
Variable * subset_const: Creates NaN coefficients with valid variable labelsLinearExpression * subset_const: Fails with AssertionError in_multiply_by_constantVariable + subset_const: Works correctly (fills missing with 0)
The root cause is that addition uses merge() with controlled join='outer' and fill_value=0, while multiplication uses uncontrolled xarray operations.
Reproducible Example
import xarray as xr
import pandas as pd
import linopy
m = linopy.Model()
x = m.add_variables(coords=[pd.Index(range(5), name='i')])
subset = xr.DataArray([10.0, 30.0], dims=['i'], coords={'i': [1, 3]})
# Works but produces NaN coefficients with valid var labels
result1 = x * subset
print(result1.coeffs.squeeze().values) # [nan, 10., nan, 30., nan]
print(result1.data.vars.squeeze().values) # [0, 1, 2, 3, 4] - all valid!
# Fails with AssertionError
expr = 1 * x
result2 = expr * subset # AssertionError in _multiply_by_constant
# Addition works correctly
result3 = x + subset
print(result3.const.values) # [0., 10., 0., 30., 0.]Expected Behavior
Multiplication should behave consistently:
- Either fill missing coordinates with 0 (like addition)
- Or raise an error for misaligned coordinates
- Both
x * cand(1*x) * cshould behave the same
Installed Versions
Details
linopy @ master (commit 59f92ae)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels