$T(z,t)$: Bivariate Generating function for combinatorial class of rooted plane trees ($z$ counts inner nodes, $t$ counts leaves)
def T(z, t):
return 1/2 * (1 - (z-t) - sqrt(1 - 2*(z+t) + (z-t)^2))
T(z,t).taylor((z,0), (t,0), 4)
def phi(f, z, t):
return lambda z, t: (1-t) * f(z/(1-t)^2, z*t/(1-t)^2)
phi(T, z, t)(z,t).simplify_rational()
phi(T, z, t)(z,t).taylor((z,0), (t,0), 4)
phi(phi(T, z, t), z, t)(z,t).taylor((z,0), (t,0), 4)
expr = 1/(1 + 1/x)
expr
expr.series(x==0, 10)
expr.simplify_rational()
expr.simplify_rational().series(x==0, 6)
factor()
is currently broken (and will be fixed soon, hopefully).
expr = (2*exp(x) + exp(-x))
expr
expr.factor()
expr = (x + sqrt(x))/(x - sqrt(x))
expr
expr.factor()
(x + sqrt(x)).factor()
SCR = SR.subring(no_variables=True) # complex numbers + symbolic constants
A = AsymptoticRing(growth_group='QQ^n * n^QQ * log(n)^QQ',
coefficient_ring=SCR,
default_prec=5)
n = A.gen()
A
Asymptotic Ring <QQ^n * n^QQ * log(n)^QQ> over Symbolic Constants Subring
A.an_element()
42 * (1/3)^n * n^(5/2) * log(n)^2 + O((1/3)^n * n * log(n)^(1/2))
log(n+1)
1/(1 + n)
(1 + 1/n)^n
try:
factorial(n)
except TypeError as e:
print(e)
no common canonical parent for objects with parents: 'Asymptotic Ring <QQ^n * n^QQ * log(n)^QQ> over Symbolic Constants Subring' and 'Asymptotic Ring <(e^(n*log(n)))^QQ * (e^n)^QQ * n^QQ * log(n)^QQ> over Symbolic Constants Subring'
binomial_2n_n = asymptotic_expansions.Binomial_kn_over_n(n, k=2, precision=5)
binomial_2n_n
catalan_asy = binomial_2n_n / (n+1)
catalan_asy
def catalan(z):
return (1 - sqrt(1 - 4*z)) / (2*z)
catalan_asy = A.coefficients_of_generating_function(catalan,
singularities=(1/4,),
precision=5)
catalan_asy
Preparation for later: There are $C_{n-1}$ plane trees with $n$ nodes.
shifted_catalan_asy = catalan_asy.subs(n=n-1)
shifted_catalan_asy
var('u v')
var('r', domain='integer')
assume(r > 0)
A.<n> = AsymptoticRing('QQ^n * n^QQ', SR)
A_func.<Z> = AsymptoticRing('Z^QQ * log(Z)^QQ', SR)
# solve z = u/(1+u)^2, get u as a function of z
def upsilon(z):
return (1 - sqrt(1 - 4*z))/(2*z) - 1
# results from Proposition
def G_r(u, v):
return (1-u^(r+2)) / ((1-u^(r+1)) * (1+u)) * \
T(u * (1-u^(r+1))^2 / (1-u^(r+2))^2 * v,
u^(r+1) * (1-u)^2 / (1-u^(r+2))^2 * v)
Sum of remaining tree sizes: $[z^n] \partial_v G_r(u,v)|_{v=1}$.
Expand $\partial_v G_r(u,v)|_{v=1}$ at $z = 1/4$ in terms of $Z$ where $Z = (1 - 4z)^{-1}$.
z_asy = 1/4 * (1-Z^(-1))
u_asy = upsilon(z_asy) + O(Z^(-3))
# Expansion of u(z) in terms of Z
u_asy
G_r(u, v).diff(v, 1)(v=1).simplify_rational()
%%time
sum_of_remaining_nodes_func = fast_callable(G_r(u, v).diff(v, 1)(v=1)\
.simplify_rational(),
vars=(u, r))(u_asy, r)
CPU times: user 8.2 s, sys: 151 ms, total: 8.35 s Wall time: 7.76 s
sum_of_remaining_nodes_func
sum_of_remaining_nodes_asy = sum_of_remaining_nodes_func\
._singularity_analysis_(n,
zeta=1/4,
precision=2)
sum_of_remaining_nodes_asy
expected_tree_size = sum_of_remaining_nodes_asy / shifted_catalan_asy
expected_tree_size
expected_tree_size.map_coefficients(lambda ex: ex.subs({1/sqrt(r^2 + 2*r + 1) : 1/(r+1)}).factor())