Skip to content

Commit 6a6feb1

Browse files
committed
compiler: Enhance ListInitializer
1 parent 648876e commit 6a6feb1

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

devito/symbolics/extended_sympy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,14 @@ class ListInitializer(sympy.Expr, Pickable):
333333
Symbolic representation of the C++ list initializer notation ``{a, b, ...}``.
334334
"""
335335

336-
__rargs__ = ('params',)
336+
__rargs__ = ('*params',)
337337
__rkwargs__ = ('dtype',)
338338

339-
def __new__(cls, params, dtype=None):
339+
def __new__(cls, *params, dtype=None, evaluate=False):
340+
# Legacy API: allow a single list/tuple as argument
341+
if len(params) == 1 and isinstance(params[0], (list, tuple, np.ndarray)):
342+
params = params[0]
343+
340344
args = []
341345
for p in as_tuple(params):
342346
try:

tests/test_symbolics.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,31 @@ def test_namespace():
424424
assert not ns0.free_symbols
425425

426426

427+
def test_list_initializer():
428+
# Legacy interface
429+
init0 = ListInitializer((1, 2, 3))
430+
assert str(init0) == '{1, 2, 3}'
431+
432+
init1 = ListInitializer(1, 2, 3)
433+
assert str(init1) == '{1, 2, 3}'
434+
435+
# Test hashing and equality
436+
assert init0 == init1
437+
assert hash(init0) == hash(init1)
438+
init2 = ListInitializer(1, 2)
439+
assert init0 != init2
440+
assert hash(init0) != hash(init2)
441+
assert hash(init0) == hash(init1)
442+
443+
# Reconstruction
444+
assert init0 == init0._rebuild()
445+
assert init1 == init1._rebuild()
446+
assert str(init1._rebuild(4, 5)) == '{4, 5}'
447+
448+
# Accept `evaluate` but gently ignore it
449+
assert str(ListInitializer((1, 2), evaluate=True)) == '{1, 2}'
450+
451+
427452
def test_rvalue():
428453
ctype = ReservedWord('dummytype')
429454
ns = Namespace(['my', 'namespace'])

0 commit comments

Comments
 (0)