I want to calculate gradient of Sort[{x1,x2,x3,x4}] but it seems like I'm getting a wrong answer. Specifically Plot[Sort[{2, 1, x, 10}][[4]], {x, -20, 20}] gives me

enter image description here

while D[Sort[{2,1,x,10}],x] returns {0,0,0,1}, which is not true.

Is there a way to fix it?

  • 3
    $\begingroup$ Since Sort[{2, 1, x, 10}] yields {1, 2, 10, x} and the derivative of {1, 2, 10, x} is {0, 0, 0, 1}, I'd say the derivative is correct. What do you think it should be? $\endgroup$ – Michael E2 Jul 20 at 22:48
  • $\begingroup$ As for the Plot[], since it holds its argument, it won't sort {2, 1, x, 10} and then plug in x; instead, it plugs in x and then sorts (then takes part 4, which will be the greatest of the four numbers. -- I'm not sure which order of operations (plug in then sort or the reverse) you want. Max[{2,1,x,10}] might be a better way to approach it. $\endgroup$ – Michael E2 Jul 20 at 22:51
  • $\begingroup$ Derivative is HevysideTheta(x-10) as you can see from the graph. $\endgroup$ – stiv Jul 20 at 22:52
  • $\begingroup$ Try D[Max[{2, 1, x, 10}], x]. $\endgroup$ – Michael E2 Jul 20 at 22:53
  • $\begingroup$ Max is a different function, which works, Sort doesn't. I need to differentiate Sort, not Max. $\endgroup$ – stiv Jul 20 at 22:56

Treating Sort as a function from the disjoint union of $\bf R^n$, $n=0,1,2,\dots$, to the same union, here is one way to define the derivative. I'll call the numerical sort NSort, just so I don't have to overwrite a built-in function.

NSort[list_?(VectorQ[#, NumericQ] &)] := NumericalSort[list];

Derivative[orders_List][NSort][list_List] /; Length[orders] == Length[list] :=
 With[{args = Array[Slot, Length@orders]},
        p, {D[p, Sequence @@ Transpose@{args, orders}], 
         Less @@ p}] /@ Permutations[args],
      ] & @@ list]

OP's example:

df = D[NSort[{2, 1, x, 10}], x]

Mathematica graphics

Plot[Indexed[df, 4], {x, -20, 20}]    

enter image description here

Another example:

D[NSort[{2, 1, 3 x, x^2}], {x, 2}] // PiecewiseExpand

Mathematica graphics

  • $\begingroup$ COOL! Thank you so much! $\endgroup$ – stiv Jul 21 at 1:58
  • $\begingroup$ @stiv You're welcome. $\endgroup$ – Michael E2 Jul 21 at 3:10

Although this is definitely an XY problem, here is a possible solution:

list = {2, 1, x, 10};
listDer = D[list, x];

 listDer[[Ordering[list, -1]]]
 , {x, -20, 20}

enter image description here

  • 1
    $\begingroup$ This seems likely but the insistence on avoid Max makes me think the OP is interested in other components of the sorted vector as well. $\endgroup$ – Michael E2 Jul 21 at 0:39
  • 1
    $\begingroup$ @MichaelE2 Yeah, probably. That's easy though: just drop the -1 in Ordering. Hard to tell what exactly OP wants anyway. $\endgroup$ – AccidentalFourierTransform Jul 21 at 0:45

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.