Practica 1
Practica 1
Practica 1
1 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
NOTA: en SAGE solo se muestra la salida de la ltima linea. Si queremos mostrar salidas
intermedias utilizamos los comandos print,html o show, esta ltima muestra la salida
en formato enrriquecido. Tenemos tambin la posibilidad de expresar la salida en LATEX .
IMPORTANTE: en SAGE los ndices empiezan en "cero", es decir, el elemento a 11 de una
matriz es A[0,0]
M=matrix(3,4)
print M
print "--------------------------"
M=matrix(3,4,range(12))
print M
print "--------------------------"
M=matrix(3,4,[1 .. 12])
print M
print "--------------------------"
M=matrix(3,4,[7,6,9,2,3,9,-4,-3,8,6,7,1])
show(M)
print "--------------------------"
print latex(M)
print "--------------------------"
html("el elemento $a_{11}$ es M[0,0]: " + M[0,0].str())
print "--------------------------"
OBJETOS Y METODOS
19/10/2013 17:08
2 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
En SAGE todos los elmentos que definimos son "objetos" y como tales, tienen "mtodos".
Por ejemplo, en el caso de las matrices estos metodos son: efectuar operaciones elementales
(add_multiple_of_row, swap_rows y rescale_row), obtener el rango (rank), calcular su
forma reducida por filas (echelon_form), determinar todos los menores de un orden dado
(minors), calcular su determinante (det o determinat), descomposicin LU (LU), etc...
Tenemos que tener presente que algunos de estos mtodos producen cambios en la matriz
original (es una forma de reservar espacio en memoria del procesador).
Si queremos saber todos los mtodos de un objeto determinado, escribimos:
objeto + "." + <TAB>
Si queremos obtener ayuda sobre un mtodo escribimos:
objeto + "." + mtodo + "(" + <TAB>
M.rescale_row(1,1/7) # error debido a que por defecto las matrices
se definen en el conjunto de los enteros
19/10/2013 17:08
3 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
print type(M2)
print type(M3)
OPERACIONES ELEMENTALES
M=matrix(3,4,[7.0,6,9,2,3,9,-4,-3,8,6,7,1])
show(M)
C=M.copy()
C.rescale_row(0,1/7)
show(C)
M=matrix(3,4,[7.0,6,9,2,3,9,-4,-3,8,6,7,1])
show(M)
C=M.copy()
C.add_multiple_of_row(1,0,-3/7)
show(C)
M=matrix(3,4,[7.0,6,9,2,3,9,-4,-3,8,6,7,1])
show(M)
C=M.copy()
C.swap_rows(1,0)
show(C)
DEFINIENDO MATRICES II
Otra forma de definir matrices con condiciones es utilizando el iterador "lambda"
P=matrix(RDF,3,4, lambda i,j: i+j)
P
19/10/2013 17:08
4 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
def A(i,j):
return i+j
#definimos matriz
P=matrix(RDF,3,4, lambda i,j: A(i,j))
P
19/10/2013 17:08
5 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
M.inverse()
M.det()
show((M*T).inverse())
show((M*T).det())
RANGO DE UNA MATRIZ: Forma escalonada por filas (rref), rango (rank) o estudio
de Menores (minors)
1
1
3
1
n
1
3
5
m
NOTA: en SAGE el nico parmetro (variable) por defecto es x, si queremos utilizar otras
hay que definirlas antes con "var"
#definimos los parmetros
var('n,m')
#definimos la matriz
C=matrix([[1,3,-3],[1,-1,5],[0,n,m],[m,-1,-4]])
C.rref() #
C.rank() #
C.minors(3)
# el comando solve nos permite calcular los valores para los que se
anula una ecuacin, en este caso cada
19/10/2013 17:08
6 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
@interact
def _(M=input_grid(D,D, default = example,
label='Matriz para invertir', to_value=matrix),
tt = text_control('bits de precisin usados, '
' (solo nmeros de coma flotante)'),
precision = slider(5,100,5,20,'precisin'),
auto_update=False):
if det(M)==0:
print 'Fallo: La Matriz no es Invertible'
return
if M.base_ring() == RR:
M = M.apply_map(RealField(precision))
N=M
M=M.augment(identity_matrix(D),subdivide=true)
print 'Construimos la matriz aumentada'
show(M)
for m in range(0,D-1):
if M[m,m] == 0:
lista = [(M[j,m],j) for j in range(m,D)]
maxi, c = max(lista)
M[c,:],M[m,:]=M[m,:],M[c,:]
print 'Permutamos filas %d y %d'%(m+1,c+1)
19/10/2013 17:08
7 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
show(M)
for n in range(m+1,D):
a=M[m,m]
if M[n,m]!=0:
print "Aadimos %s veces la fila %d a la fila
%d"%(-M[n,m]/a, m+1, n+1)
M=M.with_added_multiple_of_row(n,m,-M[n,m]/a)
show(M)
for m in range(D-1,-1,-1):
for n in range(m-1,-1,-1):
a=M[m,m]
if M[n,m]!=0:
print "Aadimos %s veces la fila %d a la fila
%d"%(-M[n,m]/a, m+1, n+1)
M=M.with_added_multiple_of_row(n,m,-M[n,m]/a)
show(M)
for m in range(0,D):
if M[m,m]!=1:
print 'Dividimos la fila %d por %s'%(m+1,M[m,m])
M = M.with_row_set_to_multiple_of_row(m,m,1/M[m,m])
show(M)
M=M.submatrix(0,D,D)
print 'Conservamos la submatriz de la derecha, que contiene la
inversa'
html('$$M^{-1}=%s$$'%latex(M))
print 'Comprobamos que, efectivamente es la inversa'
html('$$M^{-1}*M=%s*%s=%s$$'%(latex(M),latex(N),latex(M*N)))
19/10/2013 17:08
8 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
M*V
M*V.transpose()
NORMAS VECTORIALES
Definicin: Sea B un espacio vectorial real, R n . Se define una norma como una funcin
: B R tal que:
1.
2.
3.
x 0 , y x = 0 x = 0 (definida positiva);
x = ||x para cualquier escalar (real) (homogenea); y
x + y x + y (desigualdad triangular).
1/p
x 1 = |x i |
i
x 2 = (x 2i )
1/2
b.
19/10/2013 17:08
9 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
NORMAS MATRICIALES
Definicin: es una norma matricial definida en las matrices de orden m x n si es una
norma vectorial en un espacio vectorial de dimensin n m y
1.
2.
3.
A 0 , y A = 0 A = 0 ;
A = ||A para cualquier escalar (real) ; y
A + B A + B .
Por ejemplo:
a.
(a 2ij)
i
1/2
b.
norma de Frobenius: A F =
c.
Ax
x
= max i |a ij| ,
j
19/10/2013 17:08
10 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
norma_inf
norma_inf=M.norm(infinity)
print norma_inf
#otra forma
norma_inf=M.norm(oo)
norma_inf
xxa
x
a (y) = |y y a | = 1 10 6 y r (y) =
= 2.8339 10 6
yya
y
= 2.8339 10 6
Y con vectores
V=vector(RDF,4,[1,2,4,5])
show(V)
V_prima=vector(RDF,4,[V[i]+(0.5-random()) for i in [0 .. 3]])
show(V_prima)
deltav=V-V_prima;
deltav
#norma 2
error_2=deltav.norm(2)
print "error absoluto = " + error_2.str()
print "error realtivo = " +
((error_2/V.norm(2))*100).n(digits=4).str() +"%"
19/10/2013 17:08
11 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
#norma infinito
error_inf=deltav.norm(oo)
print "error absoluto = " + error_inf.str()
print "error realtivo = " +
((error_inf/V.norm(oo))*100).n(digits=4).str() +"%"
U=vector(RDF,4,[V[i]*1e-6 for i in [0 .. 3]])
U_prima=vector(RDF,4,[V_prima[i] *1e-6 for i in [0 .. 3]])
show(U)
show(U_prima)
deltau=U-U_prima;
show(deltau)
print "Norma 2"
#norma 2
error_2=deltau.norm(2)
print " error absoluto = " + error_2.str()
print " error relativo = " +
((error_2/U.norm(2))*100).n(digits=4).str() +"%"
print "Norma Infinito"
#norma infinito
error_inf=deltau.norm(oo)
print " error absoluto = " + error_inf.str()
print " error realtivo = " +
((error_inf/U.norm(oo))*100).n(digits=4).str() +"%"
19/10/2013 17:08
12 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
return 0;
A=matrix(10,10,lambda i,j: a(i,j))
show(A)
A=matrix(10,10,lambda i,j: i+1 if i==j else 2 if abs(i-j)==1 else 4
if abs(i-j)== 3 else 0)
show(A)
#Matriz de Hilbert
A=matrix(RR,10,10,lambda i,j: 1/(i+j+1));show(A)
A=matrix(QQ,10,10,lambda i,j: 1/(i+j+1));show(A)
A=matrix(QQbar,10,10,lambda i,j: 1/(i+j+1));show(A)
perturbado (
1
0.99999
2
x
3
)( )=(
) y el sistema
y
2
3.00001
x
2
3
)( ) =(
) que ocurre al resolverlos?
2
y
3.00001
1
1.00001
M=matrix(RR,2,2,[1,2,1.00001,2])
b=matrix(RR,2,1,[3,3.00001])
sol_1=M.solve_right(b)
sol_1
M=matrix(RR,2,2,[1,2,0.99999,2])
b=matrix(RR,2,1,[3,3.00001])
sol_2=M.solve_right(b)
sol_2
19/10/2013 17:08
13 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
2.-
Consideremos
perturbado (
3
4
el
sistema
3
4
4
x
7
)( )=( )
3
y
1
el
sistema
4
x
7
) ( ) = ( ) que ocurre al resolverlos?
y
2.9
1
M=matrix(RR,2,2,[3,4,4,-3])
b=matrix(RR,2,1,[7,1])
sol_1=M.solve_right(b)
sol_1
M=matrix(RR,2,2,[3,4,4,-2.9])
b=matrix(RR,2,1,[7,1.09757])
sol_2=M.solve_right(b)
sol_2
print "error absoluto = " + ((sol_1-sol_2).norm(oo)).str()
print "error relativo = " + (((sol_1sol_2).norm(oo)/sol_1.norm(oo))*100).str() + "%"
Observamos ahora que la diferencia es pequea, veamos ahora que el nmero de condicin
debe ser prximo a uno
condM=M.norm(oo)*M.inverse().norm(oo)
print "Nmero de Condicin = " + condM.str()
19/10/2013 17:08
14 de 14
http://sage.math.canterbury.ac.nz/home/albros/15/print
19/10/2013 17:08