0
$\begingroup$

I have defined a recursive function W[g_,n_], with the base case values W[0,3] and W[1,1].

Now I want to total W[g,n] for fixed values of 2g-2+n, which I do like so:

pairs[k_] := Solve[2 g - 2 + n == k && g >= 0 && n > 0, {g, n}, Integers]

S[m_]:= Total[W[g, n] /. pairs[m - 1]]

This seems to be working, except when I compute S[2], which should be W[1,1]+W[0,3]. Instead I get zero.

I see that both

W[g, n] /. {g -> 0, n -> 3}
W[g, n] /. {g -> 1, n -> 1}

give me zero, when I have defined these values, and they are not zero.

$\endgroup$

closed as off-topic by José Antonio Díaz Navas, Henrik Schumacher, m_goldberg, MarcoB, b3m2a1 Apr 19 '18 at 2:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – José Antonio Díaz Navas, Henrik Schumacher, m_goldberg, MarcoB, b3m2a1
If this question can be reworded to fit the rules in the help center, please edit the question.

1
$\begingroup$

Your function S[m] isn't well defined. The function pairs[k] returns a list of rules for g and n, so W[g,n] does not know what are the values for them.

This definition solves your problem:

S[m_] := Total@(W[Sequence @@ #] & /@ ({g, n} /. pairs[m - 1]))
$\endgroup$
  • $\begingroup$ Thank you! But I don't quite understand the reason. Why shouldn't the replacement rule work? And why does it seems to work for all other values of g and n? $\endgroup$ – PK225 Apr 13 '18 at 19:06
  • $\begingroup$ Because, g and n are variables of the function W not parts of an expression given by W[g,n]. On the other hand, what you mean with "all other values of g and n"? $\endgroup$ – José Antonio Díaz Navas Apr 13 '18 at 19:09
  • $\begingroup$ I don't have a problem with my code for values other than (g,n) = (1,1) and (g,n) = (0,3). But I suppose that makes sense given what you just said, if a recursive function is interpreted as an expression for values other than the base case. $\endgroup$ – PK225 Apr 13 '18 at 19:24

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