# Creating a function as a table of derivations

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.

• Do you also need to determine the number of parameters, or is that known?
– Ian
Commented Feb 3, 2014 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! Commented Feb 3, 2014 at 10:40
• Sure they do, but the Head of f is "Function" not list. Try Dimensions[f[[1]]].
– Ian
Commented Feb 3, 2014 at 13:34
• @Ian Thanks for the tip! Commented Feb 3, 2014 at 17:08

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]

• 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] Commented Feb 3, 2014 at 14:13
• @Pygmalion One can use Function to get a function out of it. I've edited the answer. Commented Feb 3, 2014 at 14:42
• 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? Commented Feb 3, 2014 at 18:42
• @Pygmalion g = Function[D[f@@args,#]&/@args//Transpose//Evaluate] Commented Feb 3, 2014 at 20:20

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


• 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. Commented Feb 3, 2014 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. Commented Feb 3, 2014 at 17:41
• +1 Thanks. Another solution, but the previous one is tidier, as I can directly pass vector (of variable Length) Commented Feb 3, 2014 at 18:44
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}} &
*)

• Hi again! This creates vector of three vectorial functions, what I need is matrix function (& in the end) Commented Feb 3, 2014 at 18:54
• @Pygmalion Take a look now Commented Feb 3, 2014 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! Commented Feb 4, 2014 at 15:55