Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

e.g. fg+ 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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted
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

share|improve this answer
    
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 at 4:28
1  
@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 at 4:41

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 inlcude 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]]
share|improve this answer
    
This almost works, it can't detect first order derivatives. and Dt[_,_]->1 doesn't work. –  user39963 Aug 8 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 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 at 6:22
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 *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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