The function should be evaluating at j=1 but I am getting j in the final output as a variable

I have the following function:

g [j_, k_] := Sum[Sum[Binomial[j, μ]  Binomial[k, ν] ((1/2)^(j + k) (-1)^(ν + Floor[1/2 k])  B[k])/((β τ + 1)^2 + φ^2 (j + k - 2 μ - 2 ν)^2), {ν, 0,k}], {μ, 0, j}];

where, B[k] is defined by:

B[k_] := If[ IntegerQ[k/2], β τ + 1, (j + k - 2 μ - 2 ν) φ]; 

However, if I try and evaluate the following expression:

gp[j_, k_] := ρ  g[ j, k];

I get a j which clearly should have been set to 1

I've tried various combinations of Evaluate[] and := assignments but none seem to be working. I can get temporary correct behaviour by doing B[j_, k_] := Evaluate[...] and calling B[j,k] from g[j_, k_] but this then reverts to a failed state when I save it and call as a packaged function

Expected Output

The expected output is:

(ρ φ)/((1 + β τ)^2 + 4 φ^2)

Instead I get:

ρ (-(((-3 + j) φ)/(4 ((1 + β τ)^2 + 4 φ^2))) + ((1 + j) φ)/(4 ((1 + β τ)^2 + 4 φ^2)))
  • $\begingroup$ How do you expect j in B to be resolved? Also, avoid using uppercase initials (e.g. B) for your symbols... $\endgroup$
    – ciao
    Aug 5, 2016 at 0:48
  • $\begingroup$ I expect j=1 for all j in B. I expect that before g[j=1,k=1] is evaluated that B[k=1] will be evaluated. Then that g will assign values of j=k=1 to all j,k and then will evaluate the Sum and Binomial - does that make sense? $\endgroup$ Aug 5, 2016 at 1:12
  • 2
    $\begingroup$ I understand that's what you expect. I meant how do you expect Mathematica to resolve this? Your "...a j which clearly should have been set to 1..." is clearly flawed logic. Take the time to read the documentation and the tutorials here, understand the scoping and pattern transformation mechanisms of the product. Treating it like some other language and making faulty assumptions will just lead to silly problems like this. $\endgroup$
    – ciao
    Aug 5, 2016 at 1:18

1 Answer 1


The issue you are experiencing is that you define j as a local variable to the functions gp and g but using it as a global variable in your function B. So in effect it is undefined for use with B.

Defining j local to b will resolve your issue. I also changed B[k_] into lowercase, so your new function header is b[j_,k_] (also in your function g) as per Mathematica conventions.

b[j_, k_] := 
  If[IntegerQ[k/2], β τ + 1, (j + k - 2 μ - 2ν)/ φ];

g[j_, k_] := 
   Binomial[j,μ] Binomial[k,ν] ((1/2)^(j + k) (-1)^(ν + Floor[1/2 k]) b[j,k])/((β τ + 1)^2 
   + φ^2 (j + k - 2 μ - 2 ν)^2)
  , {ν, 0, k}]
 , {μ, 0, j}];

Returns what you are expecting:

(ρ φ)/((1 + β τ)^2 + 4 φ^2)

As @ciao stated; it is recommended to not use uppercase in function definitions, and especially not for symbols. This is the reason for using b instead of B. Although in your case using an uppercase symbol does not cause any problems, check the result of ?D as an example why it is not a good idea to use uppercase initials).

  • $\begingroup$ Thanks for the explanation. Mathematica lets me get away with things I can't in C or even python so I get a little carried away with the lack of static typing as sometimes it's not totally intuitive when the global /local boundaries are set $\endgroup$ Aug 5, 2016 at 1:27
  • 1
    $\begingroup$ Yes, it's beautiful :) $\endgroup$
    – Sander
    Aug 5, 2016 at 1:29
  • $\begingroup$ ... ignore previous edit - unrelated to this question - your solution has no flaw for packages $\endgroup$ Aug 5, 2016 at 1:39

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.