2
$\begingroup$

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.

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

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

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Pygmalion Feb 3 '14 at 17:04
  • $\begingroup$ With der = D[f[z], {z}]; der /. {x[1] -> a, x[2] -> b, x[3] -> c} gives the form you ask for. $\endgroup$ – bill s Feb 3 '14 at 17:41
  • $\begingroup$ +1 Thanks. Another solution, but the previous one is tidier, as I can directly pass vector (of variable Length) $\endgroup$ – Pygmalion Feb 3 '14 at 18:44
1
$\begingroup$
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}} &
*)
$\endgroup$
  • $\begingroup$ Hi again! This creates vector of three vectorial functions, what I need is matrix function (& in the end) $\endgroup$ – Pygmalion Feb 3 '14 at 18:54
  • $\begingroup$ @Pygmalion Take a look now $\endgroup$ – Dr. belisarius Feb 3 '14 at 20:54
  • $\begingroup$ Yes, not it works! I am amazed how two so different procedures have the same effect. Hopefully I will understand them sometime in future! $\endgroup$ – Pygmalion Feb 4 '14 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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