3
$\begingroup$

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?

$\endgroup$

4 Answers 4

2
$\begingroup$

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
$\endgroup$
5
$\begingroup$

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]]
$\endgroup$
3
  • $\begingroup$ This almost works, it can't detect first order derivatives. and Dt[_,_]->1 doesn't work. $\endgroup$
    – user39963
    Commented Aug 8, 2014 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
    Commented Aug 8, 2014 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$ Commented Aug 8, 2014 at 6:22
3
$\begingroup$
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

$\endgroup$
2
  • $\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
    Commented Aug 8, 2014 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$ Commented Aug 8, 2014 at 4:41
1
$\begingroup$
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 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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