6
$\begingroup$

I have a list which looks like this

list = {0, Subscript[x,7], -Subscript[x,3]-Subscript[x,9], -Subscript[x,9]};

and all the $x_i$'s are positive semidefinite (i.e. nonnegative) real numbers. I would like to be able to sort this into

sortedlist = {-Subscript[x,3]-Subscript[x,9], -Subscript[x,9], 0, Subscript[x,7]}

How do I achieve this? I tried

Assuming[Subscript[x,3] > 0 && Subscript[x,7] > 0 && Subscript[x,9] > 0, Sort[list]]

But this obviously does not work. In general, I'd like to be able to impose more constraints on the $x_i's$ when they're being sorted.

$\endgroup$
  • 1
    $\begingroup$ An interesting idea, but a symbolic list where you have an ordering for all the elements is rare $\endgroup$ – mikado Mar 19 at 21:31
5
$\begingroup$

Here is a possibility:

sortWithAssumptions[list_, assum_] := Module[{order},
    order[a_, b_] := Simplify[a < b, assum];
    Sort[list, order]
]

For your example:

sortWithAssumptions[
    {0,Subscript[x,7],-Subscript[x,3]-Subscript[x,9],-Subscript[x,9]},
    Subscript[x,3]>0&&Subscript[x,7]>0&&Subscript[x,9]>0
] //TeXForm

$\left\{-x_3-x_9,-x_9,0,x_7\right\}$

Another example:

sortWithAssumptions[
    {0,Subscript[x,7],-Subscript[x,3]-Subscript[x,9],-Subscript[x,9], Subscript[x,9]},
    Subscript[x,3]>0&&Subscript[x,7]>0&&Subscript[x,9]>0&&Subscript[x,7]<Subscript[x,9]
] //TeXForm

$\left\{-x_3-x_9,-x_9,0,x_7,x_9\right\}$

$\endgroup$
  • $\begingroup$ Thank you, Carl! $\endgroup$ – leastaction Mar 19 at 21:38
5
$\begingroup$

How about:

list[[Ordering[list /. _Subscript -> 1]]]

{-Subscript[x, 3] - Subscript[x, 9], -Subscript[x, 9], 0, Subscript[x, 7]}

So basically we sort it the way it would be sorted with all subscripts == 1.

$\endgroup$
  • $\begingroup$ Thanks! Seems to work like a charm, but can you shed some light on why? $\endgroup$ – leastaction Mar 19 at 21:31
  • $\begingroup$ @leastaction just take a look at list /. _Subscript -> 1 and at Ordering[list /. _Subscript -> 1]. $\endgroup$ – Kuba Mar 19 at 21:32
  • $\begingroup$ I see, so you basically assigned the value $x_i = 1$ to each $x_i$. It works in this case, but generically, $x_i$'s may be different. $\endgroup$ – leastaction Mar 19 at 21:36
  • 1
    $\begingroup$ @leastaction sure, which means there isn't only one correct answer and every valid within constraints is correct. $\endgroup$ – Kuba Mar 19 at 21:38
4
$\begingroup$
Sort[list, TrueQ@Simplify[#1 < #2, _Subscript > 0] &]

(* Out: {-Subscript[x, 3] - Subscript[x, 9], -Subscript[x, 9], 0, Subscript[x, 7]} *)
$\endgroup$
4
$\begingroup$

In this case, we can use RankedMin and FullSimplify to get the answer you seek

Assuming[
 Subscript[x, 3] > 0 && Subscript[x, 7] > 0 && Subscript[x, 9] > 0, 
 FullSimplify[Table[RankedMin[list, i], {i, 1, Length[list]}]]]
(* {-Subscript[x, 3] - Subscript[x, 9], -Subscript[x, 9], 0, Subscript[x, 7]} *)

This has the advantage of not returning a (potentially) wrong answer if the sort order is uncertain.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.