Skip to content

Commit 2732898

Browse files
authored
Faster substitutions with smart_subs_dict (#3025)
* Disable evaluation during substitution * Use xreplace instead of subs For my current benchmark, this reduces the time for one `smart_subs_dict` call from over 21h (still running...) to ~12min. Disabling evaluation during substitution may prevent simplification of some expressions during substitution, but those can be simplified later on.
1 parent d20f97b commit 2732898

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

python/sdist/amici/importers/utils.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,21 @@ def smart_subs_dict(
421421
:return:
422422
Substituted symbolic expression
423423
"""
424-
s = [
425-
(eid, expr[field] if field is not None else expr)
426-
for eid, expr in subs.items()
427-
]
424+
if field is None:
425+
s = [(eid, expr) for eid, expr in subs.items()]
426+
else:
427+
s = [(eid, expr[field]) for eid, expr in subs.items()]
428+
428429
if reverse:
429430
s.reverse()
430-
for substitution in s:
431-
# note that substitution may change free symbols, so we have to do
432-
# this recursively
433-
if sym.has(substitution[0]):
434-
sym = sym.subs(*substitution)
431+
432+
with sp.evaluate(False):
433+
for old, new in s:
434+
# note that substitution may change free symbols, so we have to do
435+
# this recursively
436+
if sym.has(old):
437+
sym = sym.xreplace({old: new})
438+
435439
return sym
436440

437441

0 commit comments

Comments
 (0)