Status: Accepted
Owner: ----
CC: [email protected]
Labels: Type-Enhancement Priority-Medium
New issue 3158 by [email protected]: Function to sort a substitution
dictionary to remove ambiguity
http://code.google.com/p/sympy/issues/detail?id=3158
See https://github.com/sympy/sympy/pull/690. There should be a way to take
a dictionary and sort it so that when passed to subs, the substitution
happens in order. For example, if you have
(x*y).subs({y: z, z:2}), you might get x*z or 2*x, depending on the order
of the substitution. The function should order the dictionary to give you
the latter order.
The idea is to use a topological sort. The dictionary contains old:new
pairs. Each pair is a node in the graph. Node A points to Node B if Node
A's old contains Node B's new (that is to say, Node A "depends" on Node
B). The topological sort will put the nodes in order so that the
dependencies are handled in order (if possible).
One issue is what is meant by "contains". We could just use .has, but that
only does literal containment, whereas .subs goes beyond that (e.g.,
(x**4).subs(x**2, y) => y**2). So I guess we need a function to check if an
expression "contains" another in the subs sense, that is, expression
A "contains" expression B if A.subs(B, Dummy).has(Dummy). We could just
use that, but we could also probably be more efficient (though maybe not
without a rewrite of the structure of subs, which we need to do anyway).
As for the topological sort, it's already implemented in
sympy/utilities/iterables.py.
Finally, there's the question of whether subs should apply this function
automatically when given a dictionary as input (which has the terms
unordered, so is potentially ambiguous), or if that should be up to the
user to do.
--
You received this message because you are subscribed to the Google Groups
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy-issues?hl=en.