# Defining an array of differential operators

I'm trying to define an array of differential operators, so that I can choose which one to use in a sum using the index. That is, I'm trying to write,

$$\mathrm{del} = \{\partial_t,\partial_x,\partial_y,\partial_z\}$$

so that I can then use, for example,

$$\mathrm{del}[0] f(t) = \mathrm{D}[f(t),t]$$

but even trying to define $\mathrm{del}$ is not working. (For those curious, this is so that I can define a tensor calculus sum in general relativity.)

• There are some packages for GR; I've never used them, but you could try googling mathematica general relativity package and maybe you'll find something useful. Jan 8, 2018 at 19:08
• @corey979 Yes I’m working with one but for what I want to do, defining it myself is simpler as they have bad interfaces. Jan 8, 2018 at 20:22

One possibility is

del = Table[With[{k = k}, D[#, k] &], {k, {t, x, y, z}}];


Then,

del[[1]][x^2]
del[[2]][x^2]
Through[del[x^2]]
(* 0 *)
(* 2*x *)
(* {0, 2 x, 0, 0} *)


If you really want it to be indexed starting at zero, then redefine

Clear@del
Do[With[{var = {t, x, y, z}[[k]]}, del[k - 1] = D[#, var] &], {k, 4}]


Then,

del[0][x^2]
del[1][x^2]
(* 0 *)
(* 2*x *)