Add sorted_args and fix the sort keys for some unordered collections. #1446

Closed
wants to merge 5 commits into
from
View
14 sympy/core/basic.py
@@ -320,11 +320,12 @@ def sort_key(self, order=None):
# XXX: remove this when issue #2070 is fixed
def inner_key(arg):
if isinstance(arg, Basic):
- return arg.sort_key()
+ return arg.sort_key(order)
else:
return arg
- args = len(self.args), tuple([ inner_key(arg) for arg in self.args ])
+ args = self._sorted_args
@smichr
SymPy member
smichr added a line comment Aug 1, 2012

I would leave this as self.args otherwise you have to go to the method to find out what is gives you -- in this case, unsorted args (and I think that's confusing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ args = len(args), tuple([ inner_key(arg) for arg in args ])
return self.class_key(), args, S.One.sort_key(), S.One
def __eq__(self, other):
@@ -631,6 +632,15 @@ def args(self):
"""
return self._args
+ @property
+ def _sorted_args(self):
+ """
+ The same as ``args``. Derived classes which don't fix an
+ order on their arguments should override this method to
+ produce the sorted representation.
+ """
+ return self.args
+
def iter_basic_args(self):
"""
Iterates arguments of ``self``.
View
5 sympy/core/containers.py
@@ -204,3 +204,8 @@ def __contains__(self, key):
def __lt__(self, other):
return self.args < other.args
+
+ @property
+ def _sorted_args(self):
+ from sympy.utilities import default_sort_key
+ return sorted(self.args, key=default_sort_key)
View
12 sympy/core/sets.py
@@ -667,9 +667,8 @@ def as_relational(self, symbol):
def set_sort_fn(s):
@smichr
SymPy member
smichr added a line comment Aug 1, 2012

it seems like this should be called set_sort_key

@smichr
SymPy member
smichr added a line comment Aug 1, 2012

actaully, shouldn't this be defined in Set as sort_key? and then the function can be deleted and the places where it is used would use default-sort_key instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
- Sort by infimum if possible
-
- Otherwise sort by hash. Try to put these at the end.
+ Sort by infimum if possible. Otherwise sort by
+ ``default_sort_key``.
@smichr
SymPy member
smichr added a line comment Aug 1, 2012

Return sort key of infimum (if possible) otherwise the value of default_sort_key of the set.

(The phrasing now makes it sound like something is sorted.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
try:
val = s.inf
@@ -1219,7 +1218,7 @@ def _complement(self):
raise ValueError("%s: Complement not defined for symbolic inputs"
%self)
- args = sorted(self.args, key=default_sort_key)
+ args = sorted(self.args)
@smichr
SymPy member
smichr added a line comment Aug 1, 2012

are args always going to sort from -oo to oo without the default_sort_key? And if so, then the -sorted_args routine below doesn't need to use default-sort_key and this line here at 1222 can just be args = self._sorted_args.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
intervals = [] # Build up a list of intervals between the elements
intervals += [Interval(S.NegativeInfinity, args[0], True, True)]
@@ -1265,3 +1264,8 @@ def _eval_evalf(self, prec):
def _hashable_content(self):
return (self._elements,)
+
+ @property
+ def _sorted_args(self):
+ from sympy.utilities import default_sort_key
+ return sorted(self.args, key=default_sort_key)
View
3 sympy/core/tests/test_basic.py
@@ -124,3 +124,6 @@ def test_preorder_traversal():
expr = z + w*(x+y)
assert list(preorder_traversal([expr], key=default_sort_key)) == \
[[w*(x + y) + z], w*(x + y) + z, z, w*(x + y), w, x + y, x, y]
+
+def test_sorted_args():
+ assert b21._sorted_args == b21.args