An example of a formal power series in $\mathbb{N}[[z]]$ that is not $\mathbb{N}$-rational.
Requirements: None
# Define the rational function
var('z')
F = 2*(1+3*z)^2/(1-9*z)/(1+14*z+81*z^2)
F
-2*(3*z + 1)^2/((81*z^2 + 14*z + 1)*(9*z - 1))
# Find a recurrence for the sequence with generating function F
C.<z> = CFiniteSequences(QQ)
f = C(F)
f.recurrence_repr()
'homogeneous linear recurrence with constant coefficients of degree 3: a(n+3) = -5*a(n+2) + 45*a(n+1) + 729*a(n), starting a(0...) = [2, 2, 98]'
# Initial terms of the sequence
[f[k] for k in range(10)]
[2, 2, 98, 1058, 578, 116162, 216482, 4566242, 71592578, 5333378]
# Closed form for coefficients
f.closed_form()
9^n + 1/2*(4*I*sqrt(2) - 7)^n + 1/2*(-4*I*sqrt(2) - 7)^n
# Write partial fraction decomposition of generating function
theta = arccos(1/3)
e2theta = cos(2*theta) + I*sin(2*theta) # Help Sage simplify exp(2*I*theta)
F2 = 1/(1-9*z*e2theta)/2 + 1/(1-9*z/e2theta)/2 + 1/(1-9*z)
print(F2.simplify_full())
print((F-F2).simplify_full())
-2*(9*z^2 + 6*z + 1)/(729*z^3 + 45*z^2 - 5*z - 1) 0
# Sequence expressed in polar form
[9^n*(1+cos(2*n*theta)).simplify_trig() for n in range(10)]
[2, 2, 98, 1058, 578, 116162, 216482, 4566242, 71592578, 5333378]