4
$\begingroup$

So I tried taking a total derivative, with constants, but then the ouput had Dt[ ..., Constants -> ...] as an output. I tried to use a rule to remove it from tha output, but it did not work. See below. What am I doing wrong?

Dt[a x^2 + b y, x, Constants -> {a, b}]
Dt[a x^2 + b y, x, Constants -> {a, b}] /.{ Constants -> {a, b}} -> Null
$\endgroup$
1
  • $\begingroup$ I would do it like this: Dt[a x^2 + b y, x, Constants -> {a, b}] /. HoldPattern[Dt[x__, y_]] :> Dt[x]. I tried Dt[a x^2 + b y, x, Constants -> {a, b}] /. (Constants -> {a, b}) -> Null first, but strangely enough, if you Trace the calculation, Dt[x, y, Null] evaluates to Dt[Dt[x, y]], which I don't understand. $\endgroup$
    – march
    Commented Oct 3, 2019 at 4:08

5 Answers 5

4
$\begingroup$
SetAttributes[{a, b}, Constant]
Dt[a x^2 + b y, x]

2 a x + b Dt[y, x]

$\endgroup$
2
  • 1
    $\begingroup$ If you don't like making global definitions, you can use Block[{a,b},SetAttributes[{a, b}, Constant]; Dt[a x^2 + b y, x]] $\endgroup$
    – mikado
    Commented Oct 3, 2019 at 20:18
  • $\begingroup$ @mikado, good suggestion for the OP to consider. $\endgroup$ Commented Oct 3, 2019 at 20:26
3
$\begingroup$

A few additional ways to use ReplaceAll:

Dt[a x^2 + b y, x, Constants -> {a, b}] /. _Rule -> {}
Dt[a x^2 + b y, x, Constants -> {a, b}] /. HoldPattern[Constants -> _] -> {}
Dt[a x^2 + b y, x, Constants -> {a, b}] /. p : (Constants -> _) -> {}

and Replace:

Replace[Dt[a x^2 + b y, x, Constants -> {a, b}], _Rule -> {}, All]
Replace[Dt[a x^2 + b y, x, Constants -> {a, b}], (Constants -> _) -> {}, All}]
Replace[Dt[a x^2 + b y, x, Constants -> {a, b}], p:(Constants -> _) -> {}, All}]
Replace[Dt[a x^2 + b y, x, Constants -> {a, b}], HoldPattern[Constants -> _] -> {}, All}]

and DeleteCases:

DeleteCases[Dt[a x^2 + b y, x, Constants -> {a, b}], _Rule, All]
DeleteCases[Dt[a x^2 + b y, x, Constants -> {a, b}], Constants -> _, All]
DeleteCases[Dt[a x^2 + b y, x, Constants -> {a, b}],  HoldPattern[Constants -> _], All]

all give

2 a x + b Dt[y, x]

$\endgroup$
2
  • $\begingroup$ +1 for the DeleteCases approach, which is probably the clearest in intent and will work for all heads. Regarding the other examples: Is there any specific reason why you give examples with unused named patterns and HoldPattern? $\endgroup$
    – Lukas Lang
    Commented Oct 3, 2019 at 14:33
  • 1
    $\begingroup$ @LukasLang, thank you for the upvote. I think HoldPattern[foo] -> bar is the "canonical" method to make changes to the rule foo. I added others because they also work ( some in general, some only for the case in OP). $\endgroup$
    – kglr
    Commented Oct 3, 2019 at 15:13
2
$\begingroup$

Try

Dt[a x^2 + b y, x] /. {Dt[a, c_] -> 0, Dt[b, c_] -> 0}
$\endgroup$
2
$\begingroup$

Another solution is to replace with Sequence[], which does not rely on any behaviour specific to Dt1:

Dt[a x^2 + b y, x, Constants -> {a, b}] /. (Constants -> _) -> Sequence[]
(* 2 a x + b Dt[y, x] *)

Note the use of parentheses ((...)) instead of braces ({...}) around the r.h.s. of the rule.

1 It will not work if the wrapping function (Dt here) has theSequenceHold attribute

$\endgroup$
1
$\begingroup$

It's a little bit hacky, but this works.

In[1]:=Dt[a x^2 + b y, x, Constants -> {a, b}] /. Rule[Constants, _] -> {}
Out[1]=2 a x + b Dt[y, x]

Weirdly, if you replace Rule[Constants, _] with something other than {} like Nothing or None, then the derivative term's inputs become reordered, like Dt[y, Nothing, x] (or whatever you replace it with). If someone could explain that, it'd be very interesting.

$\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.