Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.