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.

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:

Clear[rule];
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

Clear[hh];
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

result

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?

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

1 Answer 1

Edit

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.

Clear[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

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

and

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}]

gives

{{{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}}}
share|improve this answer
    
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. –  matheorem Jul 7 at 12:13
    
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? –  matheorem Jul 7 at 12:19
    
@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 –  m_goldberg Jul 7 at 14:03
    
Thank you very much! –  matheorem Jul 7 at 14:42
    
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]; –  matheorem Jul 7 at 14:57

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.