A short investigation of power series in Sage. More details can be found in the Sage documentation or built-in documentation.
Requirements: None
# Power series rings in Sage can be defined similarly to polynomial rings
# Here we define power series expansions in the variable z over the rational numbers
QQ[['z']]
Power Series Ring in z over Rational Field
# Currently, entering "z" would give "NameError: name 'z' is not defined"
# To compute with such series, the user needs to tell Sage the variable "z" is the generator of a power series ring
# This can be done during construction of the ring as follows
R.<z> = QQ[['z']]
# R is set as the power series ring
R
Power Series Ring in z over Rational Field
# And z represents the indeterminate of this ring
parent(z)
Power Series Ring in z over Rational Field
# Sage will automatically simplify expressions which make sense in the power series ring
print(1/(1-z))
1 + z + z^2 + z^3 + z^4 + z^5 + z^6 + z^7 + z^8 + z^9 + z^10 + z^11 + z^12 + z^13 + z^14 + z^15 + z^16 + z^17 + z^18 + z^19 + O(z^20)
# Another example
sin(z/(1-z))
z + z^2 + 5/6*z^3 + 1/2*z^4 + 1/120*z^5 - 5/8*z^6 - 6931/5040*z^7 - 1591/720*z^8 - 224179/72576*z^9 - 31987/8064*z^10 - 27323293/5702400*z^11 - 19986991/3628800*z^12 - 1508675351/249080832*z^13 - 3055361231/479001600*z^14 - 8385570334051/1307674368000*z^15 - 16115825351/2641766400*z^16 - 1916072623603199/355687428096000*z^17 - 88344037902079/20922789888000*z^18 - 12475149135090259/4865804016353280*z^19 - 2415972748554847/6402373705728000*z^20 + O(z^21)
# The power series ring itself can be used in computations
print(R.is_commutative())
print(R.random_element(10))
True 3*z^2 - 2*z^3 - 7*z^4 + z^5 - 2*z^6 - 3/4*z^7 + 1/2*z^8 + 1/2*z^9 + O(z^10)
# Computations automatically track precision
f = R.random_element(3)
g = R.random_element(4)
print("The series [{}] * [{}] = {}".format(f,g,f*g))
The series [35 + 1/2*z - 2*z^2 + O(z^3)] * [2 + 1/5*z - 4*z^2 - z^3 + O(z^4)] = 70 + 8*z - 1439/10*z^2 + O(z^3)
# WARNING: equality between formal series is only checked up to the lowest precision
print(1 + z + z^3 + O(z^4) == 1 + z + z^2 + O(z^4))
print(1 + z + O(z^2) == 1 + z + z^2 + O(z^4))
False True