Massimo Nocentini

November 9, 2016: script creation, splitting
November 7, 2016: check $\Phi_{ij}$ polys, `power` closed form
November {4,5,6}, 2016: `inverse`, `power`, `sqrt` $g$ polys
November 3, 2016: Fibonacci, $\mathcal{C}$, $d_{n,k}$ matrix for $\Phi$ polys
November 2, 2016: fix $\Phi$ polynomials def, $\mathcal{P}$



Abstract
Theory of matrix functions, with tutorial functions on simple matrix.

In [1]:
from sympy import *
from sympy.abc import n, i, N, x, lamda, phi, z, j, r, k, a

from commons import *
from matrix_functions import *
from sequences import *
import functions_catalog

init_printing()

In [3]:
M = define(Symbol(r'\mathcal{R}'), Matrix([[3, 1], [1, 3]]))
M


Out[3]:
$$\mathcal{R} = \left[\begin{matrix}3 & 1\\1 & 3\end{matrix}\right]$$

In [4]:
m = M.rhs.rows # degree of \Xi minimal polynomial

In [5]:
Phi_poly = Phi_poly_ctor(deg=m-1)
Phi_poly


Out[5]:
$$\Phi{\left (z,i,j \right )} = z \phi_{i,j,0} + \phi_{i,j,1}$$

In [6]:
with lift_to_Lambda(Phi_poly) as Phi_fn:
    pass

Phi_fn, Phi_fn(z, 3, j)


Out[6]:
$$\left ( \left( \left ( z, \quad i, \quad j\right ) \mapsto z \phi_{i,j,0} + \phi_{i,j,1} \right), \quad z \phi_{3,j,0} + \phi_{3,j,1}\right )$$

In [7]:
eigendata = spectrum(M)
eigendata


Out[7]:
$$\sigma{\left (\mathcal{R} \right )} = \left ( \left \{ 1 : \left ( \lambda_{1}, \quad m_{1}\right ), \quad 2 : \left ( \lambda_{2}, \quad m_{2}\right )\right \}, \quad \left \{ \lambda_{1} : 4, \quad \lambda_{2} : 2\right \}, \quad \left \{ m_{1} : 1, \quad m_{2} : 1\right \}\right )$$

In [9]:
data, eigenvals, multiplicities = eigendata.rhs

In [12]:
Phi_polynomials = component_polynomials(eigendata)
Phi_polynomials


Out[12]:
$$\left \{ \left ( 1, \quad 1\right ) : \Phi_{ 1, 1 }{\left (z \right )} = \frac{z}{\lambda_{1} - \lambda_{2}} - \frac{\lambda_{2}}{\lambda_{1} - \lambda_{2}}, \quad \left ( 2, \quad 1\right ) : \Phi_{ 2, 1 }{\left (z \right )} = - \frac{z}{\lambda_{1} - \lambda_{2}} + \frac{\lambda_{1}}{\lambda_{1} - \lambda_{2}}\right \}$$

In [13]:
f, h = Function('f'), Function('h')
function_eq = define(let=f(z), be=h(z))
function_eq


Out[13]:
$$f{\left (z \right )} = h{\left (z \right )}$$

In [14]:
g = Hermite_interpolation_polynomial(function_eq, eigendata, Phi_polynomials)
g


Out[14]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{h{\left (\lambda_{1} \right )}}{\lambda_{1} - \lambda_{2}} - \frac{h{\left (\lambda_{2} \right )}}{\lambda_{1} - \lambda_{2}}\right) - \frac{h{\left (\lambda_{1} \right )} \lambda_{2}}{\lambda_{1} - \lambda_{2}} + \frac{h{\left (\lambda_{2} \right )} \lambda_{1}}{\lambda_{1} - \lambda_{2}}$$

In [15]:
g.subs({h: Lambda(z, sqrt(z))})


Out[15]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{\sqrt{\lambda_{1}}}{\lambda_{1} - \lambda_{2}} - \frac{\sqrt{\lambda_{2}}}{\lambda_{1} - \lambda_{2}}\right) - \frac{\sqrt{\lambda_{1}} \lambda_{2}}{\lambda_{1} - \lambda_{2}} + \frac{\lambda_{1} \sqrt{\lambda_{2}}}{\lambda_{1} - \lambda_{2}}$$

In [16]:
g.subs({h: Lambda(z, 1/z)})


Out[16]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(- \frac{1}{\left(\lambda_{1} - \lambda_{2}\right) \lambda_{2}} + \frac{1}{\left(\lambda_{1} - \lambda_{2}\right) \lambda_{1}}\right) + \frac{\lambda_{1}}{\left(\lambda_{1} - \lambda_{2}\right) \lambda_{2}} - \frac{\lambda_{2}}{\left(\lambda_{1} - \lambda_{2}\right) \lambda_{1}}$$

In [18]:
t = g.subs(eigenvals)
t


Out[18]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(- \frac{1}{2} h{\left (2 \right )} + \frac{1}{2} h{\left (4 \right )}\right) + 2 h{\left (2 \right )} - h{\left (4 \right )}$$

In [19]:
with lift_to_matrix_function(g) as g:
    res = g(M)

res


Out[19]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{1}{\lambda_{1} - \lambda_{2}} \left(- h{\left (\lambda_{1} \right )} \lambda_{2} + 3 h{\left (\lambda_{1} \right )} + h{\left (\lambda_{2} \right )} \lambda_{1} - 3 h{\left (\lambda_{2} \right )}\right) & \frac{1}{\lambda_{1} - \lambda_{2}} \left(h{\left (\lambda_{1} \right )} - h{\left (\lambda_{2} \right )}\right)\\\frac{1}{\lambda_{1} - \lambda_{2}} \left(h{\left (\lambda_{1} \right )} - h{\left (\lambda_{2} \right )}\right) & \frac{1}{\lambda_{1} - \lambda_{2}} \left(- h{\left (\lambda_{1} \right )} \lambda_{2} + 3 h{\left (\lambda_{1} \right )} + h{\left (\lambda_{2} \right )} \lambda_{1} - 3 h{\left (\lambda_{2} \right )}\right)\end{matrix}\right]$$

In [22]:
define(res.lhs, res.rhs.subs(eigenvals))


Out[22]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{1}{2} h{\left (2 \right )} + \frac{1}{2} h{\left (4 \right )} & - \frac{1}{2} h{\left (2 \right )} + \frac{1}{2} h{\left (4 \right )}\\- \frac{1}{2} h{\left (2 \right )} + \frac{1}{2} h{\left (4 \right )} & \frac{1}{2} h{\left (2 \right )} + \frac{1}{2} h{\left (4 \right )}\end{matrix}\right]$$

In [23]:
cmatrices = component_matrices(M, Phi_polynomials)
cmatrices


Out[23]:
$$\left \{ \left ( 1, \quad 1\right ) : Z^{\left[ \mathcal{R} \right]}_{1,1} = \left[\begin{matrix}- \frac{\lambda_{2} - 3}{\lambda_{1} - \lambda_{2}} & \frac{1}{\lambda_{1} - \lambda_{2}}\\\frac{1}{\lambda_{1} - \lambda_{2}} & - \frac{\lambda_{2} - 3}{\lambda_{1} - \lambda_{2}}\end{matrix}\right], \quad \left ( 2, \quad 1\right ) : Z^{\left[ \mathcal{R} \right]}_{2,1} = \left[\begin{matrix}\frac{\lambda_{1} - 3}{\lambda_{1} - \lambda_{2}} & - \frac{1}{\lambda_{1} - \lambda_{2}}\\- \frac{1}{\lambda_{1} - \lambda_{2}} & \frac{\lambda_{1} - 3}{\lambda_{1} - \lambda_{2}}\end{matrix}\right]\right \}$$

In [25]:
Zi1 = list(cm.rhs.as_immutable() for (i, j), cm in cmatrices.items() if j == 1)
Zi1


Out[25]:
$$\left [ \left[\begin{matrix}- \frac{\lambda_{2} - 3}{\lambda_{1} - \lambda_{2}} & \frac{1}{\lambda_{1} - \lambda_{2}}\\\frac{1}{\lambda_{1} - \lambda_{2}} & - \frac{\lambda_{2} - 3}{\lambda_{1} - \lambda_{2}}\end{matrix}\right], \quad \left[\begin{matrix}\frac{\lambda_{1} - 3}{\lambda_{1} - \lambda_{2}} & - \frac{1}{\lambda_{1} - \lambda_{2}}\\- \frac{1}{\lambda_{1} - \lambda_{2}} & \frac{\lambda_{1} - 3}{\lambda_{1} - \lambda_{2}}\end{matrix}\right]\right ]$$

In [29]:
s = zeros(m)
for Z in Zi1:
    s += Z
s, s.subs(eigenvals)


Out[29]:
$$\left ( \left[\begin{matrix}\frac{\lambda_{1} - 3}{\lambda_{1} - \lambda_{2}} - \frac{\lambda_{2} - 3}{\lambda_{1} - \lambda_{2}} & 0\\0 & \frac{\lambda_{1} - 3}{\lambda_{1} - \lambda_{2}} - \frac{\lambda_{2} - 3}{\lambda_{1} - \lambda_{2}}\end{matrix}\right], \quad \left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right]\right )$$

const


In [30]:
f_const = define(let=f(z), be=r)
f_const


Out[30]:
$$f{\left (z \right )} = r$$

In [37]:
g_const = Hermite_interpolation_polynomial(f_const, eigendata, Phi_polynomials)
g_const


Out[37]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = \frac{r \lambda_{1}}{\lambda_{1} - \lambda_{2}} - \frac{r \lambda_{2}}{\lambda_{1} - \lambda_{2}}$$

In [38]:
g_const = g_const.subs(eigenvals)
g_const


Out[38]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = r$$

In [39]:
with lift_to_matrix_function(g_const) as G_const:
    m_const = G_const(M)
m_const


Out[39]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}r & 0\\0 & r\end{matrix}\right]$$

identity


In [40]:
f_identity = Eq(f(z), z)
f_identity


Out[40]:
$$f{\left (z \right )} = z$$

In [41]:
g_identity = Hermite_interpolation_polynomial(f_identity, eigendata, Phi_polynomials, matrix_form=True)
g_identity


Out[41]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = Z_{1,1} \lambda_{1} + Z_{2,1} \lambda_{2}$$

In [42]:
g_identity = Hermite_interpolation_polynomial(f_identity, eigendata, Phi_polynomials, matrix_form=False)
g_identity


Out[42]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{\lambda_{1}}{\lambda_{1} - \lambda_{2}} - \frac{\lambda_{2}}{\lambda_{1} - \lambda_{2}}\right)$$

In [43]:
with lift_to_matrix_function(g_identity) as G_identity:
    m_identity = G_identity(M)
m_identity


Out[43]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}3 & 1\\1 & 3\end{matrix}\right]$$

sqrt


In [44]:
f_sqrt = define(let=f(z), be=sqrt(z))
f_sqrt


Out[44]:
$$f{\left (z \right )} = \sqrt{z}$$

In [45]:
g_sqrt = Hermite_interpolation_polynomial(f_sqrt, eigendata, Phi_polynomials)
g_sqrt


Out[45]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{\sqrt{\lambda_{1}}}{\lambda_{1} - \lambda_{2}} - \frac{\sqrt{\lambda_{2}}}{\lambda_{1} - \lambda_{2}}\right) - \frac{\sqrt{\lambda_{1}} \lambda_{2}}{\lambda_{1} - \lambda_{2}} + \frac{\lambda_{1} \sqrt{\lambda_{2}}}{\lambda_{1} - \lambda_{2}}$$

In [46]:
g_sqrt = g_sqrt.subs(eigenvals)
g_sqrt


Out[46]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(- \frac{\sqrt{2}}{2} + 1\right) - 2 + 2 \sqrt{2}$$

In [47]:
with lift_to_matrix_function(g_sqrt) as G_sqrt:
    m_sqrt = G_sqrt(M)
m_sqrt


Out[47]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{\sqrt{2}}{2} + 1 & - \frac{\sqrt{2}}{2} + 1\\- \frac{\sqrt{2}}{2} + 1 & \frac{\sqrt{2}}{2} + 1\end{matrix}\right]$$

In [50]:
assert (m_sqrt.rhs**2).applyfunc(simplify) == M.rhs

power


In [51]:
f_power = define(let=f(z), be=z**r)
f_power


Out[51]:
$$f{\left (z \right )} = z^{r}$$

In [52]:
g_power = Hermite_interpolation_polynomial(f_power, eigendata, Phi_polynomials, matrix_form=True)
g_power


Out[52]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = Z_{1,1} \lambda_{1}^{r} + Z_{2,1} \lambda_{2}^{r}$$

In [54]:
g_power = Hermite_interpolation_polynomial(f_power, eigendata, Phi_polynomials)
g_power


Out[54]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{\lambda_{1}^{r}}{\lambda_{1} - \lambda_{2}} - \frac{\lambda_{2}^{r}}{\lambda_{1} - \lambda_{2}}\right) + \frac{\lambda_{1} \lambda_{2}^{r}}{\lambda_{1} - \lambda_{2}} - \frac{\lambda_{1}^{r} \lambda_{2}}{\lambda_{1} - \lambda_{2}}$$

In [55]:
g_power = g_power.subs(eigenvals)
g_power.simplify()


Out[55]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = 2^{r + 1} - 4^{r} - \frac{z}{2} \left(2^{r} - 4^{r}\right)$$

In [56]:
with lift_to_matrix_function(g_power) as G_power:
    m_power = G_power(M)
m_power


Out[56]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{2^{r}}{2} + \frac{4^{r}}{2} & - \frac{2^{r}}{2} + \frac{4^{r}}{2}\\- \frac{2^{r}}{2} + \frac{4^{r}}{2} & \frac{2^{r}}{2} + \frac{4^{r}}{2}\end{matrix}\right]$$

In [59]:
assert (M.rhs**r).applyfunc(simplify) == m_power.rhs

log


In [61]:
f_log = define(let=f(z), be=log(z, 2))
f_log


Out[61]:
$$f{\left (z \right )} = \frac{\log{\left (z \right )}}{\log{\left (2 \right )}}$$

In [62]:
g_log = Hermite_interpolation_polynomial(f_log, eigendata, Phi_polynomials)
g_log


Out[62]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{\log{\left (\lambda_{1} \right )}}{\log{\left (2 \right )} \lambda_{1} - \log{\left (2 \right )} \lambda_{2}} - \frac{\log{\left (\lambda_{2} \right )}}{\log{\left (2 \right )} \lambda_{1} - \log{\left (2 \right )} \lambda_{2}}\right) - \frac{\log{\left (\lambda_{1} \right )} \lambda_{2}}{\log{\left (2 \right )} \lambda_{1} - \log{\left (2 \right )} \lambda_{2}} + \frac{\log{\left (\lambda_{2} \right )} \lambda_{1}}{\log{\left (2 \right )} \lambda_{1} - \log{\left (2 \right )} \lambda_{2}}$$

In [63]:
g_log = g_log.subs(eigenvals)
g_log


Out[63]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(- \frac{1}{2} + \frac{\log{\left (4 \right )}}{2 \log{\left (2 \right )}}\right) - \frac{\log{\left (4 \right )}}{\log{\left (2 \right )}} + 2$$

In [64]:
with lift_to_matrix_function(g_log) as G_log:
    M_log = G_log(M)
M_log


Out[64]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{3}{2} & \frac{1}{2}\\\frac{1}{2} & \frac{3}{2}\end{matrix}\right]$$

inverse


In [65]:
f_inverse = define(let=f(z), be=1/z)
f_inverse


Out[65]:
$$f{\left (z \right )} = \frac{1}{z}$$

In [66]:
g_inverse = Hermite_interpolation_polynomial(f_inverse, eigendata, Phi_polynomials)
g_inverse


Out[66]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{1}{\lambda_{1}^{2} - \lambda_{1} \lambda_{2}} - \frac{1}{\lambda_{1} \lambda_{2} - \lambda_{2}^{2}}\right) - \frac{\lambda_{2}}{\lambda_{1}^{2} - \lambda_{1} \lambda_{2}} + \frac{\lambda_{1}}{\lambda_{1} \lambda_{2} - \lambda_{2}^{2}}$$

In [67]:
with lift_to_matrix_function(g_inverse) as G_inverse:
    m_inverse = G_inverse(M)

m_inverse, m_inverse.rhs.subs(eigenvals)


Out[67]:
$$\left ( \operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{1}{\lambda_{1} \lambda_{2}} \left(\lambda_{1} + \lambda_{2} - 3\right) & - \frac{1}{\lambda_{1} \lambda_{2}}\\- \frac{1}{\lambda_{1} \lambda_{2}} & \frac{1}{\lambda_{1} \lambda_{2}} \left(\lambda_{1} + \lambda_{2} - 3\right)\end{matrix}\right], \quad \left[\begin{matrix}\frac{3}{8} & - \frac{1}{8}\\- \frac{1}{8} & \frac{3}{8}\end{matrix}\right]\right )$$

expt


In [68]:
f_expt = define(let=f(z), be=exp(z))
f_expt


Out[68]:
$$f{\left (z \right )} = e^{z}$$

In [69]:
g_expt = Hermite_interpolation_polynomial(f_expt, eigendata, Phi_polynomials)
g_expt


Out[69]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(\frac{e^{\lambda_{1}}}{\lambda_{1} - \lambda_{2}} - \frac{e^{\lambda_{2}}}{\lambda_{1} - \lambda_{2}}\right) - \frac{e^{\lambda_{1}} \lambda_{2}}{\lambda_{1} - \lambda_{2}} + \frac{e^{\lambda_{2}} \lambda_{1}}{\lambda_{1} - \lambda_{2}}$$

In [70]:
g_expt = g_expt.subs(eigenvals)
g_expt


Out[70]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(- \frac{e^{2}}{2} + \frac{e^{4}}{2}\right) - e^{4} + 2 e^{2}$$

In [71]:
with lift_to_matrix_function(g_expt) as G_expt:
    m_expt = G_expt(M)
m_expt


Out[71]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{e^{2}}{2} \left(1 + e^{2}\right) & \frac{e^{2}}{2} \left(-1 + e^{2}\right)\\\frac{e^{2}}{2} \left(-1 + e^{2}\right) & \frac{e^{2}}{2} \left(1 + e^{2}\right)\end{matrix}\right]$$

geometric


In [72]:
f_geo = define(let=f(z), be=(1-z**(r+1))/(1-z))
f_geo


Out[72]:
$$f{\left (z \right )} = \frac{- z^{r + 1} + 1}{- z + 1}$$

In [73]:
g_geo = Hermite_interpolation_polynomial(f_geo, eigendata, Phi_polynomials)
g_geo


Out[73]:
$$\operatorname{f_{ 2 }}{\left (z \right )} = z \left(- \frac{\lambda_{1} \lambda_{1}^{r}}{- \lambda_{1}^{2} + \lambda_{1} \lambda_{2} + \lambda_{1} - \lambda_{2}} + \frac{1}{- \lambda_{1}^{2} + \lambda_{1} \lambda_{2} + \lambda_{1} - \lambda_{2}} + \frac{\lambda_{2} \lambda_{2}^{r}}{- \lambda_{1} \lambda_{2} + \lambda_{1} + \lambda_{2}^{2} - \lambda_{2}} - \frac{1}{- \lambda_{1} \lambda_{2} + \lambda_{1} + \lambda_{2}^{2} - \lambda_{2}}\right) + \frac{\lambda_{1} \lambda_{1}^{r} \lambda_{2}}{- \lambda_{1}^{2} + \lambda_{1} \lambda_{2} + \lambda_{1} - \lambda_{2}} - \frac{\lambda_{2}}{- \lambda_{1}^{2} + \lambda_{1} \lambda_{2} + \lambda_{1} - \lambda_{2}} - \frac{\lambda_{1} \lambda_{2} \lambda_{2}^{r}}{- \lambda_{1} \lambda_{2} + \lambda_{1} + \lambda_{2}^{2} - \lambda_{2}} + \frac{\lambda_{1}}{- \lambda_{1} \lambda_{2} + \lambda_{1} + \lambda_{2}^{2} - \lambda_{2}}$$

In [79]:
with lift_to_matrix_function(g_geo) as G_geo:
    m_geo = G_geo(M)

m_geo


Out[79]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{1}{\left(\lambda_{1} \lambda_{2} - \lambda_{1} - \lambda_{2}^{2} + \lambda_{2}\right) \left(\lambda_{1}^{2} - \lambda_{1} \lambda_{2} - \lambda_{1} + \lambda_{2}\right)} \left(\left(\lambda_{1} \lambda_{2} - \lambda_{1} - \lambda_{2}^{2} + \lambda_{2}\right) \left(- \lambda_{1}^{r + 1} \lambda_{2} + 3 \lambda_{1}^{r + 1} + \lambda_{2} - 3\right) + \left(\lambda_{1} \lambda_{2}^{r + 1} - \lambda_{1} - 3 \lambda_{2}^{r + 1} + 3\right) \left(\lambda_{1}^{2} - \lambda_{1} \lambda_{2} - \lambda_{1} + \lambda_{2}\right)\right) & \frac{- \lambda_{1} \lambda_{2}^{r + 1} + \lambda_{1} + \lambda_{1}^{r + 1} \lambda_{2} - \lambda_{1}^{r + 1} - \lambda_{2} + \lambda_{2}^{r + 1}}{\lambda_{1}^{2} \lambda_{2} - \lambda_{1}^{2} - \lambda_{1} \lambda_{2}^{2} + \lambda_{1} + \lambda_{2}^{2} - \lambda_{2}}\\\frac{- \lambda_{1} \lambda_{2}^{r + 1} + \lambda_{1} + \lambda_{1}^{r + 1} \lambda_{2} - \lambda_{1}^{r + 1} - \lambda_{2} + \lambda_{2}^{r + 1}}{\lambda_{1}^{2} \lambda_{2} - \lambda_{1}^{2} - \lambda_{1} \lambda_{2}^{2} + \lambda_{1} + \lambda_{2}^{2} - \lambda_{2}} & \frac{1}{\left(\lambda_{1} \lambda_{2} - \lambda_{1} - \lambda_{2}^{2} + \lambda_{2}\right) \left(\lambda_{1}^{2} - \lambda_{1} \lambda_{2} - \lambda_{1} + \lambda_{2}\right)} \left(\left(\lambda_{1} \lambda_{2} - \lambda_{1} - \lambda_{2}^{2} + \lambda_{2}\right) \left(- \lambda_{1}^{r + 1} \lambda_{2} + 3 \lambda_{1}^{r + 1} + \lambda_{2} - 3\right) + \left(\lambda_{1} \lambda_{2}^{r + 1} - \lambda_{1} - 3 \lambda_{2}^{r + 1} + 3\right) \left(\lambda_{1}^{2} - \lambda_{1} \lambda_{2} - \lambda_{1} + \lambda_{2}\right)\right)\end{matrix}\right]$$

In [82]:
m_geo= define(m_geo.lhs, m_geo.rhs.subs(eigenvals))
m_geo


Out[82]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}\frac{1}{2} 2^{r + 1} + \frac{1}{6} 4^{r + 1} - \frac{2}{3} & - \frac{1}{2} 2^{r + 1} + \frac{1}{6} 4^{r + 1} + \frac{1}{3}\\- \frac{1}{2} 2^{r + 1} + \frac{1}{6} 4^{r + 1} + \frac{1}{3} & \frac{1}{2} 2^{r + 1} + \frac{1}{6} 4^{r + 1} - \frac{2}{3}\end{matrix}\right]$$

In [83]:
define(m_geo.lhs, m_geo.rhs.subs({r:4}))


Out[83]:
$$\operatorname{f_{ 2 }}{\left (\mathcal{R} \right )} = \left[\begin{matrix}186 & 155\\155 & 186\end{matrix}\right]$$

In [84]:
s = Function('s')
sum_def = define(let=s(z), be=Sum(z**i, (i, 0, 4)).doit())
sum_def


Out[84]:
$$s{\left (z \right )} = z^{4} + z^{3} + z^{2} + z + 1$$

In [86]:
with lift_to_matrix_function(sum_def) as sum_fn:
    R = sum_fn(M)
R


Out[86]:
$$s{\left (\mathcal{R} \right )} = \left[\begin{matrix}186 & 155\\155 & 186\end{matrix}\right]$$