"""MANIPULACIÓN DE MATRICES(OSEA LISTAS) CON PYTHON
Josué Diaz Avalos
Resumen
Aqui algunas funciones para hacer algunas cosas útiles con matrices en Python.
Las Funciones:"""
#1.-
def con1(M1,M2):
c1=len(M1[0])
c2=len(M2[0])
f=len(M1)
M=[]
M=[[0 for j in range(c1+c2)] for i in range(f) ]
for i in range(f):
M[i][0:c1]=M1[i][:]
M[i][c1:c1+c2]=M2[i][:]
return M
#2.-
def con2(M1,M2):
f1=len(M1)
f2=len(M2)
c=len(M1[0])
M=[]
M=[[0 for j in range(c)] for i in range(f1+f2) ]
for i in range(f1):
M[i][:]=M1[i][:]
for i in range(f2):
M[f1+i][:]=M2[i][:]
return M
#3.-
def dia1(M1,M2):
f1=len(M1)
c1=len(M1[0])
f2=len(M2)
c2=len(M2[0])
f=f1+f2
c=c1+c2
a=[]
a=[[0 for j in range(c2)] for i in range(f1) ]
b=[]
b=[[0 for j in range(c1)] for i in range(f2) ]
c=con1(M1,a)
d=con1(b,M2)
M=con2(c,d)
return M
#4.-
def dia2(M1,M2):
f1=len(M1)
c1=len(M1[0])
f2=len(M2)
c2=len(M2[0])
f=f1+f2
c=c1+c2
a=[]
a=[[0 for j in range(c2)] for i in range(f1) ]
b=[]
b=[[0 for j in range(c1)] for i in range(f2) ]
c=con1(a,M1)
d=con1(M2,b)
M=con2(c,d)
return M
#5.-
def diag3(N,n):
Ce=zero(n*len(N),n*len(N[0]))
A=range(0,n*len(N),len(N))
B=range(0,n*len(N[0]),len(N[0]))
for k in range(n):
for i1 in range(len(N)):
for j1 in range(len(N[0])):
Ce[A[k]+i1][B[k]+j1]=N[i1][j1]
return Ce
#6.-
def diag3_0arriba(N,n):
M1=diag3(N,n)
Ce=zero(len(N),n*len(N[0]))
M=con2(Ce,M1)
return M
#7.-
def diag3_0miderecha(N,n):
M1=diag3(N,n)
Ce=zero(n*len(N),len(N[0]))
M=con1(M1,Ce)
return M
#8.-
def diag3_0miizquierda(N,n):
M1=diag3(N,n)
Ce=zero(n*len(N),len(N[0]))
M=con1(Ce,M1)
return M
#9.-
def diag3_0abajo(N,n):
M1=diag3(N,n)
Ce=zero(len(N),n*len(N[0]))
M=con2(M1,Ce)
return M
#10.-
def diag4(N,n):
f=len(N)
c=len(N[0])
Ce=zero(n*f,n*c)
A=range(0,(n-1)*f,f)
B=range(c,n*c,c)
for k in range(n-1):
for i1 in range(len(N)):
for j1 in range(len(N[0])):
Ce[A[k]+i1][B[k]+j1]=N[i1][j1]
return Ce
#11.-
def diag5(N,n):
f=len(N)
c=len(N[0])
Ce=zero(n*f,n*c)
A=range(f,n*f,f)
B=range(0,(n-1)*c,c)
for k in range(n-1):
for i1 in range(len(N)):
for j1 in range(len(N[0])):
Ce[A[k]+i1][B[k]+j1]=N[i1][j1]
return Ce
#12.-
def diag6(N1,N2,n):
A=diag4(N1,n)
B=diag5(N2,n)
C=zero(len(A),len(A[0]))
for i in range(len(B)):
for j in range(len(B[0])):
C[i][j]=A[i][j]+B[i][j]
return C
#13.-
def M1(n):
M=[]
M=[[0 for j in range(n)] for i in range(n) ]
for i in range(n-1):
M[i][i+1]=1
return M
#14.-
def H1(n):
M=[]
M=[[0 for j in range(n+1)] for i in range(2*n) ]
for i in range(n):
M[i][i+1]=1
M[n+i][n-i-1]=1
return M
#15.-
def H2(n):
M=[]
M=[[0 for j in range(2*n)] for i in range(n+1) ]
for i in range(n):
M[i][i]=1
M[n-i][n+i]=1
return M
#16.-
def zero(f,c):
M=[]
M=[[0 for j in range(c)] for i in range(f) ]
return M
#17.-
def iden(n):
M=[]
M=[[0 for j in range(n)] for i in range(n) ]
for i in range(n):
M[i][i]=1
return M
#18.-
def sum(M,N):
S=[]
S=[[M[i][j]+N[i][j] for j in range(len(N[0]))] for i in range(len(M))]
return S
No hay comentarios:
Publicar un comentario