In [6]:
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 [22]:
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 )
def isNilpotent(A):
i = 0
for n in range(25):
if A**n == zeros(4,4):
return i;
else:
i+=1
return 0
In [11]:
D1= crearMatrix("d^1")
C1= crearMatrix("c^1")
D2= crearMatrix("d^2")
C2= crearMatrix("c^2")
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 [25]:
D1 = Matrix([[1,1],
[1,2]])
C1 = Matrix([[2,1],
[1,0]])
D2 = Matrix([[1,-1],
[0,1]])
C2 = Matrix([[1,1],
[0,1]])
In [26]:
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],]
])).T
T1 = Matrix([ # How???
[D1[0,0],D1[0,1], 0 , 0 ],
[D1[1,0],D1[1,1], 0 , 0 ],
[ 0 , 0 ,D1[0,0],D1[0,1]],
[ 0 , 0 ,D1[1,0],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],]
])).T
T2 = (Matrix([ # How???
[D2[0,0],D2[0,1], 0 , 0 ],
[D2[1,0],D2[1,1], 0 , 0 ],
[ 0 , 0 ,D2[0,0],D2[0,1]],
[ 0 , 0 ,D2[1,0],D2[1,1]]
]))
In [27]:
psym_matrix(R1, ending=psym_matrix(T1,row=1))
psym_matrix(R2, ending=psym_matrix(T2,row=1))
In [28]:
psym_matrix(R1*T1+R2*T2)
In [24]:
psym_matrix(crearMatrix('g').T)
In [ ]: