In [1]:
from sympy import *
from sympy.abc import n, i, N, x, lamda, phi, z, j, r, k, a, alpha, beta
init_printing()
In [2]:
import functions_catalog
In [3]:
from matrix_functions import *
from commons import *
from sequences import *
In [30]:
%run ../../src/commons.py
%run ../../src/matrix_functions.py
%run ../../src/functions_catalog.py
In [4]:
m=8
In [5]:
P_ = Matrix(m,m, lambda n,k: binomial(n, k, evaluate=k >= n or not k))
P_ # not usable in the framework because not a definition in equality style
Out[5]:
In [6]:
P = define(Symbol(r'\mathcal{{P}}_{{ {} }}'.format(m)), Matrix(m,m,binomial))
P
Out[6]:
In [7]:
eigendata = spectrum(P)
eigendata
Out[7]:
In [8]:
data, eigenvals, multiplicities = eigendata.rhs # unpacking to use `eigenvals` in `subs`
In [9]:
Phi_polynomials = component_polynomials(eigendata, early_eigenvals_subs=True)
Phi_polynomials
Out[9]:
In [10]:
cmatrices = component_matrices(P, Phi_polynomials)
list(cmatrices.values())
Out[10]:
In [11]:
f_power, g_power = functions_catalog.power(eigendata, Phi_polynomials)
In [12]:
P_power = g_power(P)
P_power
Out[12]:
In [13]:
production_matrix(P_power.rhs)
Out[13]:
In [14]:
assert P_power.rhs == (P.rhs**r).applyfunc(simplify)
In [15]:
f_inverse, g_inverse = functions_catalog.inverse(eigendata, Phi_polynomials)
In [16]:
P_inverse = g_inverse(P)
P_inverse, g_inverse(P_inverse)
Out[16]:
In [17]:
production_matrix(P_inverse.rhs)
Out[17]:
In [18]:
assert (P_inverse.rhs * P.rhs) == Matrix(m, m, identity_matrix())
assert P_inverse.rhs == P.rhs**(-1)
assert P_inverse.rhs == P_power.rhs.subs({r:-1})
In [19]:
f_sqrt, g_sqrt = functions_catalog.square_root(eigendata, Phi_polynomials)
In [20]:
P_sqrt = g_sqrt(P)
P_sqrt
Out[20]:
In [21]:
production_matrix(P_sqrt.rhs)
Out[21]:
In [22]:
assert P_sqrt.rhs == P.rhs**(S(1)/2)
assert P_sqrt.rhs * P_sqrt.rhs == P.rhs
assert P_sqrt.rhs == P_power.rhs.subs({r:S(1)/2})
In [23]:
P_power.rhs.subs({r:S(1)/3})
Out[23]:
In [24]:
P_power.rhs.subs({r:2})
Out[24]:
In [25]:
inspect(_)
Out[25]:
In [26]:
f_exp, g_exp = functions_catalog.exp(eigendata, Phi_polynomials)
In [27]:
P_exp = g_exp(P)
P_exp
Out[27]:
In [28]:
inspect(P_exp.rhs)
Out[28]:
In [29]:
production_matrix(P_exp.rhs.subs({alpha:1})) # faster than `production_matrix(P_exp.rhs).subs({alpha:1})`
Out[29]:
In [30]:
simp_P_expt = define(P_exp.lhs,
Mul(exp(alpha), P_exp.rhs.applyfunc(lambda c: (c/exp(alpha)).expand()), evaluate=False))
simp_P_expt
Out[30]:
In [31]:
from sympy.functions.combinatorial.numbers import stirling
In [32]:
S = Matrix(m, m, lambda n,k: stirling(n,k, kind=2))
define(Symbol('S'), S), production_matrix(S), production_matrix(S, exp=True)
Out[32]:
In [33]:
S*P.rhs*S**(-1), S**(-1)*P.rhs*S
Out[33]:
In [34]:
f_log, g_log, = functions_catalog.log(eigendata, Phi_polynomials)
In [35]:
P_log = g_log(P)
P_log
Out[35]:
In [36]:
inspect(P_log.rhs[1:,:-1])
Out[36]:
In [37]:
production_matrix(P_log.rhs[1:,:-1])
Out[37]:
In [38]:
P_exp_dirty = define(P_exp.lhs, P_exp.rhs.subs({alpha:1}))
P_exp_dirty
Out[38]:
In [39]:
P_exp_eigendata = spectrum(P_exp_dirty)
P_exp_eigendata
Out[39]:
In [40]:
P_exp_Phi_polynomials = component_polynomials(P_exp_eigendata, early_eigenvals_subs=True)
P_exp_Phi_polynomials
Out[40]:
In [42]:
f_log_dirty, g_log_dirty, = functions_catalog.log(P_exp_eigendata, P_exp_Phi_polynomials)
In [50]:
g_log_dirty
Out[50]:
In [43]:
g_log_dirty(P_exp_dirty)
Out[43]:
In [44]:
P_log_eigendata = spectrum(P_log)
P_log_eigendata
Out[44]:
In [45]:
P_log_Phi_polynomials = component_polynomials(P_log_eigendata, early_eigenvals_subs=True)
P_log_Phi_polynomials
Out[45]:
In [46]:
f_exp_dirty, g_exp_dirty, = functions_catalog.exp(P_log_eigendata, P_log_Phi_polynomials)
In [47]:
g_exp_dirty
Out[47]:
In [48]:
g_exp_dirty(P_log)
Out[48]:
In [49]:
_.rhs.subs({alpha:1})
Out[49]:
In [9]:
f_sin, g_sin, G_sin = functions_catalog.sin(eigendata, Phi_polynomials)
In [10]:
P_sin = G_sin(P)
P_sin
Out[10]:
In [ ]:
production_matrix(P_sin.rhs).applyfunc(simplify) # takes long to evaluate
In [11]:
f_cos, g_cos, G_cos = functions_catalog.cos(eigendata, Phi_polynomials)
In [12]:
P_cos = G_cos(P)
P_cos
Out[12]:
In [ ]:
production_matrix(P_sin).applyfunc(simplify) # takes long to evaluate
In [52]:
assert (P_sin.rhs**2 + P_cos.rhs**2).applyfunc(trigsimp) == Matrix(m,m, identity_matrix()) # sin^2 + cos^2 = 1
In [29]:
P2 = define(Symbol(r'2\,\mathcal{P}'), 2*P.rhs)
P2
Out[29]:
In [30]:
eigendata_P2 = spectrum(P2)
Phi_polynomials_P2 = component_polynomials(eigendata_P2, early_eigenvals_subs=True)
In [36]:
f_sin2, g_sin2, G_sin2 = functions_catalog.sin(eigendata_P2, Phi_polynomials_P2)
In [37]:
g_sin2
Out[37]:
In [38]:
P_sin2 = G_sin2(P2)
P_sin2
Out[38]:
In [39]:
PP = P_sin2.rhs.applyfunc(lambda i: i.subs({alpha:1}))
assert PP == (2*P_sin.rhs*P_cos.rhs).applyfunc(trigsimp)
In [18]:
PP
Out[18]:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.