# Compute power series

I would like to compute $\sum_{k=0}^\infty (1+(-1)^k) x^k$. In Mathematica, one can do

In[1]:= Sum[(1+(-1)^k)*x^k,{k,0,Infinity}]

-2
Out[1]= -------
2
-1 + x


How does it work in Sage? I tried

sage: var('k')
k
sage: sum((1+(-1)^k)*x^k, k, 0, oo)
sum(((-1)^k + 1)*x^k, k, 0, +Infinity)


which doesn't give me any new information.

edit retag close merge delete

Sort by ยป oldest newest most voted

For example

giac('normal(sum((1+(-1)^k)*x^k,k,0,inf))').sage()

-2/(x^2 - 1)

more

1

Nice ! I didn't know this one.

Alternate way to express, possibly more "natural"' to Python/Sage :

sage: sum((1+(-1)^k)*x^k, k, 0, oo)._giac_().normal()._sage_()
-2/(x^2 - 1)


Note a convergence condition given by Sympy's solution (currently not directly translatable in Sage) :

sage: sum((1+(-1)^k)*x^k, k, 0, oo)._sympy_().doit()
Piecewise((1/(1 - x), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True)) + Piecewise((1/(x + 1), Abs(x) < 1), (Sum((-1)**k*x**k, (k, 0, oo)), True))


( 2023-07-09 15:51:52 +0200 )edit

Thank you both!

( 2023-07-09 16:35:17 +0200 )edit