Skip to content

Commit ddf07ef

Browse files
committed
compiler: Enhance ListInitializer
1 parent 055db03 commit ddf07ef

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

devito/symbolics/extended_sympy.py

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

337-
__rargs__ = ('params',)
337+
__rargs__ = ('*params',)
338338
__rkwargs__ = ('dtype',)
339339

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

tests/test_symbolics.py

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

430430

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

0 commit comments

Comments
 (0)