2
$\begingroup$

I want to calculate a derivative such as: $\delta f=\sum_{i=A,B} \sum_{k=1,2,3} \frac{\partial f}{\partial a_k^i}$. Since $ a_k^i$ are assigned values other places in the code, I have used Block. I obtain correct results for $\delta f$ directly calculating a function. However, when I define a function deltaf which is suppose to do the same thing I don't get the correct results.

Here are the values of a[i,k]

a[A, 1] = 1; a[A, 2] = 2; a[A, 3] = 3;
a[B, 1] = 5; a[B, 2] = 10; a[B, 3] = 15;

As a sample function for which I want the derivative of, I use f1[i,x]

f1[i_, x_] := (1 - x)*Sum[a[i, k]^2, {k, 1, 3}]

Using Block works for getting the derivative of f1[A,x] with respect to a[i,k] (I emphasize that I'm calculating f1[A,x] and not f1[i,x]):

Block[{a},Sum[D[f1[A, x], a[i, k]], {i, {A, B}}, {k, {1, 2, 3}}]]
12 (1 - x)

However if I define a function that does the same thing:

deltaf[f_] := Block[{a}, Sum[D[f, a[i, k]], {i, {A, B}}, {k, {1, 2, 3}}]]

It doesn't give the correct value anymore:

deltaf[f1[A, x]]
0

Why is this happening? How is it that when I calculate the same thing directly I get a correct answer but not when defined as a function?

$\endgroup$
  • $\begingroup$ Hi ! As this is a Q&A site you should also explicitly state what are you trying to do - e.g ask a question, not just leave it blank. $\endgroup$ – Sektor Apr 21 '15 at 11:36
  • $\begingroup$ Sorry, this is my first question. My question is exactly why this is happening. In other words the same thing as mentioned in the title: "Why does Block not work when used as a function? But it works when directly used? " $\endgroup$ – nein Apr 21 '15 at 11:39
  • $\begingroup$ Did you try deltaf[Evaluate[f1[A, x]]]? $\endgroup$ – Sosi Apr 21 '15 at 11:47
  • $\begingroup$ Yes I did but it still didn't work! @Sosi $\endgroup$ – nein Apr 21 '15 at 11:49
  • 1
    $\begingroup$ Actually, I think you want the exact opposite of what I was suggesting. Notice that when you do deltaf[f1[A,x]] you are evaluating D[f, a[i, k]] (=14 (1 - x)) AND THEN using this result to compute the Sum. This is why the derivative then gives 0, because you are evaluating the derivatives of 14(1-x) with respect to a[i,k]. $\endgroup$ – Sosi Apr 21 '15 at 13:29
3
$\begingroup$

(this answer resumes the discussions over at the comments and over the chat. Thanks to @IstvánZachar for helping out and for suggesting my posting it as answer).

The problem is that deltaf[f1[A,x]] evaluates f1[A,x] (=14 (1 - x)) before actually computing Sum[D[f, a[i, k]], {i, {A, B}}, {k, {1, 2, 3}}]. Specifying that the attributes of deltaf are to remain unevaluated solves the problem:

SetAttributes[deltaf, HoldAll];
deltaf[f_] := Block[{a}, Sum[D[f, a[i, k]], {i, {A, B}}, {k, {1, 2, 3}}]];

deltaf[f1[A, x]]
12 (1 - x)
$\endgroup$

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.