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 [2]:
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)
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 [3]:
X = np.array([[Symbol("x_{11}"), Symbol("x_{12}")],[Symbol("x_{21}"),Symbol("x_{22}")]])
pmatrix(X, intro="X=")
In [4]:
B = np.array([[5,1],
[5,2]])
C1 = np.array([[1,1],
[1,2]])
D1 = np.array([[2,1],
[1,0]])
C2 = np.array([[1,-1],
[0,1]])
D2 = np.array([[1,1],
[0,1]])
In [5]:
W = np.array([
[0,2,1,6],
[0,0,1,2],
[0,0,0,3],
[0,0,0,0]
])
print(rg(W))
In [6]:
A = (C1.dot(X)).dot(D1) + (C2.dot(X)).dot(D2)
pmatrix(A)
In [7]:
F = np.array([[3,1,1,1],
[2,1,0,-1],
[2,1,5,2],
[1,0,3,1]])
pmatrix(F, ending=pmatrix(X.reshape((4, 1)),row=True)+"="+pmatrix(B.reshape((4, 1)),row=True))
In [8]:
pmatrix(rg(F), intro=r'rg(F)=', ending="\Leftrightarrow\exists F^{-1}\sum") #ранг
In [9]:
from sympy.abc import a,b,c,d
system = Matrix(( (3,1,1,1,5), (2,1,0,-1,1), (2,1,5,2,5),(1,0,3,1,2) ))
x = solve_linear_system(system, a,b,c,d)
X =np.array([[x[a],x[b]],[x[c],x[d]] ])
pmatrix(X,intro="X=")
In [10]:
X = crearMatrix("x")
B = crearMatrix("b")
D1= crearMatrix("d^1")
C1= crearMatrix("c^1")
D2= crearMatrix("d^2")
C2= crearMatrix("c^2")
psym_matrix(X, intro="X=")
psym_matrix(B, intro="B=")
psym_matrix(D1, intro="D^1=")
psym_matrix(C1, intro="C^1=")
psym_matrix(D2, intro="D^2=")
psym_matrix(C2, intro="C^2=")
In [11]:
A = C2*X*D1+C2*X*D2
psym_matrix(crearMatrix("a"), intro="A=")
In [12]:
psym_matrix(A)
In [13]:
F = Matrix([ # How???
[ D1[0,0]*C1[0,0]+D2[0,0]*C2[0,0], D1[1,0]*C1[0,0]+D2[1,0]*C2[0,0], D1[0,0]*C1[0,1]+D2[0,0]*C2[0,1], D1[1,0]*C1[0,1]+D2[1,0]*C2[0,1]],
[ D1[0,0]*C1[0,0]+D2[0,1]*C2[0,0], D1[1,1]*C1[0,0]+D2[1,1]*C2[0,0], D1[0,1]*C1[0,1]+D2[0,1]*C2[0,1], D1[1,1]*C1[0,1]+D2[1,1]*C2[0,1]],
[ D1[0,1]*C1[1,0]+D2[0,0]*C2[1,0], D1[1,0]*C1[1,0]+D2[1,0]*C2[1,0], D1[0,0]*C1[1,1]+D2[0,0]*C2[1,1], D1[1,0]*C1[1,1]+D2[1,0]*C2[1,1]],
[ D1[0,1]*C1[1,0]+D2[0,1]*C2[1,0], D1[1,1]*C1[1,0]+D2[1,1]*C2[1,0], D1[0,1]*C1[1,1]+D2[0,1]*C2[1,1], D1[1,1]*C1[1,1]+D2[1,1]*C2[1,1]]
])
In [14]:
psym_matrix(F,intro="F=")
In [15]:
matrix_to_word(F)
In [16]:
psym_matrix(X.reshape(4, 1),
ending="="+psym_matrix(B.reshape(4, 1),
row=True),
intro=psym_matrix(crearMatrix("f",shape=(4,4)),
row=True))
In [17]:
R1 = Matrix([ # How???
[ C1[0,0], 0 , C1[0,1], 0 ,],
[ 0 , C1[0,0], 0 , C1[0,1],],
[ C1[1,0], 0 , C1[1,1], 0 ,],
[ 0 , C1[1,0], 0 , C1[1,1],]
])
T1 = Matrix([ # How???
[D1[0,0],D1[1,0], 0 , 0 ],
[D1[0,1],D1[1,1], 0 , 0 ],
[ 0 , 0 ,D1[0,0],D1[1,0]],
[ 0 , 0 ,D1[0,1],D1[1,1]]
])
R2 = Matrix([ # How???
[ C2[0,0], 0 , C2[0,1], 0 ,],
[ 0 , C2[0,0], 0 , C2[0,1],],
[ C2[1,0], 0 , C2[1,1], 0 ,],
[ 0 , C2[1,0], 0 , C2[1,1],]
])
T2 = Matrix([ # How???
[D2[0,0],D2[1,0], 0 , 0 ],
[D2[0,1],D2[1,1], 0 , 0 ],
[ 0 , 0 ,D2[0,0],D2[1,0]],
[ 0 , 0 ,D2[0,1],D2[1,1]]
])
In [19]:
psym_matrix(T1*R1)
In [ ]:
psym_matrix(T1,
intro=psym_matrix(R1,row=True),
ending="+"+psym_matrix(T2, row=True,
intro=psym_matrix(R2,row=True))
)
matrix_to_word(T1)
In [ ]:
psym_matrix(R1*T1,
ending="+"+psym_matrix(R2*T2,row=1))
In [ ]:
psym_matrix(R1*T1+R2*T2)
In [ ]:
X = crearMatrix("x")
B = crearMatrix("b")
D= crearMatrix("d")
C= crearMatrix("c")
psym_matrix(X, intro="X=")
psym_matrix(B, intro="B=")
psym_matrix(B, intro="D=")
psym_matrix(C, intro="C=")
In [ ]:
print("Для удобства транспонируем")
psym_matrix((C*X*D).reshape(4, 1),
ending="="+ psym_matrix(B.reshape(4, 1),row=1))
In [ ]:
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_ = Matrix([ # How???
[D[0,0],D[0,1], 0 , 0 ],
[D[1,0],D[1,1], 0 , 0 ],
[ 0 , 0 ,D[0,0],D[0,1]],
[ 0 , 0 ,D[1,0],D[1,1]]
])
psym_matrix(T_,
intro=psym_matrix(X.reshape(1,4),row=1)+psym_matrix(R_,row=1),
ending="="+psym_matrix(B.reshape(1,4),row=1))
In [ ]:
psym_matrix(R_*T_, row=1)
In [20]:
P1 = R1*T1
P2 = R2*T2
In [23]:
print(P2*P1 == P1*P2)
print(R1*T1 == T1*R1)
In [ ]: