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.

Suppose I have function, which is in fact a list of $n$ functions with $n$ parameters

f = {#1 + #2^2 + #3^3, #1^2 + #2^3 + #3^4, #1^3 + #2^4 + #3^5}&

I want to get a function that is in fact matrix of all possible partial derivations

$ \left( \begin{array}{ccc} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \frac{\partial f_1}{\partial x_3} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \frac{\partial f_2}{\partial x_3} \\ \frac{\partial f_3}{\partial x_1} & \frac{\partial f_3}{\partial x_2} & \frac{\partial f_3}{\partial x_3} \end{array} \right)$ , i.e

g = {{1,2#1,3#1^2},{2#2,3#2^2,4#2^3},{3#3^2,4#3^3,5#3^4}}&

I tried to do that myself by adapting the answer Find partial derivatives when the number of variables is unknown, but I don't understand the cumulative use of Apply, Seqence and Map.

share|improve this question
    
Do you also need to determine the number of parameters, or is that known? –  Ian Feb 3 at 10:33
    
@Ian number of parameters is equal to number of functions and should be determined too. Too bad, Length and Dimensions don't work on f as defined above! –  Pygmalion Feb 3 at 10:40
    
Sure they do, but the Head of f is "Function" not list. Try Dimensions[f[[1]]]. –  Ian Feb 3 at 13:34
    
@Ian Thanks for the tip! –  Pygmalion Feb 3 at 17:08
add comment

3 Answers 3

One can first find all arguments as slots and then differentiate with respect to them:

    f = {#1 + #2^2 + #3^3, #1^2 + #2^3 + #3^4, #1^3 + #2^4 + #3^5} &;
    args = ToExpression@Union@StringCases[ToString@FullForm@f,
        RegularExpression["Slot\\[\\d+\\]"]];
    g = Function[D[f@@args,#]&/@args//Evaluate]
share|improve this answer
    
Thanks for the answer, but you have missed the point. I need function that is in fact matrix. E.g., to be able to write deriv=D[f @@ args, #] & /@ args and then use deriv[1,2,3] –  Pygmalion Feb 3 at 14:13
    
@Pygmalion One can use Function to get a function out of it. I've edited the answer. –  Andrew Feb 3 at 14:42
    
+1 Yes, this works! –  Pygmalion Feb 3 at 17:03
    
I am sorry, I made a mistake, in fact I need the transposed matrix of the above. Can you make an adjustment to your solution? –  Pygmalion Feb 3 at 18:42
    
@Pygmalion g = Function[D[f@@args,#]&/@args//Transpose//Evaluate] –  Andrew Feb 3 at 20:20
add comment

The derivative function is actually quite general. For example, say you have a list of three functions in three variables. Then the function D can calculate the matrix of derivatives directly:

f[x_] := {x[[1]] + x[[2]]^2 + x[[3]] x[[2]], x[[1]] x[[2]], x[[3]]^3};
z = Array[x, 3];
D[f[z], {z}] // MatrixForm

enter image description here

share|improve this answer
    
The problem with this solution is that der=D[f[z], {z}] is not function of vector x, i.e. der[{a,b,c}] does not work. –  Pygmalion Feb 3 at 17:04
    
With der = D[f[z], {z}]; der /. {x[1] -> a, x[2] -> b, x[3] -> c} gives the form you ask for. –  bill s Feb 3 at 17:41
    
+1 Thanks. Another solution, but the previous one is tidier, as I can directly pass vector (of variable Length) –  Pygmalion Feb 3 at 18:44
add comment
mf = Derivative[Sequence @@ #][f] & /@ IdentityMatrix@Length@f[[1]]
(*
  {{1, 2 #1, 3 #1^2} &, {2 #2, 3 #2^2, 4 #2^3} &, {3 #3^2, 4 #3^3, 5 #3^4} &}
*)
 mf /. Function[o___] :> o /. x_ :> Function[x]
(*
 {{1, 2 #1, 3 #1^2}, {2 #2, 3 #2^2, 4 #2^3}, {3 #3^2, 4 #3^3, 5 #3^4}} &
*)
share|improve this answer
    
Hi again! This creates vector of three vectorial functions, what I need is matrix function (& in the end) –  Pygmalion Feb 3 at 18:54
    
@Pygmalion Take a look now –  belisarius Feb 3 at 20:54
    
Yes, not it works! I am amazed how two so different procedures have the same effect. Hopefully I will understand them sometime in future! –  Pygmalion Feb 4 at 15:55
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.