File tree Expand file tree Collapse file tree 1 file changed +15
-3
lines changed
python/sdist/amici/importers Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -399,6 +399,7 @@ def smart_subs_dict(
399399 sym : sp .Expr ,
400400 subs : SymbolDef ,
401401 field : str | None = None ,
402+ reverse : bool = True ,
402403) -> sp .Expr :
403404 """
404405 Substitutes expressions completely flattening them out. Requires
@@ -413,16 +414,27 @@ def smart_subs_dict(
413414 :param field:
414415 Field of substitution expressions in subs.values(), if applicable
415416
417+ :param reverse:
418+ Whether ordering in subs should be reversed. Note that substitution
419+ requires the reverse order of what is required for evaluation.
420+
416421 :return:
417422 Substituted symbolic expression
418423 """
419424 if field is None :
420- s = { eid : expr for eid , expr in subs .items ()}
425+ s = [( eid , expr ) for eid , expr in subs .items ()]
421426 else :
422- s = {eid : expr [field ] for eid , expr in subs .items ()}
427+ s = [(eid , expr [field ]) for eid , expr in subs .items ()]
428+
429+ if reverse :
430+ s .reverse ()
423431
424432 with sp .evaluate (False ):
425- sym = sym .subs (s , simultaneous = 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 })
426438
427439 return sym
428440
You can’t perform that action at this time.
0 commit comments