This question is related to the question I posted here. In that question, I thought I have simplified my original problem into an equivalent concise version, but I found that it is not. So I decided to open this question.

The goal is the same that I want to define a function hhpar that will differentiate hh with respect to x, while I changed the rule function as follows:

rulelist = {{tt -> 1, zz -> 1}, {tt -> 1, zz -> 2}, {tt -> 2, 
    zz -> 1}, {tt -> 2, zz -> 2}};
rule[i_] := rulelist[[i]];

The hh function is

hh[x_, y_, i_] := {{tt, x + Sin[x]},{y, zz}}/.rule[i];

As suggested by Szabolcs, to defined hhpar as

hhpar[x_,y_,i_]:=Derivative[1, 0, 0][hh][x, y, i]

will solve my former question. But it didn't work in current problem because it only gives


Using Trace, I figure out that the problem is due to the failure of rule replacement before differentiation. But how would I define hhpar now to solve the problem?

  • $\begingroup$ Try with Derivative[1, 0, 0][hh[x, y, i]] $\endgroup$
    – eldo
    Jul 6, 2014 at 11:54
  • $\begingroup$ @eldo Thank you, eldo! But it doesn't work. $\endgroup$
    – matheorem
    Jul 6, 2014 at 12:29
  • $\begingroup$ define it this way: hhpar [x_, y_, i_] := Derivative[1, 0][Evaluate@hh[#1, #2, i] &][x, y] $\endgroup$
    – ecoxlinux
    Jul 7, 2014 at 21:03

1 Answer 1



This edited answer reflects comments made on my original version by the OP.

Clear[rulelist, rule];
rulelist = {{tt -> 1, zz -> 1}, {tt -> 1, zz -> 2}, {tt -> 2, zz -> 1}, {tt -> 2, zz -> 2}};
Table[rule[i] = rulelist[[i]], {i, 4}];

To see where you went wrong let's look at what Derivative returns when it is given your version of hh.

hh[x_, y_, i_] := {{tt, x + Sin[x]}, {y, zz}} /. rule[i]
Derivative[1, 0, 0][hh]

ReplaceAll::reps: {rule[#3]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

D[{{tt, Sin[#1] + #1}, {#2, zz}} /. rule[#3], #1] &

This tells us quite a lot about what is going wrong.

Derivative is trying to take the 1st partial derivative of the 1st independent variable of a function of three variables, and that function is a ReplaceAll expression. There is no way that can work, and it isn't at all what you want anyway. In this case, you want Derivative to return a function that is the 1st partial derivative of the 1st independent variable of a function of two variables.

Clearly hh needs to be redefined to be a function of one variable, the rule index, returning a form which is a function of two variables and where all the desired replacements have been made. While we are at it, we may as well make hh return a pure function. This will avoid any possible scoping issues. We will also generalize hhpar so it can be used to produce any of the 1st order partials of hh[i]. It also turns out that we get the 0 order partial, h[i] itself, for free (hh[i] == hpar[0, 0][#1, #2, i] &).

Clear[hh, hhpar];
hh[i_] = {{tt, #1 + Sin[#1]}, {#2, zz}} & /. rule[i];
hhpar[j_, k_][x_, y_, i_] := Derivative[j, k][hh[i]][x, y]

Now we have the situation where

{{1, #1 + Sin[#1]}, {#2, 1}} &


Derivative[1, 0][hh[1]]
{{0, 1 + Cos[#1]}, {0, 0}} &

exactly as expected.

Computing the 1st partial derivatives gives for all the rules

Table[hhpar[1, 0][x, y, i], {i, 4}]


{{{0, 1 + Cos[x]}, {0, 0}}, {{0, 1 + Cos[x]}, {0, 0}},
 {{0, 1 + Cos[x]}, {0, 0}}, {{0, 1 + Cos[x]}, {0, 0}}}
Table[hhpar[0, 1][x, y, i], {i, 4}]
{{{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}}
  • $\begingroup$ Thank you, m_goldberg. Your definition really works, but I don't understand why my definition gives wrong answer. Could you explain a little? I think I miss a lesson on function definition. $\endgroup$
    – matheorem
    Jul 7, 2014 at 12:13
  • $\begingroup$ And I forgot what this kind of function definition hhpar[j_, k_][x_, y_, i_] is called? Could you show me a link which has a discussion on this kind of function definition? $\endgroup$
    – matheorem
    Jul 7, 2014 at 12:19
  • $\begingroup$ @matheorem. I don't have time now to update my answer to explain how your code goes wrong. I will do so latter today. As for a link on non-symbolic heads, look at this $\endgroup$
    – m_goldberg
    Jul 7, 2014 at 14:03
  • $\begingroup$ I found this works: hh[i_][x_, y_] := {{tt, x + Sin[x]}, {y, 1}} /. rulelist[[i]]; hhpar[x_, y_, i_] := Derivative[1, 0, 0][hh[i][#1, #2] &][x, y]; While this not works: hh[x_, y_, i_] := {{tt, x + Sin[x]}, {y, 1}} /. rulelist[[i]]; hhpar[x_, y_, i_] := Derivative[1, 0, 0][hh[#1, #2, i] &][x, y]; $\endgroup$
    – matheorem
    Jul 7, 2014 at 14:57
  • $\begingroup$ Nice answer. +1 $\endgroup$
    – Mr.Wizard
    Jul 8, 2014 at 0:41

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.