I have a symbol a appearing in my computations, which has a complicated definition in terms of other variables. However, I know that any derivative of a has the form

da/dx = a^3 f(x)

for some known function f, and I would like to teach Mathematica how to do it.

I thought setting up-values would be appropriate, but I just can't get it right. My first attempt was

D[a, x_] ^:= a^3*f[x]

It works if I try D[a, x5] and gives a^3*f[x5], but already D[a^2, x5] gives 0. Then I thought it might help to actually define a as a function, so that Mathematica knows it has to compute derivatives of it too, so I tried

D[a[x1, x2], x_] ^:= a[x1, x2] f[x]

Again, the basic test passed and calling D[a[x1, x2], x1] gives what I want. However, calling D[a[x1, x2]^2, x1] gives 2 a[x1,x2]^2 Derivative[1,0][a][x1,x2]], I don't fully understand why Mathematica does that and doesn't just use the definition of D that is already there, but my last hope was to define this as an up-value too. However, setting this as an up-value for a doesn't work because the nesting is too deep, and the only way is to really set it as an up-value for Derivative, which already I think is not very good style. I was a bit surprised that Derivative was not a protected symbol, so I could just write

Derivative /: Derivative[1, 0][a][x1_, x2_] := D[a[x1, x2], x1]
Derivative /: Derivative[0, 1][a][x1_, x2_] := D[a[x1, x2], x2]

This passed the few simple test that I did, but I'm not happy with it. The biggest problem is that I have to specify all the dependencies of a explicitly and then make an individual definition of Derivative for each of them. Also the solution above doesn't look very clean, and I'm afraid that there will be some cases which I haven't thought of, where this doesn't work again. I would love to not specify the arguments of a and write something like a[_] or a[__], but it doesn't seem to work. I'm a bit sad I wasn't able to solve this, given that what I want is so simple: to tell Mathematica, look, whenever you compute derivatives of an expression which contains as with respect to some x, then derivative of each occurrence of a is a^3 f(x).

  • $\begingroup$ Try your first approach but with the total derivative Dt instead. $\endgroup$
    – swish
    May 12, 2017 at 18:48
  • $\begingroup$ @swish thanks, it seems to work. However, now any other symbol b that appears in the expression becomes Dt[b,x], such that now I have to know which other symbols there will be beforehand and set Dt[b,x]^:=0 for all possible b. Also in terms of performance, is there any difference between D and Dt? $\endgroup$
    – Stan
    May 12, 2017 at 19:12

3 Answers 3


You want the NonConstants option to D.

D[a, x_, OptionsPattern[]] ^:= a^3*f[x]

D[a, x5]
(* a^3 f[x5] *)

D[a^2, x5, NonConstants -> a]
(* 2 a^4 f[x5] *)

I think giving a a hidden dependence on x will cause difficulties. I would make it explicit, and do something like:

Derivative[1][a] = a[#]^3 f[#]&;


D[a[x], x]
D[a[x]^2, x]
D[g[a[x]], {x, 2}] //TeXForm

a[x]^3 f[x]

2 a[x]^4 f[x]

$\left(a(x)^3 f'(x)+3 a(x)^5 f(x)^2\right) g'(a(x))+a(x)^6 f(x)^2 g''(a(x))$


According to the original post, a:"[h]as a complicated definition in terms of other variables". Also, the derivative of the symbol a wrt to variable x is equal to:a^3f[x].

These two facts about symbol a have implications for the derivatives of a. This can be summarized by:

d[n_, x_, y___]:=Nest[D[#, x]/.D_[1, B___][a][X_, Y___]:>a[X,Y]^3f[X]&,a[x, y], n]

Effectively what 'function' d does is to recursively apply the definition of the derivative of a and return the result.

Having this auxiliary function at hand can be helpful in producing the derivatives of a for arbitrary order. Eg.

In[1]:= d[5, x]

produces $a(x)^3 f^{(4)}(x)+15 a(x)^5 f(x) f^{(3)}(x)+150 a(x)^7 f(x)^2 f''(x)+1050 a(x)^9 f(x)^3 f'(x)+225 a(x)^7 f(x) f'(x)^2+30 a(x)^5 f'(x) f''(x)+945 a(x)^{11} f(x)^5$

Finally, doing

Derivative[1, b___][a][x_, y___] := a[x, y]^3 f[x]
Derivative[n_, b___][a][x_, y___] := d[n, x, y]

yields a possible solution to the original problem

$_{(Ithink Carl Woll's solution is more elegant)}$


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.