In [1]:
import numpy as np
from numpy.linalg import *
rg = matrix_rank
from IPython.display import display, Math, Latex, Markdown

from sympy import *
from sympy import Matrix, solve_linear_system

In [8]:
pr = lambda s: display(Markdown('$'+str(latex(s))+'$'))
def psym_matrix(a, intro='',ending='',row=False):
    try:
        if row:
                return(intro+str(latex(a))+ending)
        else:
                display(Latex("$$"+intro+str(latex(a))+ending+"$$"))
    except TypeError:
        display(latex(a)) #TODO MAY BY...
pr = lambda s: display(Markdown('$'+str(latex(s))+'$'))

def pmatrix(a, intro='',ending='',row=False):
    if len(a.shape) > 2:
        raise ValueError('pmatrix can at most display two dimensions')
    lines = str(a).replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{pmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{pmatrix}']
    if row:
        return(intro+'\n'.join(rv)+ending)
    else:
        display(Latex('$$'+intro+'\n'.join(rv)+ending+'$$'))
def crearMatrix(name,shape=(2,2)):
    X = []
    for i in range(shape[0]):
        row = []
        for j in range(shape[1]):
            row.append(Symbol(name+'_{'+str(i*10+j+11)+'}'))
        X.append(row)
    return Matrix(X)

In [43]:
from re import sub
def matrix_to_word(A):
    replaced = sub(r"begin{matrix}", r"(■( ", psym_matrix(A,row=1))
    replaced = sub(r"{", r"", replaced)
    replaced = sub(r"}", r"", replaced)
    replaced = sub(r"\\\\\\\\", r" @ ", replaced)
    replaced = sub(r"\\\\", r" @ ", replaced)
    replaced = sub(r"[\[\]]", r"", replaced)
    replaced = sub(r"left", r"", replaced)
    replaced = sub(r".endmatrix.right", r" ))", replaced)
    replaced = sub(r"\\", r"", replaced)
    print(replaced )

In [44]:
X = crearMatrix("x")
B = crearMatrix("b")

D= crearMatrix("d")
C= crearMatrix("c")

psym_matrix(X, intro="X=")
matrix_to_word(X)
#psym_matrix(B, intro="B=")
#psym_matrix(B, intro="D=")
#psym_matrix(C, intro="C=")


$$X=\left[\begin{matrix}x_{11} & x_{12}\\x_{21} & x_{22}\end{matrix}\right]$$
(■( x_11 & x_12 @ x_21 & x_22 ))

$$ CXD=B $$


In [5]:
psym_matrix(D, 
            intro=psym_matrix(C,row=1)+psym_matrix(X,row=1),
            ending="="+ psym_matrix(B,row=1))


$$\left[\begin{matrix}c_{11} & c_{12}\\c_{21} & c_{22}\end{matrix}\right]\left[\begin{matrix}x_{11} & x_{12}\\x_{21} & x_{22}\end{matrix}\right]\left[\begin{matrix}d_{11} & d_{12}\\d_{21} & d_{22}\end{matrix}\right]=\left[\begin{matrix}b_{11} & b_{12}\\b_{21} & b_{22}\end{matrix}\right]$$

In [6]:
psym_matrix(C*X*D, ending="="+ psym_matrix(B,row=1))


$$\left[\begin{matrix}d_{11} \left(c_{11} x_{11} + c_{12} x_{21}\right) + d_{21} \left(c_{11} x_{12} + c_{12} x_{22}\right) & d_{12} \left(c_{11} x_{11} + c_{12} x_{21}\right) + d_{22} \left(c_{11} x_{12} + c_{12} x_{22}\right)\\d_{11} \left(c_{21} x_{11} + c_{22} x_{21}\right) + d_{21} \left(c_{21} x_{12} + c_{22} x_{22}\right) & d_{12} \left(c_{21} x_{11} + c_{22} x_{21}\right) + d_{22} \left(c_{21} x_{12} + c_{22} x_{22}\right)\end{matrix}\right]=\left[\begin{matrix}b_{11} & b_{12}\\b_{21} & b_{22}\end{matrix}\right]$$

In [21]:
psym_matrix((C*X*D).reshape(4, 1),
            ending="="+ psym_matrix(B.reshape(4, 1),row=1))


$$\left[\begin{matrix}d_{11} \left(c_{11} x_{11} + c_{12} x_{21}\right) + d_{21} \left(c_{11} x_{12} + c_{12} x_{22}\right)\\d_{12} \left(c_{11} x_{11} + c_{12} x_{21}\right) + d_{22} \left(c_{11} x_{12} + c_{12} x_{22}\right)\\d_{11} \left(c_{21} x_{11} + c_{22} x_{21}\right) + d_{21} \left(c_{21} x_{12} + c_{22} x_{22}\right)\\d_{12} \left(c_{21} x_{11} + c_{22} x_{21}\right) + d_{22} \left(c_{21} x_{12} + c_{22} x_{22}\right)\end{matrix}\right]=\left[\begin{matrix}b_{11}\\b_{12}\\b_{21}\\b_{22}\end{matrix}\right]$$

In [40]:
R = (Matrix([ # How???
    [ C[0,0], 0     , C[1,0], 0     ,],
    [ 0     , C[0,0], 0     , C[1,0],],
    [ C[0,1], 0     , C[1,1], 0     ,],
    [ 0     , C[0,1], 0     , C[1,1],]
])).T
T = Matrix([ # How???
    [D[0,0],D[1,0],   0  , 0    ],
    [D[0,1],D[1,1],   0  , 0    ],
    [ 0    , 0    ,D[0,0],D[1,0]],
    [ 0    , 0    ,D[0,1],D[1,1]]
])
#psym_matrix(T,row=1)
psym_matrix(T*R)

matrix_to_word(T*R)


$$\left[\begin{matrix}c_{11} d_{11} & c_{11} d_{21} & c_{12} d_{11} & c_{12} d_{21}\\c_{11} d_{12} & c_{11} d_{22} & c_{12} d_{12} & c_{12} d_{22}\\c_{21} d_{11} & c_{21} d_{21} & c_{22} d_{11} & c_{22} d_{21}\\c_{21} d_{12} & c_{21} d_{22} & c_{22} d_{12} & c_{22} d_{22}\end{matrix}\right]$$
(■( c_11 d_11 & c_11 d_21 & c_12 d_11 & c_12 d_21 @ c_11 d_12 & c_11 d_22 & c_12 d_12 & c_12 d_22 @ c_21 d_11 & c_21 d_21 & c_22 d_11 & c_22 d_21 @ c_21 d_12 & c_21 d_22 & c_22 d_12 & c_22 d_22 ))

In [9]:
psym_matrix(X.reshape(1, 4))
psym_matrix(X.reshape(1, 4)*R*T)


$$\left[\begin{matrix}x_{11} & x_{12} & x_{21} & x_{22}\end{matrix}\right]$$
$$\left[\begin{matrix}d_{11} \left(c_{11} x_{11} + c_{12} x_{21}\right) + d_{21} \left(c_{11} x_{12} + c_{12} x_{22}\right) & d_{12} \left(c_{11} x_{11} + c_{12} x_{21}\right) + d_{22} \left(c_{11} x_{12} + c_{12} x_{22}\right) & d_{11} \left(c_{21} x_{11} + c_{22} x_{21}\right) + d_{21} \left(c_{21} x_{12} + c_{22} x_{22}\right) & d_{12} \left(c_{21} x_{11} + c_{22} x_{21}\right) + d_{22} \left(c_{21} x_{12} + c_{22} x_{22}\right)\end{matrix}\right]$$

In [10]:
psym_matrix(F, intro=r'rg(F)=', ending="\Leftrightarrow\exists F^{-1}") #ранг


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-45e4ed4abb09> in <module>()
----> 1 psym_matrix(F, intro=r'rg(F)=', ending="\Leftrightarrow\exists F^{-1}") #ранг

NameError: name 'F' is not defined
$$\sum_a^bAD$$

In [24]:
R = Matrix([ # How???
    [3,1,0,0],
    [1,0,0,0],
    [0,0,3,1],
    [0,0,1,0]
])
T = Matrix([ # How???
    [1,0,0,0],
    [0,1,0,0],
    [2,0,1,0],
    [0,2,0,1]
])
#psym_matrix(T,row=1)
psym_matrix(T*R)


$$\left[\begin{matrix}3 & 1 & 0 & 0\\1 & 0 & 0 & 0\\6 & 2 & 3 & 1\\2 & 0 & 1 & 0\end{matrix}\right]$$

In [ ]: