Ask Your Question
1

How do I evaluate sum() containing factorial()?

asked 2013-03-22 17:15:40 +0100

stan gravatar image

updated 2014-11-10 16:39:19 +0100

tmonteil gravatar image

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!

edit retag flag offensive close merge delete

Comments

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.

stan gravatar imagestan ( 2013-03-25 04:13:24 +0100 )edit

3 Answers

Sort by » oldest newest most voted
2

answered 2013-03-23 12:03:00 +0100

updated 2013-03-27 04:55:52 +0100

"Why do stuff in symbolics", I agree with ppurka, and I suggest:

def sumfact (k, n, t):
    return sum (factorial (n-i) * k^i * t^i for i in range (n+1))

which uses the built-in Python function sum.

[edit] After precisions added by 'stan', I'm puzzled, Sage gives me the (not very surprising) expected answers:

sage: var ('i k t')
sage: for n in range (5):
...       print sum (factorial(n-i)*k^i*t^i, i, 0, n)
1
k*t + 1
k^2*t^2 + k*t + 2
k^3*t^3 + k^2*t^2 + 2*k*t + 6
k^4*t^4 + k^3*t^3 + 2*k^2*t^2 + 6*k*t + 24
edit flag offensive delete link more

Comments

Thanks, but please see my reply to ppurka's idea. In addition, your idea with using a list for the summation even throws an error why trying to substitute values for variables: ValueError: cannot convert n + 1 to int. Any ideas why the symbolic function does not evaluate? EDIT: Thanks, this is very helpful. I further narrowed down my question based on this.

stan gravatar imagestan ( 2013-03-25 04:06:45 +0100 )edit
1

answered 2013-03-22 17:30:23 +0100

ppurka gravatar image

Write a function. Why do stuff in symbolics when you don't need them to be so? Working with symbolic expressions is also very slow.

def sumf(k,n,t):
    return sum(factorial(3-i)*k^i*t^i, i,0,n)
edit flag offensive delete link more

Comments

Thanks, but this only works if I assign numerical values to k, n and t before evaluating sumf(k,n,t). There are a few reasons why I use symbolic functions, and one of them is that I can make all sorts of symbolic derivations, display the results in symbolic form so that a reader not familiar with Python can roughly follow and then just substitute values for the symbols using a dictionary. In this case, I would have a dictionary, e.g. vdict = {n: 2, t: 10, k: 0.1} and would like to get the solution by sumf(k,n,t).subs(vdict). Unfortunately, defining a Python function does not help here, the result is the same as in my above example, using symbolics.

stan gravatar imagestan ( 2013-03-25 03:55:50 +0100 )edit
0

answered 2013-03-27 04:57:40 +0100

The answer to the 'howto' question is: don't use symbolic calculus when it's not necessary (see preceding answers). Here is a tentative answer to the 'why' question. Yes there are special rules for evaluation of symbolic sums:

sage: var ('i k n t')
sage: sum(factorial(n+1-i)*k^i*t^i,i,0,n)
sum(k^i*t^i*factorial(-i + n + 1), i, 0, n)
sage: sum(factorial(n+1-i)*k^i*t^i,i,0,n)(n=3)
sum(k^i*t^i*factorial(-i + 4), i, 0, 3)

This is indeed surprising, but think of:

sage: sum(factorial(n+1-i)*k^i*t^i,i,0,n)(n=100)
sum(k^i*t^i*factorial(-i + 101), i, 0, 100)

Do you prefer this symbolic expression, or its full expansion? Hence there is no perfect strategy for evaluation of symbolic expressions, and there are also theoretical obstacles because canonical forms can't exist for symbolic expressions, even for very narrow classes.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-03-22 17:15:40 +0100

Seen: 4,596 times

Last updated: Mar 27 '13