ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 27 Mar 2013 04:57:40 +0100How do I evaluate sum() containing factorial()?https://ask.sagemath.org/question/9937/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^i*t^i, i, 0, n)(n=3)
not give k^2*t^2 + k*t + 2?**
Does the .subs() method work differently on symbolic sums than on other symbolic equations?
Thanks again!
Fri, 22 Mar 2013 17:15:40 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/Comment by stan for <p>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?</p>
<pre><code>var('i k n t')
sum(factorial(3-i)*k^i*t^i, i,0,n)(k=1, n=3, t=4)
</code></pre>
<blockquote>
<p>sum(4^i*factorial(-i + 3), i, 0, 3)</p>
</blockquote>
<p>If I copy the result into a new input cell and evaluate:</p>
<pre><code>sum(4^i*factorial(-i + 3), i, 0, 3)
</code></pre>
<p>only then I obtain the desired</p>
<blockquote>
<p>94</p>
</blockquote>
<p>This is annoying, as I would like to compute the result for a long list of n and t and plot the results.</p>
<p>EDIT:
Maybe I simplifed the question too much. Just to specify again why I would like to use symbolics: I actually wanted to evaluate</p>
<pre><code>var('i k n t')
sum(factorial(n-i)*k^i*t^i, i,0,n)
</code></pre>
<p>for different values of n and get the symbolic result, e.g. for n = 3, I would expect:</p>
<pre><code>3*k^3*t^3 + 2*k^2*t^2 + k*t
</code></pre>
<p><strong>EDIT2:</strong> Betrema's edited solution is very helpful:</p>
<pre><code>[sum(factorial(n-i)*k^i*t^i, i, 0, n) for n in range(3)]
</code></pre>
<p>gives</p>
<pre><code>[1, k*t + 1, k^2*t^2 + k*t + 2]
</code></pre>
<p>as desired. <strong>The only remaining question is:
Why does sum(factorial(n-i)<em>k^i</em>t^i, i, 0, n)(n=3)
not give k^2<em>t^2 + k</em>t + 2?</strong>
Does the .subs() method work differently on symbolic sums than on other symbolic equations?
Thanks again!</p>
https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?comment=18018#post-id-18018Playing 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.Mon, 25 Mar 2013 04:13:24 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?comment=18018#post-id-18018Answer by ppurka for <p>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?</p>
<pre><code>var('i k n t')
sum(factorial(3-i)*k^i*t^i, i,0,n)(k=1, n=3, t=4)
</code></pre>
<blockquote>
<p>sum(4^i*factorial(-i + 3), i, 0, 3)</p>
</blockquote>
<p>If I copy the result into a new input cell and evaluate:</p>
<pre><code>sum(4^i*factorial(-i + 3), i, 0, 3)
</code></pre>
<p>only then I obtain the desired</p>
<blockquote>
<p>94</p>
</blockquote>
<p>This is annoying, as I would like to compute the result for a long list of n and t and plot the results.</p>
<p>EDIT:
Maybe I simplifed the question too much. Just to specify again why I would like to use symbolics: I actually wanted to evaluate</p>
<pre><code>var('i k n t')
sum(factorial(n-i)*k^i*t^i, i,0,n)
</code></pre>
<p>for different values of n and get the symbolic result, e.g. for n = 3, I would expect:</p>
<pre><code>3*k^3*t^3 + 2*k^2*t^2 + k*t
</code></pre>
<p><strong>EDIT2:</strong> Betrema's edited solution is very helpful:</p>
<pre><code>[sum(factorial(n-i)*k^i*t^i, i, 0, n) for n in range(3)]
</code></pre>
<p>gives</p>
<pre><code>[1, k*t + 1, k^2*t^2 + k*t + 2]
</code></pre>
<p>as desired. <strong>The only remaining question is:
Why does sum(factorial(n-i)<em>k^i</em>t^i, i, 0, n)(n=3)
not give k^2<em>t^2 + k</em>t + 2?</strong>
Does the .subs() method work differently on symbolic sums than on other symbolic equations?
Thanks again!</p>
https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?answer=14675#post-id-14675Write 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)Fri, 22 Mar 2013 17:30:23 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?answer=14675#post-id-14675Comment by stan for <p>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.</p>
<pre><code>def sumf(k,n,t):
return sum(factorial(3-i)*k^i*t^i, i,0,n)
</code></pre>
https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?comment=18020#post-id-18020Thanks, 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. Mon, 25 Mar 2013 03:55:50 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?comment=18020#post-id-18020Answer by Bétréma for <p>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?</p>
<pre><code>var('i k n t')
sum(factorial(3-i)*k^i*t^i, i,0,n)(k=1, n=3, t=4)
</code></pre>
<blockquote>
<p>sum(4^i*factorial(-i + 3), i, 0, 3)</p>
</blockquote>
<p>If I copy the result into a new input cell and evaluate:</p>
<pre><code>sum(4^i*factorial(-i + 3), i, 0, 3)
</code></pre>
<p>only then I obtain the desired</p>
<blockquote>
<p>94</p>
</blockquote>
<p>This is annoying, as I would like to compute the result for a long list of n and t and plot the results.</p>
<p>EDIT:
Maybe I simplifed the question too much. Just to specify again why I would like to use symbolics: I actually wanted to evaluate</p>
<pre><code>var('i k n t')
sum(factorial(n-i)*k^i*t^i, i,0,n)
</code></pre>
<p>for different values of n and get the symbolic result, e.g. for n = 3, I would expect:</p>
<pre><code>3*k^3*t^3 + 2*k^2*t^2 + k*t
</code></pre>
<p><strong>EDIT2:</strong> Betrema's edited solution is very helpful:</p>
<pre><code>[sum(factorial(n-i)*k^i*t^i, i, 0, n) for n in range(3)]
</code></pre>
<p>gives</p>
<pre><code>[1, k*t + 1, k^2*t^2 + k*t + 2]
</code></pre>
<p>as desired. <strong>The only remaining question is:
Why does sum(factorial(n-i)<em>k^i</em>t^i, i, 0, n)(n=3)
not give k^2<em>t^2 + k</em>t + 2?</strong>
Does the .subs() method work differently on symbolic sums than on other symbolic equations?
Thanks again!</p>
https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?answer=14677#post-id-14677"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
Sat, 23 Mar 2013 12:03:00 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?answer=14677#post-id-14677Comment by stan for <p>"Why do stuff in symbolics", I agree with ppurka, and I suggest:</p>
<pre><code>def sumfact (k, n, t):
return sum (factorial (n-i) * k^i * t^i for i in range (n+1))
</code></pre>
<p>which uses the built-in Python function sum.</p>
<p>[edit] After precisions added by 'stan', I'm puzzled, Sage gives me the (not very surprising) expected answers:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?comment=18019#post-id-18019Thanks, 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.Mon, 25 Mar 2013 04:06:45 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?comment=18019#post-id-18019Answer by Bétréma for <p>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?</p>
<pre><code>var('i k n t')
sum(factorial(3-i)*k^i*t^i, i,0,n)(k=1, n=3, t=4)
</code></pre>
<blockquote>
<p>sum(4^i*factorial(-i + 3), i, 0, 3)</p>
</blockquote>
<p>If I copy the result into a new input cell and evaluate:</p>
<pre><code>sum(4^i*factorial(-i + 3), i, 0, 3)
</code></pre>
<p>only then I obtain the desired</p>
<blockquote>
<p>94</p>
</blockquote>
<p>This is annoying, as I would like to compute the result for a long list of n and t and plot the results.</p>
<p>EDIT:
Maybe I simplifed the question too much. Just to specify again why I would like to use symbolics: I actually wanted to evaluate</p>
<pre><code>var('i k n t')
sum(factorial(n-i)*k^i*t^i, i,0,n)
</code></pre>
<p>for different values of n and get the symbolic result, e.g. for n = 3, I would expect:</p>
<pre><code>3*k^3*t^3 + 2*k^2*t^2 + k*t
</code></pre>
<p><strong>EDIT2:</strong> Betrema's edited solution is very helpful:</p>
<pre><code>[sum(factorial(n-i)*k^i*t^i, i, 0, n) for n in range(3)]
</code></pre>
<p>gives</p>
<pre><code>[1, k*t + 1, k^2*t^2 + k*t + 2]
</code></pre>
<p>as desired. <strong>The only remaining question is:
Why does sum(factorial(n-i)<em>k^i</em>t^i, i, 0, n)(n=3)
not give k^2<em>t^2 + k</em>t + 2?</strong>
Does the .subs() method work differently on symbolic sums than on other symbolic equations?
Thanks again!</p>
https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?answer=14694#post-id-14694The 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.
Wed, 27 Mar 2013 04:57:40 +0100https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/?answer=14694#post-id-14694