Computing series terms related to a multivariate walk generating function using the ring $\mathbb{Q}[x,\overline{x}][[t]]$.
Requirements: None
# Define the ring where the expansion takes place
A = LaurentPolynomialRing(QQ,'x')[['t']]
A
Power Series Ring in t over Univariate Laurent Polynomial Ring in x over Rational Field
# Print the expansion of the function under consideration
print("The expansion of 1/(1-t(x+1/x)) in Q[x,1/x][[t]] is")
print(1/A("1-t*(x+1/x)").O(5))
The expansion of 1/(1-t(x+1/x)) in Q[x,1/x][[t]] is 1 + (x^-1 + x)*t + (x^-2 + 2 + x^2)*t^2 + (x^-3 + 3*x^-1 + 3*x + x^3)*t^3 + (x^-4 + 4*x^-2 + 6 + 4*x^2 + x^4)*t^4 + O(t^5)
# Setting x = 1 gives the GF for walks on the steps {-1,1} with no restrictions, which has coefficients 2^n
ser = 1/A("1-t*(x+1/x)").O(10)
ser(x=1)
1 + 2*t + 4*t^2 + 8*t^3 + 16*t^4 + 32*t^5 + 64*t^6 + 128*t^7 + 256*t^8 + 512*t^9 + O(t^10)
# The counting sequence for for walks returning to the origin is
coeffs = ser.coefficients()
[k.constant_coefficient() for k in coeffs]
[1, 0, 2, 0, 6, 0, 20, 0, 70, 0]
# This sequence is also encoded by the main power series diagonal of 1/(1-t*(1+x^2))
coeffs2 = (1/A("1-t*(1+x^2)").O(10)).coefficients()
[coeffs2[k][k] for k in range(10)]
[1, 0, 2, 0, 6, 0, 20, 0, 70, 0]
# The counting sequence for walks ending at positive x-value is given by the diagonal of 1/(1-x)/(1-t*(1+x^2))
# Note: this rational function has a bivariate power series expansion, but does not have an expansion in QQ[x,1/x][[t]]
B = QQ[['x']][['t']]
coeffs3 = (1/B("(1-x)*(1-t*(1+x^2))").O(10)).coefficients()
print([coeffs3[k][k] for k in range(10)])
print([add([coeffs[k][j] for j in range(k+1)]) for k in range(10)])
[1, 1, 3, 4, 11, 16, 42, 64, 163, 256] [1, 1, 3, 4, 11, 16, 42, 64, 163, 256]