In [1]:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

import oeis

import sys
import sympy
import math
from sympy import *
from sympy.abc import x, n, z, t, k
from sympy.core.cache import *

init_printing() # for nice printing, a-la' TeX

sys.setrecursionlimit(100000)

plt.rcParams['figure.figsize'] = (10.0, 10.0)




In [7]:

with assuming(Q.positive(1-x)):
print(sqrt((1-x)**2))




sqrt((-x + 1)**2)




In [26]:

x = symbols('x', negative=True)
s = sqrt((1-x)**2)
s




Out[26]:

$$- x + 1$$





In [3]:

c = IndexedBase('c')




In [4]:

(1*c[n,4]).args




Out[4]:

$$\left ( c, \quad n, \quad 4\right )$$




In [1]:

eqs=Tuple(#Eq(c[0,0],c[3,3]),
Eq(c[1,0],c[0,0]),
Eq(c[1,1],c[0,0]),
Eq(c[2,0],c[1,0]+c[1,1]),
Eq(c[2,1],c[1,0]+c[1,1]),
Eq(c[2,2],c[1,1]),
Eq(c[3,0],c[2,0]+c[2,1]+c[2,2]),
Eq(c[3,1],c[2,0]+c[2,1]+c[2,2]),
Eq(c[3,2],c[2,1]+c[2,2]),
Eq(c[3,3],c[0,0]),
)
solve(eqs, eqs.atoms(Indexed) )
#[c[0,0],c[1,0],c[2,0],c[3,0]], check=True)
#exclude=[c[3,0],c[3,1],c[3,2],c[3,3]], check=True)
#[c[0,0],c[1,1],c[2,2],c[3,3]])
#check=True, implicit=True)




---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-eb91dc46e296> in <module>()
----> 1 eqs=Tuple(#Eq(c[0,0],c[3,3]),
2       Eq(c[1,0],c[0,0]),
3       Eq(c[1,1],c[0,0]),
4       Eq(c[2,0],c[1,0]+c[1,1]),
5       Eq(c[2,1],c[1,0]+c[1,1]),

NameError: name 'Tuple' is not defined




In [4]:

solve([#Eq(c[0,0],c[3,3]),
Eq(c[4,0],c[3,0]+c[3,1]+c[3,2]+c[3,3]),
Eq(c[4,1],c[3,0]+c[3,1]+c[3,2]+c[3,3]),
Eq(c[4,2],c[3,1]+c[3,2]+c[3,3]),
Eq(c[4,3],c[3,2]+c[3,3]),
Eq(c[4,4],c[3,3]),
], check=True
#[c[0,0],c[1,0],c[2,0],c[3,0]], check=True)
#exclude=[c[3,0],c[3,1],c[3,2],c[3,3]], check=True)
#[c[3,0],c[3,1],c[3,2],c[3,3]])
#check=True, implicit=True)
)




Out[4]:

$$\left [ \left \{ c_{3,0} : c_{4,1} - c_{4,2}, \quad c_{3,1} : c_{4,2} - c_{4,3}, \quad c_{3,2} : c_{4,3} - c_{4,4}, \quad c_{3,3} : c_{4,4}, \quad c_{4,0} : c_{4,1}\right \}\right ]$$




In [7]:

solve([#Eq(c[0,0],c[3,3]),
Eq(c[1,0],c[0,0]),
Eq(c[1,1],c[0,0])
], check=True
#[c[0,0],c[1,0],c[2,0],c[3,0]], check=True)
#exclude=[c[3,0],c[3,1],c[3,2],c[3,3]], check=True)
#[c[3,0],c[3,1],c[3,2],c[3,3]])
#check=True, implicit=True)
)




Out[7]:

$$\left [ \left \{ c_{0,0} : c_{1,1}, \quad c_{1,0} : c_{1,1}\right \}\right ]$$




In [3]:

def catalan_gf(t): return (1-sqrt(1-4*t))/(2*t)




In [5]:

catalan_series = catalan_gf(t).series(t, n=15)




In [6]:

[catalan_series.coeff(t,n=i) for i in range(15)]




Out[6]:

$$\left [ 1, \quad 1, \quad 2, \quad 5, \quad 14, \quad 42, \quad 132, \quad 429, \quad 1430, \quad 4862, \quad 16796, \quad 58786, \quad 208012, \quad 742900, \quad 2674440\right ]$$





In [3]:

from contextlib import contextmanager

@contextmanager
def bind_Mul_indexed(term, indexed, forbidden_terms=[]):
coeff_w, ind_w = Wild('coeff', exclude=[indexed] + forbidden_terms), Wild('ind')
matched = term.match(coeff_w * ind_w)
# if no indexing happen then isinstance(matched[ind_w], IndexedBase) holds
if matched and ind_w in matched and isinstance(matched[ind_w], Indexed):
_, *subscripts = matched[ind_w].args
yield matched[coeff_w], subscripts
else:
raise Exception()

f, n, k = IndexedBase('f'), *symbols('n k')
term = 3 * f[n,k]
try:
with bind_Mul_indexed(term, f) as (coeff, subscripts):
print('{} * {}'.format(coeff, subscripts))
except Exception:
print('something else')

term




3 * [n, k]

Out[3]:

$$3 f_{n,k}$$




In [ ]:




In [8]:

n, k = symbols('n k')
i = IndexedBase('i')




In [40]:

a, *b = i.args
a,b




Out[40]:

$$\left ( i, \quad \left [ \right ]\right )$$




In [16]:

i_w = Wild('i_w')
m = i[n,k].match(i_w)
m[i_w]




Out[16]:

$$i_{n,k}$$




In [27]:

type(i)




Out[27]:

sympy.tensor.indexed.IndexedBase




In [22]:

eq = Eq(2**n * k, 111)
k_constraint = Eq(k, 2*t+1)
eq, k_constraint




Out[22]:

$$\left ( 2^{n} k = 111, \quad k = 2 t + 1\right )$$




In [23]:

solve([eq, k_constraint], [n,k,t])




Out[23]:

$$\left [ \left \{ k : 2 t + 1, \quad n : \frac{\log{\left (\frac{111}{2 t + 1} \right )}}{\log{\left (2 \right )}}\right \}\right ]$$




In [24]:

n_sol = floor(solve(Eq(2**n, 111), n).pop())
n_sol




Out[24]:

$$6$$




In [29]:

m=(solve(eq.subs(n, n_sol), k).pop())
m




Out[29]:

$$\frac{111}{64}$$




In [30]:

m.p, m.q




Out[30]:

$$\left ( 111, \quad 64\right )$$




In [31]:

Mul(2**n_sol*m.q, m.p, evaluate=False)




Out[31]:

$$111 \cdot 4096$$




In [3]:

divmod(Integer(83726), 8475)




Out[3]:

$$\left ( 9, \quad 7451\right )$$




In [41]:

def list_to_frac(count, symbol_chr='a', start_index=0):
l = symbols('{}{}:{}'.format(symbol_chr, start_index, count+start_index))
expr = Integer(0)
for i in reversed(l[1:]):
expr += i
expr = 1/expr
return l[0] + expr, l
four_convergent, syms = list_to_frac(4)
four_convergent




Out[41]:

$$a_{0} + \frac{1}{a_{1} + \frac{1}{a_{2} + \frac{1}{a_{3}}}}$$




In [45]:

four_convergent.subs(syms[1], 1)




Out[45]:

$$a_{0} + \frac{1}{1 + \frac{1}{a_{2} + \frac{1}{a_{3}}}}$$




In [43]:

symbols('a_{n+1}')




Out[43]:

$$a_{n+1}$$




In [33]:

@contextmanager
def split_dict(symbols, subs):
yield (subs[s] for s in symbols)




In [34]:

with split_dict([n,k,t], {k:40, n:39, t:3}) as (x_v, *rest): pass
x_v




Out[34]:

$$39$$




In [9]:

T = IndexedBase('T')
hanoi_eq = Eq(T[n]+1, 2*T[n-1]+2).factor()
hanoi_eq




Out[9]:

$$T_{n} + 1 = 2 \left(T_{n - 1} + 1\right)$$



the following shows that subs doesn't perform true pattern matching; on the other hand, replace does it since it is possible to use Wild object too.



In [10]:

U = IndexedBase('U')
a = Wild('a')
hanoi_eq.subs(T[n]+1, U[n]), hanoi_eq.replace(T[a]+1, U[a])




Out[10]:

$$\left ( U_{n} = 2 \left(T_{n - 1} + 1\right), \quad U_{n} = 2 U_{n - 1}\right )$$




In [14]:

n, m = Wild('n'), Wild('m')
r = Integer(8475).replace(2**n * m, m, map=True)
r




Out[14]:

$$\left ( 8475, \quad \left \{ \right \}\right )$$




In [13]:

solve(Equality(k**2, 2*n*log(1/(1-t))), k)[1].simplify()




Out[13]:

$$\sqrt{2} \sqrt{n \log{\left (- \frac{1}{t - 1} \right )}}$$




In [9]:

solve?




In [14]:

17 >> 1




Out[14]:

$$8$$




In [3]:

a = IndexedBase('a')
eq1 = Eq(a[2], 2+a[1])
eq2 = Eq(a[3], 1+a[1])
eq3 = Eq(3, a[2]+a[3])
solve([eq1, eq2, eq3], [a[1],a[2],a[3],])




Out[3]:

$$\left \{ a_{1} : 0, \quad a_{2} : 2, \quad a_{3} : 1\right \}$$





In [20]:

x, y = IndexedBase('x'), IndexedBase('y')

eq_4 = Eq(1, x[2]*y[2])
eq_3 = Eq(0, x[2]*y[1] + x[1]*y[2])
eq_2 = Eq(0, x[2] + x[1]*y[1] + y[2])
eq_1 = Eq(0, x[1] + y[1])
eq_0 = Eq(0, x[0] * y[0])

eqs = [eq_4, eq_3, eq_2, eq_1, eq_0]
eqs




Out[20]:

$$\left [ 1 = x_{2} y_{2}, \quad 0 = x_{1} y_{2} + x_{2} y_{1}, \quad 0 = x_{1} y_{1} + x_{2} + y_{2}, \quad 0 = x_{1} + y_{1}, \quad 0 = x_{0} y_{0}\right ]$$




In [21]:

solve(eqs, [x[2],x[1], x[0],y[2],y[1],y[0]], )




Out[21]:

$$\left [ \left \{ x_{0} : 0, \quad x_{1} : 0, \quad x_{2} : - i, \quad y_{1} : 0, \quad y_{2} : i\right \}, \quad \left \{ x_{0} : 0, \quad x_{1} : 0, \quad x_{2} : i, \quad y_{1} : 0, \quad y_{2} : - i\right \}, \quad \left \{ x_{0} : 0, \quad x_{1} : - \sqrt{2}, \quad x_{2} : 1, \quad y_{1} : \sqrt{2}, \quad y_{2} : 1\right \}, \quad \left \{ x_{0} : 0, \quad x_{1} : \sqrt{2}, \quad x_{2} : 1, \quad y_{1} : - \sqrt{2}, \quad y_{2} : 1\right \}, \quad \left \{ x_{0} : 0, \quad x_{1} : - \sqrt{2} i, \quad x_{2} : -1, \quad y_{1} : \sqrt{2} i, \quad y_{2} : -1\right \}, \quad \left \{ x_{0} : 0, \quad x_{1} : \sqrt{2} i, \quad x_{2} : -1, \quad y_{1} : - \sqrt{2} i, \quad y_{2} : -1\right \}, \quad \left \{ x_{1} : 0, \quad x_{2} : - i, \quad y_{0} : 0, \quad y_{1} : 0, \quad y_{2} : i\right \}, \quad \left \{ x_{1} : 0, \quad x_{2} : i, \quad y_{0} : 0, \quad y_{1} : 0, \quad y_{2} : - i\right \}, \quad \left \{ x_{1} : - \sqrt{2}, \quad x_{2} : 1, \quad y_{0} : 0, \quad y_{1} : \sqrt{2}, \quad y_{2} : 1\right \}, \quad \left \{ x_{1} : \sqrt{2}, \quad x_{2} : 1, \quad y_{0} : 0, \quad y_{1} : - \sqrt{2}, \quad y_{2} : 1\right \}, \quad \left \{ x_{1} : - \sqrt{2} i, \quad x_{2} : -1, \quad y_{0} : 0, \quad y_{1} : \sqrt{2} i, \quad y_{2} : -1\right \}, \quad \left \{ x_{1} : \sqrt{2} i, \quad x_{2} : -1, \quad y_{0} : 0, \quad y_{1} : - \sqrt{2} i, \quad y_{2} : -1\right \}\right ]$$




In [ ]: