In have some matrices of pure functions or numbers which I multiply by other matrices or vectors. I would like the functions to be evaluated in the result, but all that I get is something like this:

(I*(D[#1, x] & ))[f[{x, y}]]

Instead I would like something like this:

I*Derivative[{1, 0}][f][{x, y}]

All that I need is that the derivative could pass through the most external round brackets, so that the function could be applied to the argument.

Since I work with a large amount of matrices, I cannot delete manually all the parenthesis after getting the result; is there a way to let the function know it can pass through the brackets?



The solution which best fits my needs is a modified version of @kglr

f1=#/.head_[a___][d___]:>If[StringMatchQ[ToString[{a}//FullForm],"*Function*"], head[##&@@({a}/.w_Function:(1*#&):>w[d])], head[a][d]]&;]

  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Apr 30, 2016 at 11:22
  • $\begingroup$ see Here for the underlying problem. $\endgroup$
    – andre314
    Apr 30, 2016 at 11:32
  • $\begingroup$ Thank you, now I understand the reason for that behaviour. Nevertheless, the brackets are put automatically by mathematica, even if they are not present when the list which they are part of is defined. $\endgroup$
    – ablagi
    Apr 30, 2016 at 12:24

1 Answer 1


Restructure the FullForm using ReplaceAll:

f1 =  # /. head_[a___][d___] :> head[## & @@ ({a} /. w_Function :> w[d])] &


{#, f1 @ #} &[(((Log@(D[#1, x] &))))[h[{x, y}]] ]

Mathematica graphics

 {#, f1 @ #} &[(((I (D[#1, x] &) ((D[#1, y] &)))))[h[{x, y}]] ]

Mathematica graphics

Alternatively, define a function that processes the box forms of the expressions to remove the parentheses:

f2 = ToExpression[Replace[ToBoxes[#], {RowBox[{"(", RowBox[{a__}], ")"}] :> 
       RowBox[{a}], RowBox[{RowBox[{a___}], b___, RowBox[{c___}], d___}] :> 
       RowBox[{a, b, c, d}]}, {0, Infinity}]] &;


{I*D[#1, x] &[3 x y z], (I*(D[#1, x] &))[3 x y  z], (I*(D[#1, x] &))[3 x y  z] // f2}

Mathematica graphics

I*D[#1, x] &[h[{x, y}]]

Mathematica graphics

(I*(D[#1, x] &))[h[{x, y}]]

Mathematica graphics

(I*(D[#1, x] &))[h[{x, y}]] // f2

Mathematica graphics

  • $\begingroup$ ... not sure how robust this is for general expressions. $\endgroup$
    – kglr
    Apr 30, 2016 at 13:15
  • $\begingroup$ Thank you, it seems a good starting point to solve my problem. $\endgroup$
    – ablagi
    Apr 30, 2016 at 13:53
  • $\begingroup$ I don't understand very well what the f1 function does, could you explain it to me, please? $\endgroup$
    – ablagi
    May 2, 2016 at 17:11
  • $\begingroup$ @PPeg, if you look at the FullForm of the expression in your post using FullForm[(I*(D[#1, x] &))[3 x y z]] you see that it is seen by mma as Times[Complex[0,1], Function[D[Slot[1],x]]][Times[3,x,y,z]] which has the pattern head[arg1][arg2]. The function f1 transforms such patterns by replacing any Function object, i.e., Function[D[Slot[1], x]] within args1 with Function[D[Slot[1], x]][args2]; that is, it pushes args2 inside the parantheses into the appropriate place. Hope this helps. $\endgroup$
    – kglr
    May 2, 2016 at 17:26
  • $\begingroup$ Thank you very much. This could be the answer to my question. I will work a bit on it and eventually close the question. $\endgroup$
    – ablagi
    May 3, 2016 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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