I have an expression that is the the sum and product of undefined derivatives and their derivatives.

e.g. f g+ Dt[f, t] g +Dt[g, {t, 2}] f

What I would like to find is the maximum order of the derivatives, and so 2 would be returned for the above example.

My first attempts use Cases to make lists of the order of derivatives. This would send Dt[x_, t] -> 1 and Dt[x_, {t, n_}] -> n. However for some reason Dt[f, t] and Dt[f, {t, 2}] fail to match with their respective patterns and don't trigger the rule. If, in the rules, the x_ is replaced with f the rules will trigger but only for f and no other function. Is there a way to do this search without a strict list of the available functions?


This works on a principle similar to kglr's answer, in that HoldPattern[] is needed to prevent Dt[] from prematurely evaluating:

expr = f g + Dt[f, t] g + Dt[g, {t, 2}] f;

Max[0, Cases[expr, HoldPattern[Dt[f_, ord_]] :> If[ListQ[ord], Last[ord], 1], ∞]]

This should work for any function (f, g or whatever) and any independent variable (t or whatever):

Cases[f g + Dt[f, t] g + Dt[g, {t, 2}] f, Dt[_, {_, order_}] :> order, Infinity]

If you want to include the first order derivative, there is a little bit more to write. Indeed, the pattern Dt[_, _] is evaluated to 1 before it can be used by Cases to find out the expression D[f, t]. So here is a possible alternative:

            Cases[f g + Dt[f, t] g + Dt[g, {t, 2}] f, 
                  Alternatives[_?(# === Dt &)[_, order_Symbol], 
                               Dt[_, {_, order_}]] :> order, Infinity], 
            _Symbol -> 1, 1]]
  • $\begingroup$ This almost works, it can't detect first order derivatives. and Dt[_,_]->1 doesn't work. $\endgroup$ – user39963 Aug 8 '14 at 1:43
  • $\begingroup$ I edited my previous answer and put a complete solution for the case Dt[_, _]. Hope this is what you was looking for. $\endgroup$ – bobknight Aug 8 '14 at 5:59
  • $\begingroup$ @bobknight An alternative to your edit: Cases[f g+Dt[f,t] g+Dt[g,{t,2}] f,HoldPattern[Dt[_,order_]]:>If[Head[order]===List,Last@order,1],{0,Infinity}]//Max $\endgroup$ – seismatica Aug 8 '14 at 6:22
maxOrder[expr_] := 
  Max[{0, (# /. _Symbol :> 1) & /@
     Last /@
      (Level[#, {-1}] & /@
        Cases[expr, _Dt, Infinity])}];

maxOrder[f g + Dt[f, t] g + Dt[g, {t, 2}] f]


maxOrder[f g + Dt[f, t] g]


maxOrder[f g]


  • $\begingroup$ This also nearly works, I just needed to place {} around expr when used in Cases[] so that when expr is a single term Cases still act as expected. Thank you for the help, I'll be trying to break down the function to better understand how it works. $\endgroup$ – user39963 Aug 8 '14 at 4:28
  • 1
    $\begingroup$ @user39963 You can just use {0, Infinity} (instead of just Infinity at the end of Cases) and it will take care of that issue. I was writing my own answer but it was remarkably similar to Bob's aside from the level spec of Cases. $\endgroup$ – seismatica Aug 8 '14 at 4:41
mxOrdr = Max[0,Max@Cases[Replace[#,
                            HoldPattern[Dt[x_, t:Except[_List]]] :> 
                                         HoldForm[Dt[x, {t, 1}]], {0, Infinity}], 
                x_Dt :> Unevaluated[x][[2, -1]], {0, Infinity}]] &;

mxOrdr[f g + Dt[f, t] g + Dt[g, {t, 2}] f]
(* 2 *)

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.