1
$\begingroup$

The following code

f[k_, n_] := 
Sum[2*(1 + Sum[f[k - j, n - m - j], {j, 1, k - 1}]), {m, 1, 
n - k + 1}]
f[4, 7]
f[4, n]

gives the output:

192
-(2/3) (261 - 342 n + 157 n^2 - 30 n^3 + 2 n^4)

But if I then define the function

g[n_] := -(2/3) (261 - 342 n + 157 n^2 - 30 n^3 + 2 n^4)

And evaluate it at $n=7$ I get a different answer:

g[7]

gives the output:

-48

What is going on here?

$\endgroup$
2
  • $\begingroup$ Your definition for f seems to be missing some boundary conditions. $\endgroup$ Jun 8, 2015 at 13:17
  • 1
    $\begingroup$ I thought that the fact that the empty sum is assigned the value 0 would be enough to take care of boundary conditions, but perhaps I'm wrong. For example f[1,n] correctly gives the output $2n$. $\endgroup$ Jun 8, 2015 at 13:35

1 Answer 1

3
$\begingroup$

I think that in the simplification of your sum, Mathematica is assuming that $n - k + 1\geq1$. But that is not true, if you include a Sow:

f[k_, n_] := 
Sum[2*(1 +Sum[Sow[{n - k + 1}]; f[k - j, n - m - j], {j, 1, k - 1}]), {m,1, n - k + 1}]

Then the issue probably starts at $n=4$, as that includes a negative endpoint.

Reap[f[4, n]] /. m -> (n - 4 + 1) /. n -> 7
{-48, {{{4}, {0}, {-4}, {0}, {4}, {0}, {4}}}}
$\endgroup$
2
  • 1
    $\begingroup$ I see. So how would I go about calculating the polynomials $f[k,n]$ for fixed $k$, and at the same time avoiding this problem? I would like to be able to input a $k$ (e.g. $k=4$) and get $f[k,n]$ as a polynomial in $n$ as an output. Is that at all possible? $\endgroup$ Jun 8, 2015 at 14:13
  • $\begingroup$ I don't know the answer to that immediately but I suspect that one of the experts could come along and help. I don't have the rep to comment yet (which seems silly to me). $\endgroup$
    – SPPearce
    Jun 8, 2015 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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