How do I evaluate sum() containing factorial()?
I am trying to evaluate a sum containing a factorial, but need to do copy and paste of the interim result to get the final answer in the sage notebook (ver. 5.7). Is there a direct way?
var('i k n t')
sum(factorial(3-i)*k^i*t^i, i,0,n)(k=1, n=3, t=4)
sum(4^i*factorial(-i + 3), i, 0, 3)
If I copy the result into a new input cell and evaluate:
sum(4^i*factorial(-i + 3), i, 0, 3)
only then I obtain the desired
94
This is annoying, as I would like to compute the result for a long list of n and t and plot the results.
EDIT: Maybe I simplifed the question too much. Just to specify again why I would like to use symbolics: I actually wanted to evaluate
var('i k n t')
sum(factorial(n-i)*k^i*t^i, i,0,n)
for different values of n and get the symbolic result, e.g. for n = 3, I would expect:
3*k^3*t^3 + 2*k^2*t^2 + k*t
EDIT2: Betrema's edited solution is very helpful:
[sum(factorial(n-i)*k^i*t^i, i, 0, n) for n in range(3)]
gives
[1, k*t + 1, k^2*t^2 + k*t + 2]
as desired. The only remaining question is: Why does sum(factorial(n-i)k^it^i, i, 0, n)(n=3) not give k^2t^2 + kt + 2? Does the .subs() method work differently on symbolic sums than on other symbolic equations? Thanks again!
Playing with ppurka's idea, I narrowed it down a little bit: if I replace the symbolic variable n by a value, the term evaluates: n1 = 3 sum(factorial(3-i)*k^i*t^i, i,0,n1)(k=1, t=4) gives 94. Is this a bug in sum()? All other symbolic functions substitute the variables in brackets and evaluate.