ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 20 Dec 2017 12:49:25 -0600working with coefficients of formal serieshttp://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/ I want to define a truncated series or polynomial of arbitrary degree and then work algebraically with the polynomial to solve for various quantities in terms of the coefficients. When I write something like
i,n,z=var('i,n,z')
c=function('c')
p=z^(-4)+sum(c(i)/z^i,i,0,2)
p
this returns
(z^2*c(0) + z*c(1) + c(2))/z^2 + 1/z^4
But if I try to define an arbitrary polynomial of this type
p(n)=z^(-2n) + sum(c(i)/z^i,i,0,2n-2)
p(2)
returns
z^4 + sum(z^(-i)*c(i), i, 0, 2)
What is the crucial difference here?
I had a similar problem when working with truncated power series over the ring `PowerSeriesRing(SR)`. I want to manipulate these expressions algebraically as elements of a ring then ask for info about certain coefficients. But working with formal sums and substituting values of n returns power series coefficients, e.g. the following expression as the coefficient of z^-4 (after some computations...f and g are symbolic functions)
1/4*(sum(z^i*f(i), i, 1, 3)*sum(z^i*g(i), i, 1, 3) + 2)^2 + sum(z^i*f(i), i, 1, 3)*sum(z^i*g(i), i, 1, 3)
How do I get sage to work with the series and also give info about the coefficients, i.e. multiply series expressions but then expand them out in z? I've tried `expand()` in this setting with mixed success.Wed, 20 Dec 2017 08:44:55 -0600http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/Answer by B r u n o for <p>I want to define a truncated series or polynomial of arbitrary degree and then work algebraically with the polynomial to solve for various quantities in terms of the coefficients. When I write something like</p>
<pre><code>i,n,z=var('i,n,z')
c=function('c')
p=z^(-4)+sum(c(i)/z^i,i,0,2)
p
</code></pre>
<p>this returns</p>
<pre><code>(z^2*c(0) + z*c(1) + c(2))/z^2 + 1/z^4
</code></pre>
<p>But if I try to define an arbitrary polynomial of this type</p>
<pre><code>p(n)=z^(-2n) + sum(c(i)/z^i,i,0,2n-2)
p(2)
</code></pre>
<p>returns</p>
<pre><code>z^4 + sum(z^(-i)*c(i), i, 0, 2)
</code></pre>
<p>What is the crucial difference here?</p>
<p>I had a similar problem when working with truncated power series over the ring <code>PowerSeriesRing(SR)</code>. I want to manipulate these expressions algebraically as elements of a ring then ask for info about certain coefficients. But working with formal sums and substituting values of n returns power series coefficients, e.g. the following expression as the coefficient of z^-4 (after some computations...f and g are symbolic functions)</p>
<pre><code>1/4*(sum(z^i*f(i), i, 1, 3)*sum(z^i*g(i), i, 1, 3) + 2)^2 + sum(z^i*f(i), i, 1, 3)*sum(z^i*g(i), i, 1, 3)
</code></pre>
<p>How do I get sage to work with the series and also give info about the coefficients, i.e. multiply series expressions but then expand them out in z? I've tried <code>expand()</code> in this setting with mixed success.</p>
http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?answer=40257#post-id-40257Is the following what you need?
sage: f = function('f')
sage: g = function('g')
sage: R.<z> = PowerSeriesRing(SR)
sage: F = sum(f(i)*z^i for i in range(3)) + O(z^3)
sage: G = sum(g(i)*z^i for i in range(3)) + O(z^3)
sage: F*G
f(0)*g(0) + (f(0)*g(1) + f(1)*g(0))*z + (f(0)*g(2) + f(1)*g(1) + f(2)*g(0))*z^2 + O(z^3)
Wed, 20 Dec 2017 08:56:06 -0600http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?answer=40257#post-id-40257Comment by B r u n o for <p>Is the following what you need?</p>
<pre><code>sage: f = function('f')
sage: g = function('g')
sage: R.<z> = PowerSeriesRing(SR)
sage: F = sum(f(i)*z^i for i in range(3)) + O(z^3)
sage: G = sum(g(i)*z^i for i in range(3)) + O(z^3)
sage: F*G
f(0)*g(0) + (f(0)*g(1) + f(1)*g(0))*z + (f(0)*g(2) + f(1)*g(1) + f(2)*g(0))*z^2 + O(z^3)
</code></pre>
http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40261#post-id-40261Now another approach, fully working with symbolics, is a follows:
sage: var('i,n,z')
(i, n, z)
sage: f = function('f')
sage: g = function('g')
sage: F = sum(f(i)*z^i, i, 0, n)
sage: G = sum(g(i)*z^i, i, 0, n)
sage: (F*G).subs(n=3).expand_sum().collect(z)
z^6*f(3)*g(3) + (f(2)*g(3) + f(3)*g(2))*z^5 + (f(1)*g(3) + f(2)*g(2) + f(3)*g(1))*z^4 + (f(0)*g(3) + f(1)*g(2) + f(2)*g(1) + f(3)*g(0))*z^3 + (f(0)*g(2) + f(1)*g(1) + f(2)*g(0))*z^2 + (f(0)*g(1) + f(1)*g(0))*z + f(0)*g(0)
Note though that you aren't working with power series, but with polynomials in `z`.Wed, 20 Dec 2017 12:49:25 -0600http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40261#post-id-40261Comment by B r u n o for <p>Is the following what you need?</p>
<pre><code>sage: f = function('f')
sage: g = function('g')
sage: R.<z> = PowerSeriesRing(SR)
sage: F = sum(f(i)*z^i for i in range(3)) + O(z^3)
sage: G = sum(g(i)*z^i for i in range(3)) + O(z^3)
sage: F*G
f(0)*g(0) + (f(0)*g(1) + f(1)*g(0))*z + (f(0)*g(2) + f(1)*g(1) + f(2)*g(0))*z^2 + O(z^3)
</code></pre>
http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40260#post-id-402601. I don't think you can leave `n` unspecified and in the same time get it in explicit form in `z`.
2. There are (in some sense) two different functions called `sum`:
- `sum(ex, i, start, end)` gives you the symbolic sum made of the expression `ex` where `i` goes from `start` to `end`. Sometimes, SageMath knows how to simplify it, as in `sum(i, i, 0, n)`.
- `sum(it)` where `it` is an iterator (`ex for i in range(12)` for instance) is the standard sum function from Python, that sums a definite number of terms. You cannot in this case have a number of terms that is a variable as in the first case.
My example uses the second function, while you use the first one. That makes the difference in the results.Wed, 20 Dec 2017 12:47:40 -0600http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40260#post-id-40260Comment by charleslebarron for <p>Is the following what you need?</p>
<pre><code>sage: f = function('f')
sage: g = function('g')
sage: R.<z> = PowerSeriesRing(SR)
sage: F = sum(f(i)*z^i for i in range(3)) + O(z^3)
sage: G = sum(g(i)*z^i for i in range(3)) + O(z^3)
sage: F*G
f(0)*g(0) + (f(0)*g(1) + f(1)*g(0))*z + (f(0)*g(2) + f(1)*g(1) + f(2)*g(0))*z^2 + O(z^3)
</code></pre>
http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40259#post-id-40259But also, what makes the difference in whether Sage returns the expanded expression or the just something like
sum(z^i*f(i), i, 1, 2)*sum(z^i*g(i), i, 1, 2)Wed, 20 Dec 2017 11:03:05 -0600http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40259#post-id-40259Comment by charleslebarron for <p>Is the following what you need?</p>
<pre><code>sage: f = function('f')
sage: g = function('g')
sage: R.<z> = PowerSeriesRing(SR)
sage: F = sum(f(i)*z^i for i in range(3)) + O(z^3)
sage: G = sum(g(i)*z^i for i in range(3)) + O(z^3)
sage: F*G
f(0)*g(0) + (f(0)*g(1) + f(1)*g(0))*z + (f(0)*g(2) + f(1)*g(1) + f(2)*g(0))*z^2 + O(z^3)
</code></pre>
http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40258#post-id-40258Ideally, we would leave n as a variable, only specifying n=3 when necessary to return meaningful results. Something more like:
F(n)=sum(f(i)*z^i,i,1,n)
G(n)=sum(g(i)*z^i,i,1,n)
F(3)*G(3)
...Wed, 20 Dec 2017 11:00:51 -0600http://ask.sagemath.org/question/40256/working-with-coefficients-of-formal-series/?comment=40258#post-id-40258