# Finding the maximum order derivative of an expression

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?

## 4 Answers

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], ∞]]
2


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:

Max[Replace[
Cases[f g + Dt[f, t] g + Dt[g, {t, 2}] f,
Alternatives[_?(# === Dt &)[_, order_Symbol],
Dt[_, {_, order_}]] :> order, Infinity],
_Symbol -> 1, 1]]

• This almost works, it can't detect first order derivatives. and Dt[_,_]->1 doesn't work. – user39963 Aug 8 '14 at 1:43
• I edited my previous answer and put a complete solution for the case Dt[_, _]. Hope this is what you was looking for. – bobknight Aug 8 '14 at 5:59
• @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 – 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]


2

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


1

maxOrder[f g]


0

• 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. – user39963 Aug 8 '14 at 4:28
• @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. – 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 *)