Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I type the following command line

Table[D[#1,varx[[i]]] &,{i,3}]

where varx is a vector of dummy variables

varx = {x1, x2, x3}

According to this link NonCommutativeMultiply, I want to obtain a vector like this

$\left\{\frac{\partial }{\partial \text{x1}},\frac{\partial}{\partial \text{x2}},\frac{\partial}{\partial \text{x3}}\right\}$

But I got this

enter image description here

It seems like the index i in the command Table does not work. I don't know why this happens. I try not to use the placeholder '#1' like this

Table[D[2 x1^2 + 4 x2^3 + x3^4, varb[[i]]], {i, n}]

And this works all fine

{4 x1, 12 x2^2, 4 x3^3}

If I want to keep the placeholder, what should I do?

share|improve this question
up vote 5 down vote accepted

Use With:

Table[With[{v = varx[[i]]}, D[#1, v] &], {i, 3}]`
(* {D[#1, x1] & , D[#1, x2] & , D[#1, x3] & } *)

See the section "Scope" of the documentation page for With. Note that Function (&) has the attribute HoldAll, so that the value of varx[[i]] needs to be inserted into the function.

The above gives a list of operators. An alternate interpretation of what is sought is a single operator that evaluates to a list. Here's a way:

grad = Block[{D}, Evaluate[Table[D[#1, varx[[i]]], {i, 3}]] &]
(* {D[#1, x1], D[#1, x2], D[#1, x3]} & *)

Here's another method for this particular example, based on behavior of D[f, {{x1, x2, ...}}]:

grad = With[{v = varx}, D[#, {v}] &]

Both yield

grad[x1 + x2^2 x3]
(* {1, 2 x2 x3, x2^2} *)
share|improve this answer

You can use Slot (#) but the pure function (&) should be at a different position, i.e.

varx = {x1, x2, x3};

Table[List[#1, varx[[i]]], {i, 3}] & @@@ {{f, g}, {h, i}}

{{{f, x1}, {f, x2}, {f, x3}}, {{h, x1}, {h, x2}, {h, x3}}}

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.