# Specifying independence of a variable in a function

I am dealing with a function of a finite number of variables and among the operations I wish to do is to differentiate it repeatedly with respect with any of the variables.

Now this function happens to be defined in terms of an unknown function, which only depends on a proper subset of the variables. I would like to tell this to Mathematica; i.e., for the purposes of differentiation, the unknown function is independent of a number of the variables.

The following minimal example shows what I am trying to do (and failing). Here f is supposed to be a function of only y, whereas g is defined in terms of f and is a function of both x and y.

g = x f + y
(* f x + y *)

f /: D[f, x, NonConstants -> {f}] := 0

D[g, x, NonConstants -> {f}]
(* f *)

D[g, x, y, NonConstants -> {f}]
(* D[f, y, NonConstants -> {f}] *)

D[g, y, x, NonConstants -> {f}]
(* D[f, y, NonConstants -> {f}] + x D[f, x, y, NonConstants  -> {f}] *)


I can assume that f is infinitely continuously differentiable, whence mixed partials commute and hence the second term should be absent. I could try to substitute In[2] by a more comprehensive rule which tests whether the variables on which f does not depend appear in v where v is defined by D[f,v__, NonConstants->{f}]. However I wonder whether there is perhaps a simpler way to do this.

Why not define the expressions in such a way that their dependence on the independent variables is explicit:

ClearAll[f, g, x, y]

g[x_, y_] = x f[y] + y

(* ==> y + x f[y] *)

D[g[x, y], y, x]

(* ==> Derivative[1][f][y] *)

D[g[x, y], x, y]

(* ==> Derivative[1][f][y] *)


The mixed derivatives now agree.

• Thanks! That’s eminently reasonable… I should have tried that before asking the question. Sep 4, 2014 at 19:17