# Applying rules on rules

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

• 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. – march Oct 3 '19 at 4:08

SetAttributes[{a, b}, Constant]
Dt[a x^2 + b y, x]


2 a x + b Dt[y, x]

• 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]] – mikado Oct 3 '19 at 20:18
• @mikado, good suggestion for the OP to consider. – Suba Thomas Oct 3 '19 at 20:26

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 -> _) -> {}

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}]

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]

• +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? – Lukas Lang Oct 3 '19 at 14:33
• @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). – kglr Oct 3 '19 at 15:13

Try

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


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

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.