Hi,

On 19 October 2014 05:40, Duane Nykamp <[email protected]> wrote:
> Here's my solution for a customized sort key.  Does this seem reasonable?  I
> just copied the format for the sort key for numbers and applied it to
> anything that was real.  It seems to work how I'd want it to work.
>
>
> def customized_sort_key(item, order=None):
>
>     try:
>         x=sympify(item)
>     except:
>         pass
>     else:
>         if x.is_real:
>             return  ((1, 0, 'Number'), (0, ()), (), x)
>
>     return default_sort_key(item,order)
>

This won't work, e.g.:

In [1]: r = Symbol('r', real=True)

In [2]: r.is_real
Out[2]: True

Really what you should be looking into are sort_key() methods defined
all over sympy. default_sort_key() is just a convenience function that
allows to handle non-Basic objects as well, thus being usable as a key
to sorted().

Defining the default sort key isn't that simple, e.g.:

In [1]: l2 = [1 - sqrt(2), 1, pi, 4, 1 + sqrt(2)]

In [2]: lx = [1 - sqrt(x), 1, pi, 4, 1 + sqrt(x)]

In [3]: l2I = map(lambda x: x + I, l2)

In [4]: lxI = map(lambda x: x + I, lx)

In [5]: sorted(l2, key=default_sort_key)
Out[5]:
⎡               ___      ___    ⎤
⎣1, 4, π, 1 + ╲╱ 2 , - ╲╱ 2  + 1⎦

In [6]: sorted(lx, key=default_sort_key)
Out[6]:
⎡             ___        ___    ⎤
⎣1, 4, π, - ╲╱ x  + 1, ╲╱ x  + 1⎦

In [7]: sorted(l2I, key=default_sort_key)
Out[7]:
⎡                           ___          ___        ⎤
⎣1 + ⅈ, 4 + ⅈ, π + ⅈ, 1 + ╲╱ 2  + ⅈ, - ╲╱ 2  + 1 + ⅈ⎦

In [8]: sorted(lxI, key=default_sort_key)
Out[8]:
⎡                         ___            ___        ⎤
⎣1 + ⅈ, 4 + ⅈ, π + ⅈ, - ╲╱ x  + 1 + ⅈ, ╲╱ x  + 1 + ⅈ⎦

Will your key provide the same level of uniformity? Perhaps you could,
doing fair amount of symbolic processing, using as_real_imag(), etc.,
but then you would increase cost of sort_key() considerably and, most
likely, make printing framework even slower than it already is.

There should be some old discussions regarding this on the mailing
list or in the issue tracker. At the time, it was a pretty big
undertaking (historically sort_key started as as_tuple_tree, so you
may also use this keyword).

Mateusz

>
>
>
> On Saturday, October 18, 2014 8:52:50 PM UTC-5, Duane Nykamp wrote:
>>
>> I was using sympy's default_sort_order to sort objects.  However, I
>> realized that it does not sort numerical quantities in order:
>>
>> In [15]: sorted([-1, -1-sqrt(2),1+sqrt(2), pi, 4], key=default_sort_key)
>> Out[15]: [-1, 4, pi, 1 + sqrt(2), -sqrt(2) - 1]
>>
>> The default sort key, lex, works in this case
>>
>> In [16]: sorted([-1, -1-sqrt(2),1+sqrt(2), pi, 4])
>> Out[16]: [-sqrt(2) - 1, -1, 1 + sqrt(2), pi, 4]
>>
>> but that doesn't work on symbolic objects.
>>
>> Is there a way to sort that works on all sympy objects, symbolic or not,
>> but still produces the normal order for numerical quantities?
>>
>> The only thing I can think of is trying the lex sort key first, then
>> reverting to default_sort_key on TypeError.  But, that has the undesirable
>> effect of changing the relative order of numerical quantities once a
>> symbolic quantity is added to the mix.
>>
>> Any suggestions or pointers to a place where this is discussed already?
>>
>> Thanks,
>> Duane
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/ade686f6-776c-4e6b-a34f-997918afa64c%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAGBZUCaodrWQRA456OeyFw8CM9TA_tfOKyip0CF-CHwQHdGpZg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to