<jupyter_start><jupyter_text>Подготовка:<jupyter_code>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
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 )<jupyter_output><empty_output><jupyter_text>3 пункт пример: Дано:<jupyter_code>X = np.array([[Symbol("x_{11}"), Symbol("x_{12}")],[Symbol("x_{21}"),Symbol("x_{22}")]])
pmatrix(X, intro="X=")
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]])

W = np.array([
    [0,2,1,6],
    [0,0,1,2],
    [0,0,0,3],
    [0,0,0,0]
])
print(rg(W))<jupyter_output>3<jupyter_text>Распишим  $$C^1XD^1+C^2XD^2$$<jupyter_code>A = (C1.dot(X)).dot(D1) + (C2.dot(X)).dot(D2)
pmatrix(A)<jupyter_output><empty_output><jupyter_text>Приведём к виду Ax=b, где x,b вектора<jupyter_code>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))
pmatrix(rg(F), intro=r'rg(F)=', ending="\Leftrightarrow\exists F^{-1}\sum") #ранг<jupyter_output><empty_output><jupyter_text>Решаем это:<jupyter_code>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=")<jupyter_output><empty_output><jupyter_text>3 пункт обощение:  Рассмотрим $$C^1XD^1+C^2XD^2=B$$  Обозначения:  Примечание: $$С^n,D^n,\textbf{ где n не степень!} $$<jupyter_code>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=")<jupyter_output><empty_output><jupyter_text>Символьно посчитаем выражение$$A = C^1XD^1+C^2XD^2$$<jupyter_code>A = C2*X*D1+C2*X*D2
psym_matrix(crearMatrix("a"), intro="A=")
psym_matrix(A)<jupyter_output><empty_output><jupyter_text>Выражаем вектор x из A:  пришлось вручную...<jupyter_code>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]]
])
psym_matrix(F,intro="F=")
matrix_to_word(F)
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))<jupyter_output><empty_output><jupyter_text>Разложение  $$ \exists R,T: R_{4x4}T_{4x4}X_{4x1}=B_{4x1}$$<jupyter_code>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]]
])
psym_matrix(T1*R1) 
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)
psym_matrix(R1*T1,
           ending="+"+psym_matrix(R2*T2,row=1))
psym_matrix(R1*T1+R2*T2)<jupyter_output><empty_output><jupyter_text>А что если... $$ X_{1x4}R^{'} _{4x4}T^{'} _{4x4}=B_{1x4}$$<jupyter_code>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=")
print("Для удобства транспонируем")
psym_matrix((C*X*D).reshape(4, 1),
            ending="="+ psym_matrix(B.reshape(4, 1),row=1))
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))
psym_matrix(R_*T_, row=1)<jupyter_output><empty_output><jupyter_text>Заметим, что: $$ R^{'},T^{'} = R^TT^T $$<jupyter_code>P1 = R1*T1
P2 = R2*T2
print(P2*P1 == P1*P2)
print(R1*T1 == T1*R1)<jupyter_output>False
True
{'fraction_non_alphanumeric': 0.26157341664299916, 'fraction_numerical': 0.08165293950582221, 'mean_word_length': 3.6611515552614162, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '483033', 'license': 'MIT', 'n_tokens_mistral': 3549, 'n_tokens_neox': 3499, 'n_words': 442, 'path': '7ex/3.ipynb', 'repo_name': 'TeamProgramming/ITYM', 'size': '7042'}