Skip to content

Inconsistent multiplication behavior: Variable vs LinearExpression with subset coordinates #569

@FabianHofmann

Description

@FabianHofmann

Issue Description

Multiplying with a constant that has a subset of coordinates on a shared dimension behaves differently for Variable vs LinearExpression:

  • x * subset_da works (produces NaN coefficients)
  • (1 * x) * subset_da fails with AssertionError

Mathematically these should be equivalent.

Reproducible Example

import pandas as pd
import xarray as xr
import linopy

m = linopy.Model()
x = m.add_variables(coords=[pd.Index(range(5), name="i")])

subset_da = xr.DataArray([10.0, 30.0], dims=["i"], coords={"i": [1, 3]})

# This works
result1 = x * subset_da
print(result1.coeffs.values)  # [nan, 10., nan, 30., nan]

# This fails with AssertionError
expr = 1 * x
result2 = expr * subset_da  # AssertionError in _multiply_by_constant

Expected Behavior

Both operations should behave consistently. Either:

  1. Both succeed with same result, or
  2. Both raise an error indicating misaligned coordinates

Installed Versions

Details

master branch (commit 59f92ae)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions