Подготовка:


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 )

3 пункт

пример:

Дано:


In [3]:
X = np.array([[Symbol("x_{11}"), Symbol("x_{12}")],[Symbol("x_{21}"),Symbol("x_{22}")]])
pmatrix(X, intro="X=")


$$X=\begin{pmatrix} x_{11} & x_{12}\\ x_{21} & x_{22}\\ \end{pmatrix}$$

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))


3

Распишим $$C^1XD^1+C^2XD^2$$


In [6]:
A = (C1.dot(X)).dot(D1) + (C2.dot(X)).dot(D2)
pmatrix(A)


$$\begin{pmatrix} 3*x_{11} & + & x_{12} & + & x_{21} & + & x_{22} & 2*x_{11} & + & x_{12} & - & x_{22}\\ 2*x_{11} & + & x_{12} & + & 5*x_{21} & + & 2*x_{22} & x_{11} & + & 3*x_{21} & + & x_{22}\\ \end{pmatrix}$$

Приведём к виду Ax=b, где x,b вектора


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))


$$\begin{pmatrix} 3 & 1 & 1 & 1\\ 2 & 1 & 0 & -1\\ 2 & 1 & 5 & 2\\ 1 & 0 & 3 & 1\\ \end{pmatrix}\begin{pmatrix} x_{11}\\ x_{12}\\ x_{21}\\ x_{22}\\ \end{pmatrix}=\begin{pmatrix} 5\\ 1\\ 5\\ 2\\ \end{pmatrix}$$

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


$$rg(F)=\begin{pmatrix} 4\\ \end{pmatrix}\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=")


$$X=\begin{pmatrix} 10/11 & 9/11\\ -2/11 & 18/11\\ \end{pmatrix}$$

3 пункт обощение:

Рассмотрим $$C^1XD^1+C^2XD^2=B$$

Обозначения:

Примечание: $$С^n,D^n,\textbf{ где n не степень!} $$


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=")


$$X=\left[\begin{matrix}x_{11} & x_{12}\\x_{21} & x_{22}\end{matrix}\right]$$
$$B=\left[\begin{matrix}b_{11} & b_{12}\\b_{21} & b_{22}\end{matrix}\right]$$
$$D^1=\left[\begin{matrix}d^1_{11} & d^1_{12}\\d^1_{21} & d^1_{22}\end{matrix}\right]$$
$$C^1=\left[\begin{matrix}c^1_{11} & c^1_{12}\\c^1_{21} & c^1_{22}\end{matrix}\right]$$
$$D^2=\left[\begin{matrix}d^2_{11} & d^2_{12}\\d^2_{21} & d^2_{22}\end{matrix}\right]$$
$$C^2=\left[\begin{matrix}c^2_{11} & c^2_{12}\\c^2_{21} & c^2_{22}\end{matrix}\right]$$

Символьно посчитаем выражение

$$A = C^1XD^1+C^2XD^2$$

In [11]:
A = C2*X*D1+C2*X*D2
psym_matrix(crearMatrix("a"), intro="A=")


$$A=\left[\begin{matrix}a_{11} & a_{12}\\a_{21} & a_{22}\end{matrix}\right]$$

In [12]:
psym_matrix(A)


$$\left[\begin{matrix}d^1_{11} \left(c^2_{11} x_{11} + c^2_{12} x_{21}\right) + d^1_{21} \left(c^2_{11} x_{12} + c^2_{12} x_{22}\right) + d^2_{11} \left(c^2_{11} x_{11} + c^2_{12} x_{21}\right) + d^2_{21} \left(c^2_{11} x_{12} + c^2_{12} x_{22}\right) & d^1_{12} \left(c^2_{11} x_{11} + c^2_{12} x_{21}\right) + d^1_{22} \left(c^2_{11} x_{12} + c^2_{12} x_{22}\right) + d^2_{12} \left(c^2_{11} x_{11} + c^2_{12} x_{21}\right) + d^2_{22} \left(c^2_{11} x_{12} + c^2_{12} x_{22}\right)\\d^1_{11} \left(c^2_{21} x_{11} + c^2_{22} x_{21}\right) + d^1_{21} \left(c^2_{21} x_{12} + c^2_{22} x_{22}\right) + d^2_{11} \left(c^2_{21} x_{11} + c^2_{22} x_{21}\right) + d^2_{21} \left(c^2_{21} x_{12} + c^2_{22} x_{22}\right) & d^1_{12} \left(c^2_{21} x_{11} + c^2_{22} x_{21}\right) + d^1_{22} \left(c^2_{21} x_{12} + c^2_{22} x_{22}\right) + d^2_{12} \left(c^2_{21} x_{11} + c^2_{22} x_{21}\right) + d^2_{22} \left(c^2_{21} x_{12} + c^2_{22} x_{22}\right)\end{matrix}\right]$$

Выражаем вектор x из 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=")


$$F=\left[\begin{matrix}c^1_{11} d^1_{11} + c^2_{11} d^2_{11} & c^1_{11} d^1_{21} + c^2_{11} d^2_{21} & c^1_{12} d^1_{11} + c^2_{12} d^2_{11} & c^1_{12} d^1_{21} + c^2_{12} d^2_{21}\\c^1_{11} d^1_{11} + c^2_{11} d^2_{12} & c^1_{11} d^1_{22} + c^2_{11} d^2_{22} & c^1_{12} d^1_{12} + c^2_{12} d^2_{12} & c^1_{12} d^1_{22} + c^2_{12} d^2_{22}\\c^1_{21} d^1_{12} + c^2_{21} d^2_{11} & c^1_{21} d^1_{21} + c^2_{21} d^2_{21} & c^1_{22} d^1_{11} + c^2_{22} d^2_{11} & c^1_{22} d^1_{21} + c^2_{22} d^2_{21}\\c^1_{21} d^1_{12} + c^2_{21} d^2_{12} & c^1_{21} d^1_{22} + c^2_{21} d^2_{22} & c^1_{22} d^1_{12} + c^2_{22} d^2_{12} & c^1_{22} d^1_{22} + c^2_{22} d^2_{22}\end{matrix}\right]$$

In [15]:
matrix_to_word(F)


(■( c^1_11 d^1_11 + c^2_11 d^2_11 & c^1_11 d^1_21 + c^2_11 d^2_21 & c^1_12 d^1_11 + c^2_12 d^2_11 & c^1_12 d^1_21 + c^2_12 d^2_21 @ c^1_11 d^1_11 + c^2_11 d^2_12 & c^1_11 d^1_22 + c^2_11 d^2_22 & c^1_12 d^1_12 + c^2_12 d^2_12 & c^1_12 d^1_22 + c^2_12 d^2_22 @ c^1_21 d^1_12 + c^2_21 d^2_11 & c^1_21 d^1_21 + c^2_21 d^2_21 & c^1_22 d^1_11 + c^2_22 d^2_11 & c^1_22 d^1_21 + c^2_22 d^2_21 @ c^1_21 d^1_12 + c^2_21 d^2_12 & c^1_21 d^1_22 + c^2_21 d^2_22 & c^1_22 d^1_12 + c^2_22 d^2_12 & c^1_22 d^1_22 + c^2_22 d^2_22 ))

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))


$$\left[\begin{matrix}f_{11} & f_{12} & f_{13} & f_{14}\\f_{21} & f_{22} & f_{23} & f_{24}\\f_{31} & f_{32} & f_{33} & f_{34}\\f_{41} & f_{42} & f_{43} & f_{44}\end{matrix}\right]\left[\begin{matrix}x_{11}\\x_{12}\\x_{21}\\x_{22}\end{matrix}\right]=\left[\begin{matrix}b_{11}\\b_{12}\\b_{21}\\b_{22}\end{matrix}\right]$$

Разложение

$$ \exists R,T: R_{4x4}T_{4x4}X_{4x1}=B_{4x1}$$


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)


$$\left[\begin{matrix}c^1_{11} d^1_{11} & c^1_{11} d^1_{21} & c^1_{12} d^1_{11} & c^1_{12} d^1_{21}\\c^1_{11} d^1_{12} & c^1_{11} d^1_{22} & c^1_{12} d^1_{12} & c^1_{12} d^1_{22}\\c^1_{21} d^1_{11} & c^1_{21} d^1_{21} & c^1_{22} d^1_{11} & c^1_{22} d^1_{21}\\c^1_{21} d^1_{12} & c^1_{21} d^1_{22} & c^1_{22} d^1_{12} & c^1_{22} d^1_{22}\end{matrix}\right]$$

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)

А что если... $$ X_{1x4}R^{'} _{4x4}T^{'} _{4x4}=B_{1x4}$$


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)

Заметим, что: $$ R^{'},T^{'} = R^TT^T $$


In [20]:
P1 = R1*T1
P2 = R2*T2

In [23]:
print(P2*P1 == P1*P2)
print(R1*T1 == T1*R1)


False
True

In [ ]: