Skip to content

Commit 792d158

Browse files
authored
fix: empty for linear expressions with terms (#424)
1 parent fa1ffbe commit 792d158

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

linopy/expressions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
import functools
1111
import logging
12-
from collections.abc import Hashable, Iterator, Mapping, Sequence
12+
from collections.abc import Callable, Hashable, Iterator, Mapping, Sequence
1313
from dataclasses import dataclass, field
1414
from itertools import product, zip_longest
1515
from typing import (
1616
TYPE_CHECKING,
1717
Any,
18-
Callable,
1918
)
2019
from warnings import warn
2120

@@ -1352,7 +1351,7 @@ def empty(self) -> bool:
13521351
"""
13531352
Get whether the linear expression is empty.
13541353
"""
1355-
return self.shape == (0,)
1354+
return not self.size
13561355

13571356
def densify_terms(self) -> LinearExpression:
13581357
"""

test/test_linear_expression.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,12 @@ def test_linear_expression_loc(x: Variable, y: Variable) -> None:
539539
assert expr.loc[0].size < expr.loc[:5].size
540540

541541

542+
def test_linear_expression_empty(v: Variable) -> None:
543+
expr = 7 * v
544+
assert not expr.empty()
545+
assert expr.loc[[]].empty()
546+
547+
542548
def test_linear_expression_isnull(v: Variable) -> None:
543549
expr = np.arange(20) * v
544550
filter = (expr.coeffs >= 10).any(TERM_DIM)

0 commit comments

Comments
 (0)