Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent reordering in derivative output?

A recent post on the Wolfram Blog offered the following function to format derivatives in a more traditional way.

pdConv[f_] := 
 TraditionalForm[
  f /. Derivative[inds__][g_][vars__] :> 
    Apply[Defer[D[g[vars], ##]] &, 
     Transpose[{{vars}, {inds}}] /. {{var_, 0} :> 
        Sequence[], {var_, 1} :> {var}}]
 ]

An example use, Dt[d[x, a]] // pdConv gives:

enter image description here

Without breaking the general capabilities of pdConv, can someone alter it to maintain the given order of variables, producing the output shown below? (of course this is purely for asthetic reasons, making derivations easier for a human to follow)

enter image description here

I suspect this will be nontrivial to implement---unless someone knows of a magical Global option that can be temporarily overridden within a Block.

For what it's worth, these SO questions may be related:

  • Preventing "Plus" from rearranging things
  • controlling order of variables in an expression
like image 452
telefunkenvf14 Avatar asked Dec 24 '11 11:12

telefunkenvf14


1 Answers

There is probably a cleaner way to do s, but if it's purely for presentation purposes, you could do something like

pdConv[f_, vv_] :=
 Module[{v},
  (HoldForm[
       Evaluate@
        TraditionalForm[((f /. Thread[vv -> #]) /. 
           Derivative[inds__][g_][vars__] :> 
            Apply[Defer[D[g[vars], ##]] &, 
             Transpose[{{vars}, {inds}}] /. {{var_, 0} :> 
                Sequence[], {var_, 1} :> {var}}])]] /. 
      Thread[# -> vv]) &@ Table[Unique[v], {Length[vv]}]]

Here, the extra parameter vv is a list of the variables in f in the order you want the partial derivatives to appear. To use this function, you would do something like

pdConv[Dt[d[x, c]], {x, c}]

equations in right order

Basically what this solution does is to temporarily replace the list of variables vv with a list of dummy variables which are in the right lexicographical order, apply the transformation, and then replace the dummy variables with the original variables while preserving the desired order by wrapping the transformed expression in HoldForm.

like image 172
Heike Avatar answered Nov 08 '22 16:11

Heike