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=")
In [5]:
psym_matrix(D,
intro=psym_matrix(C,row=1)+psym_matrix(X,row=1),
ending="="+ psym_matrix(B,row=1))
In [6]:
psym_matrix(C*X*D, ending="="+ psym_matrix(B,row=1))
In [21]:
psym_matrix((C*X*D).reshape(4, 1),
ending="="+ psym_matrix(B.reshape(4, 1),row=1))
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)
In [9]:
psym_matrix(X.reshape(1, 4))
psym_matrix(X.reshape(1, 4)*R*T)
In [10]:
psym_matrix(F, intro=r'rg(F)=', ending="\Leftrightarrow\exists F^{-1}") #ранг
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)
In [ ]: