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))
In [26]:
x = symbols('x', negative=True)
s = sqrt((1-x)**2)
s
Out[26]:
In [3]:
c = IndexedBase('c')
In [4]:
(1*c[n,4]).args
Out[4]:
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)
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]:
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]:
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]:
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
Out[3]:
In [ ]:
In [8]:
n, k = symbols('n k')
i = IndexedBase('i')
In [40]:
a, *b = i.args
a,b
Out[40]:
In [16]:
i_w = Wild('i_w')
m = i[n,k].match(i_w)
m[i_w]
Out[16]:
In [27]:
type(i)
Out[27]:
In [22]:
eq = Eq(2**n * k, 111)
k_constraint = Eq(k, 2*t+1)
eq, k_constraint
Out[22]:
In [23]:
solve([eq, k_constraint], [n,k,t])
Out[23]:
In [24]:
n_sol = floor(solve(Eq(2**n, 111), n).pop())
n_sol
Out[24]:
In [29]:
m=(solve(eq.subs(n, n_sol), k).pop())
m
Out[29]:
In [30]:
m.p, m.q
Out[30]:
In [31]:
Mul(2**n_sol*m.q, m.p, evaluate=False)
Out[31]:
In [3]:
divmod(Integer(83726), 8475)
Out[3]:
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]:
In [45]:
four_convergent.subs(syms[1], 1)
Out[45]:
In [43]:
symbols('a_{n+1}')
Out[43]:
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]:
In [9]:
T = IndexedBase('T')
hanoi_eq = Eq(T[n]+1, 2*T[n-1]+2).factor()
hanoi_eq
Out[9]:
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]:
In [14]:
n, m = Wild('n'), Wild('m')
r = Integer(8475).replace(2**n * m, m, map=True)
r
Out[14]:
In [13]:
solve(Equality(k**2, 2*n*log(1/(1-t))), k)[1].simplify()
Out[13]:
In [9]:
solve?
In [14]:
17 >> 1
Out[14]:
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]:
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]:
In [21]:
solve(eqs, [x[2],x[1], x[0],y[2],y[1],y[0]], )
Out[21]:
In [ ]: