In [1]:
from sympy import *
from IPython.display import Markdown, Latex
from oeis import oeis_search
init_printing()
In [71]:
%run ~/Developer/working-copies/programming-contests/competitive-programming/python-libs/oeis.py
In [39]:
j = symbols('j', positive=True)
t = symbols('t')
j_range = range(1, 10)
In [4]:
def make_expander(gf, t, terms_in_expansion = 15):
def worker(j_index):
term = Subs(gf, j, j_index)
return Eq(j, j_index), Eq(term, term.doit().series(t, n=terms_in_expansion))
return worker
def coeffs(res, t, limit):
return [res.rhs.coeff(t, n) for n in range(limit)]
def match_table(results, gf_col_width="12cm"):
rows = []
for j_eq, res in results:
j, j_index = j_eq.lhs, j_eq.rhs
terms_in_expansion = res.rhs.getn()
searchable = oeis_search(seq=coeffs(res, t, limit=terms_in_expansion),
only_possible_matchings=True, progress_indicator=None)
row_src = searchable(term_src=latex(res.lhs))
rows.append(row_src)
header_row = r'<tr><th style="width:{width};" >gf</th><th>matches</th></tr>'.format(width=gf_col_width)
return Markdown('<table style="width:100%">\n {header}\n {rows}\n </table>'.format(
header=header_row, rows='\n'.join(rows)))
def coeffs_table(results):
def coeffs_table_rows():
rows = []
for j_eq, res in results:
j, j_index = j_eq.lhs, j_eq.rhs
terms_in_expansion = res.rhs.getn()
coefficients = coeffs(res, t, limit=terms_in_expansion)
rows.append(latex(j_eq) + ' & ' + latex(coefficients))
return rows
return Latex(r'\begin{{array}}{{r|l}} {rows} \end{{array}}'.format(rows=r'\\'.join(coeffs_table_rows())))
In [5]:
def fib(t):
return t/(1-t-t**2)
In [6]:
f = fib(t)
f
Out[6]:
In [7]:
s = f.series(t, n=10)
s
Out[7]:
In [8]:
s.getn()
Out[8]:
In [9]:
results = list(map(make_expander(fib(t), t), [1]))
In [10]:
results
Out[10]:
In [11]:
coeffs_table(results)
Out[11]:
In [12]:
match_table(results)
Out[12]:
In [14]:
def S(t):
radix_term = sqrt(1-4*t+4*t**(j+1))
return 2/(radix_term * (1 + radix_term))
In [15]:
S(t)
Out[15]:
In [16]:
results = list(map(make_expander(S(t), t), j_range))
# results
In [17]:
coeffs_table(results)
Out[17]:
In [18]:
match_table(results)
Out[18]:
In [19]:
def S(t):
radix_term = sqrt(1-4*t+4*t**(j+1))
return 2*(1-t**j)/(radix_term * (1 + radix_term))
In [20]:
S(t)
Out[20]:
In [21]:
results = list(map(make_expander(S(t), t), j_range))
In [22]:
coeffs_table(results)
Out[22]:
In [23]:
match_table(results)
Out[23]:
In [24]:
def S(t):
radix_term = sqrt(1-4*t+2*t**j+t**(2*j))
return 2/(radix_term * (1 -t**j + radix_term))
In [25]:
S(t)
Out[25]:
In [26]:
results = list(map(make_expander(S(t), t), j_range))
In [27]:
coeffs_table(results)
Out[27]:
In [28]:
match_table(results)
Out[28]:
In [29]:
def S(t):
radix_term = sqrt(1-4*t+2*t**(j+1)+4*t**(j+2)-3*t**(2*j+2))
return 2*(1-t**j)/(1-4*t**j+3*t**(j+1)+radix_term)
In [30]:
S(t)
Out[30]:
In [31]:
results = list(map(make_expander(S(t), t), j_range))
In [32]:
coeffs_table(results)
Out[32]:
In [33]:
match_table(results)
Out[33]:
In [34]:
def S(t):
radix_term = sqrt(1-4*t+2*t**(j+1)+4*t**(j+2)-3*t**(2*j+2))
return 2*(1-t**j-t**(j+1)+t**(2*j+1))/(radix_term * (1 -2*t**j +t**(j+1) + radix_term))
In [35]:
S(t)
Out[35]:
In [36]:
results = list(map(make_expander(S(t), t), j_range))
In [37]:
coeffs_table(results)
Out[37]:
In [38]:
match_table(results, gf_col_width="18cm")
Out[38]:
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.