Vectores y Matrices
Curso 2016-17 1Notaci´on
A = a11 a12 . . . a1n a21 a22 . . . a2n .. . ... . .. ... am1 am2 . . . amn , A = [aij] 11≤≤ij≤≤mn aij= elemento (i,j) de A.Tama˜no u orden de A= m ×n. Si m = n, A cuadrada.
i-sima fila y j-sima columna:
ai = [ai1 ai2 . . . ain] y aj = a1j a2j .. . amj , Notaci´on de MATLAB: A(i,:) y A(:,j).
En general aij ∈ R, anillo conmutativo con elemento 1.
Rm×n= cto de las matrices de tama˜no m× n con elementos en R.
Operaciones con matrices
Si A = [aij] ∈ Rm×n y B = [bij] ∈ Rm×n:
A+ B = [aij + bij] ∈ Rm×n.MATLAB:A+B
y si A = [aij] ∈ Rm×n y B = [bij] ∈ Rn×p, entonces
AB = " n X k=1 aikbkj # ∈ Rm×p.MATLAB:A*B
(Rn×n,+,·) es un anillo no conmutativo con identidad
In = 1 0 . . . 0 0 1 . . . 0 .. . ... . .. ... 0 0 . . . 1 = [δij],
matriz identidad de orden n.
MATLAB:eye(n)
Si x ∈ R y A = [aij] ∈ Rm×n, xA = [xaij] ∈ Rm×n. MATLAB: x.∗A o x∗A
3
Algunos tipos de matrices
Si A = [aij]
AT = [aji] transpuesta de A. MATLAB: A’ si R = R y A.’ si R = C.
Si R = C, A = [aij] conjugada de A. (z = x +iy ⇒ z = x − iy) Si R = C, A∗ = AT. MATLAB: A’ Sim´etricas: A = AT: 1 22 0 34 3 4 −1 , i 2i 2i 4 Herm´ıticas: A = A∗: 2i −3i 2 +5 i 2− i 5 1 Skew-sim´etricas: AT = −A: 0 1 + 2i −1− 2i 0 Skew-herm´ıticas: A∗ = −A.
M´as tipos de matrices
Diagonales: d1 0 · · · 0 0 d2 · · · 0 .. . ... . .. ... 0 0 · · · dn = Diag(d1, . . . ,dn) Matlab: diagDiagonal por bloques:
A1 0 . . . 0 0 A2 . . . 0 .. . ... . .. ... 0 0 . . . Ap . Matlab: blkdiag Triangular superior: a11 a12 · · · a1n 0 a22 · · · a2n .. . ... . .. ... 0 0 · · · ann . Matlab: triu
Triangular inferior, por bloques, etc.
Matlab: tril 5
Matrices de permutaci´on
P ∈ Rn×n matriz de permutaci´on si en cada fila y columna hay un
elemento igual a 1 y todos los dem´as son cero: Permutan filas y columnas.
P = 0 0 1 1 0 0 0 1 0 y PT = 0 1 0 0 0 1 1 0 0 P 1 2 3 = 3 1 2 y 1 2 3 PT = 3 1 2 . Si σ = (i1,i2, . . . ,in) ∈ Sn; i. e. σ(k) = ik, Pσ = [δiσ(j)]= matriz con un 1
en (σ(i),i) y todo lo dem´as cero . En el ejemplo, σ = (2,3,1).
PT = Pσ−1 = P−1
PσA = [aiσ(j)] y APT = [aσ(i)j]. Matlab: P=eye(n); P=P(:,σ)
Submatrices
Si A = [aij] ∈ Rm×n, B = [bij] ∈ Rp×q submatriz de A si existen ´ındices
(i1, . . . ,ip) y (j1, . . . ,jq) tales que 1≤ i1 < · · · < ip ≤ m, 1 ≤ j1 < · · · < jq ≤ n y airjs = brs, 1 ≤ r ≤ p,1≤ s ≤ q. A = r r r r r r r r r r r r i1 i2 i3 i4 j3 j2 j1 b11 b12 b13 b21 b22 b23 b31 b32 b33 b41 b42 b43
Usaremos notaci´on de MATLAB: si u = (i1, . . . ,ip) y v = (j1, . . . ,jq):
B=A(u,v)
Hay configuraciones que no son submatrices.
7
Matrices y aplicaciones lineales
R = F f : V1 → V2 Fijadas bases B1 = {v1, . . . ,vn}, B2 = {u1, . . . ,um} f (vj) = m X i=1 aijui, j = 1, . . . ,n
determina A = [aij] ∈ Fm×n: matriz de f respecto de B1 y B2. Y
rec´ıprocamente.
A ∈ Fm×n puede verse como una aplicaci´on lineal entre los espacios vectoriales Fn y Fm:
A : Fn → Fm
x ; Ax
La matriz de esta aplicaci´on lineal es A cuando en Fm y Fn se toman las bases can´onicas.
Rango y nulidad
A ∈ Fm×nIm(A) = {y ∈ Fm|y = Ax para alg´un x ∈ Fn}
Ker(A) = {x ∈ Fn|Ax = 0}
rang(A) = dimImA MATLAB: rank
ν(A)= nulidad de A= dimKerA
Primer teorema de isomorf´ıa
ImA ∼= F
n
KerA ⇒ ν(A) = n − rang(A). Desigualdad de Sylvester (1884)
A ∈ Fm×n, B ∈ Fn×p
rang(A) + rang(B)− n ≤ rang(AB) ≤ m´ın(rang(A),rang(B))
9
Matrices de rango completo
A tiene rango completo ⇔ rang(A) = m´ın{m,n}.
¿Qu´e significa que A es de rango completo?
m ≥ n ⇒ dimImA = n ⇔ KerA = {0} ⇔ A es inyectiva (invertible por la izquierda: BA = In).
m ≤ n ⇒ dimImA = m ⇔ ImA = Fm ⇔ A es suprayectiva
(invertible por la derecha: AB = Im).
m = n ⇒ A biyectiva (invertible: AB = BA = In).
Matrices invertibles
Para A ∈ Fn×n (F cuerpo) las siguientes condiciones son equivalentes:
1 A tiene inversa A−1. (MATLAB: inv) 2 rang(A) = n.
3 ImA = Fn. 4 ν(A) = 0. 5 KerA = {0}.
6 0 no es un valor propio (autovalor) de A. (MATLAB: eig) 7 det(A) 6= 0. (MATLAB: det)
A no singular: det(A) 6= 0.
A no singular ⇔ A invertible (F cuerpo)
λ ∈ C valor propio de A ∈ Fn×n:
∃x ∈ Cn (x 6= 0) tal que Ax = λx x= vector propio de A asociado a λ.
11
¿Qu´e significa
Ax
=
b
?
b es la imagen de x por Ab es el resultado de multiplicar A por x.
bi = n X j=1 aijxj, i = 1, . . . ,m. b1 b2 .. . bm = a11 a21 .. . am1 x1 + a12 a22 .. . am2 x2 +· · ·+ a1n a2n .. . amn xn. b = x1a1 +x2a2 + · · ·+xnan.
b es una combinaci´on lineal de las columnas de A cuyos coeficientes son las componentes del vector x.
b ∈ ImA ⇔ b =
n X
i=1
La Imagen y el
Span
Notaci´on: Si a1, . . . ,an ∈ Fn < a1, . . . ,an >= Span (a1, . . . ,an) = ( n X i=1 xiai|xi ∈ F ) .Im(A) es es subespacio de Fm generado por las columnas de A; i.e. , si
A = a1 a2 · · · an, ImA =< a1, . . . ,an >.
rang(A) = dim < a1, . . . ,an >
KerA est´a formado por los vectores cuyas componentes son los
coeficientes del vector 0 como combinaci´on lineal de las columnas de A: 0 = x1a1 +x2a2 + · · ·+xnan.
13
Producto de matrices
¿ Qu´e significa C = AB?
C es la matriz de la composici´on Rp −→B Rn −→A Rm C es el resultado de multiplicar A por B.
cij = ai1b1j +ai2b2j +· · ·+ainbnj, cj = c1j c2j .. . cmj = a11 a12 · · · a1n a21 a22 · · · a2n .. . ... . .. ... am1 am2 · · · amn b1j b2j .. . bnj = Abj, j = 1, . . . ,p. c1 c2 · · · cp = Ab1 Ab2 · · · Abp , ci = aiB.
A act´ua sobre B: combinaciones lineales en las filas de B, y
Producto interno y externo de vectores
Si u,v ∈ Rn: Producto interno: uTv uTv = u1 u2 · · ·un v1 v2 .. . vn = n X i=1 uivi ∈ R. producto externo: uvT ∈ Rn×n uvT = u1 u2 .. . un v1 v2 · · · vn = u1v u2v .. . unv = v1u v2u · · · vnu.uvT es una matriz de rango 1 y todas las matrices de rango 1 son as´ı.
15
Otra vez el producto de matrices
(AB)ij = n P k=1 aikbkj = aibj = (ai0)Tbj, a0i es la i-´esima columna de AT y bj la j-´esima columna de B. (akbk)ij = aikbkj AB = a1(b10)T +a2(b20)T +· · ·+ an(bn0)T,
Matrices elementales. Tipo I
i j ↓ ↓ E1(α) = In +Eij(α) = 1 . .. 1 . . . α . .. ... 1 . .. 1 ← i ← j E1(α)A = a1 .. . ai +αaj .. . aj .. . am , AE1(α) = a1 · · · ai · · · aj +αai · · · an. 17Matrices elementales. Tipo II
i ↓ E2(α) = 1 . .. 1 α 1 . .. 1 ← i E2(α)A = a1 .. . αai .. . am y AE2(α) = a1 · · · αai · · · an . 18Matrices elementales. Transposiciones
i j ↓ ↓ P = 1 . .. 1 0 . . . 1 .. . . .. ... 1 . . . 0 1 . .. 1 ← i ← j PA = a1 .. . aj .. . ai .. . am y AP =a1 · · · aj · · · ai · · · an. 19Eliminaci´on Gaussiana (EG)
Ax = b, A ∈ Fn×n,b ∈ Fn×1A b = A(1) b(1) =⇒ A(n) b(n) A(n) = U triangular superior.
n − 1 etapas y en cada etapa k = 1 : n − 1
A(k) = " A(11k) A(12k) 0 A(22k) # ,A(11k) ∈ F(k−1)×(k−1) triangular Objetivo en la etapa k: sustituir por ceros los elementos de
A(k)(k + 1 : n,k) restando a la fila i = k + 1 : n la fila k multiplicada por
mik = a(ikk)
a(kkk) (MULTIPLICADOR):
a(ijk+1) = a(ijk) − mikakk(k), i,j = k + 1, . . . ,n. bi(k+1) = bi(k) −mikbk(k), i,j = k + 1, . . . ,n.
Factorizaci´on
LU
Si mk = 0 · · · 0 mk+1k · · · mnk T y Mk = In − mkekT: MkA(k) = A(k+1) Entonces Mn−1Mn−2. . .M2M1A = A(n) = U triangular superior A = M1−1M2−1. . .Mn−−11U, Mk−1 = Im +mkek L := M1−1M2−1. . .Mn−−11 = 1 m21 1 .. . m32 . .. .. . ... . .. mn1 mn2 · · · mnn−1 1 A = LU Factorizaci´on LU de A 21Algortimo
LU
Factorizaci´on LU Dato: A ∈ Fn×nObjetivo: calcular factorizari´on LU de A.
• L =eye(n)
• for k = 1 : n− 1
• for i = k + 1 : n • lik = aik/akk • aik = 0
• for j = k + 1 : n • aij = aij − likakj • end for • end for • end for • U = A Coste: ∼ 23n3 22
Pivoteo parcial por columnas
al comienzo de la etapa k, se intercambian las filas k y r estando r determinada por la condici´on:
|a(rkk)| = m´ax k≤i≤n|a (k) ik | Mn−1Pn−1Mn−2Pn−2. . .M2P2M1P1A = U, U = M4P4M3P3M2P2M1P1A = M4 ·P4M3P4 | {z } M0 3 ·P4P3M2P3P4 | {z } M0 2 ·P4P3P2M1P2P3P4 | {z } M0 1 ·P4P3P2P1 | {z } P ·A = M4M30M20M10PA Mk0 = Pn−1Pn−2. . .Pk+1(In −mkekT)Pk+1. . .Pn−2Pn−1 = (In −Pn−1Pn−2. . .Pk+1mkekT = In −m0kekT, PA = LU, L= M10−1M20−1. . .Mn0−−12Mn−−11 = 1 m021 1 .. . m320 . .. .. . ... . .. m0n1 mn02 · · · mnn−1 1 23
Algoritmo Factorizaci´on
LU
con pivoteo. Versi´on 1
Factorizaci´on LU con pivoteo parcial. Versi´on 1Dato: A ∈ Fn×n, detA 6= 0
Objetivo: calcular P,L U tales que PA = LU.
• L =eye(n)
• for k = 1 : n− 1
• Calc´ulese el m´ax|A(k : n,k)| y su posici´on, r.
• Perm´utense las filas k y r de A, L y P
for i = k + 1 : n lik = aik/akk aik = 0 for j = k + 1 : n aij = aij − likakj end for end for end for U = A
Algoritmo Factorizaci´on
LU
(LUTXFOR). Versi´on 1
function [L,U,P] = lutxforv1(A)%LUTXFORV1, para una matriz invertible dada A, devuelve matrices L, %triangular inferior con 1 en la diagonal,
%U, triangular superior y P de permutaci´on
%tal que PA=LU. En vez trabajar con la matriz P se trabaja %con un vector p que almacena las transposiciones y a partir %del cual se construye P. La matriz L se define
%y actualiza de forma expl´ıcita y se utilizan bucles FOR para %todos los contadores.
% Calculamos el tama~no de A [n,n] = size(A); %Iniciamos p=[1 2 ... n]^T p = (1:n)’; % y L L=eye(n);
%Transformaciones elementales en las columnas 1, 2,..., n-1. for k = 1:n-1
%Calculamos el m´aximo, r, (en valor absoluto) de los elementos % A(k,k), A(k,k+1),...,A(k,n), y la posici´on, m, donde se encuentra [m,r] = max(abs(A(k:n,k)));
% Calculamos la posici´on del m´aximo en toda la columna k-´esima de A r = r+k-1;
%Si el m´aximo fuera cero es porque toda la columna es cero y A no ser´ıa %invertible
if (A(r,k) ~= 0)
%Si el m´aximo no est´a en la diagonal permutamos if (r ~= k)
%permutamos las filas k y r en A, L y p A([k r],k:n) = A([r k],k:n);
L([k r],1:k-1) = L([r k],1:k-1); p([k r]) = p([r k]);
end
25
Algoritmo Factorizaci´on
LU
(LUTXFOR). Versi´on 1. Cont.
%L est´a fromada por los multiplicadores for i = k+1:n;
L(i,k) = A(i,k)/A(k,k);
% Por debajo de (k,k) los elementos de A en la columna k ser´an 0 A(i,k)=0;
% Realizamos sobre las filas i=k+1,...,n la transformaci´on elemental for j = k+1:n;
A(i,j) = A(i,j) - L(i,k)*A(k,j); end
end end
%A se ha convertido en triangular superior: es la U U=A;
% Constru´ımos P a partir de p: las filas 1:n de P son las de la permutaci´on P=eye(n); P=P(p,:);
Algoritmo Factorizaci´on
LU
(LUTXFOR). Final
function [L,U,P] = lutxfor(A)[n,n] = size(A); p = (1:n)’; for k = 1:n-1 [m,r] = max(abs(A(k:n,k))); r = r+k-1; if (A(r,k) ~= 0) if (r ~= k) A([k r],:) = A([r k],:); p([k r]) = p([r k]); end for i = k+1:n; A(i,k)=A(i,k)/A(k,k); for j = k+1:n;
A(i,j) = A(i,j) - A(i,k)*A(k,j); end
end end end
%En la parte triangular inferior de A (sin la diagonal) est´a L L = tril(A,-1) + eye(n,n);
%Y en la parte triangular superior (incluyendo la diagonal) est´a U U = triu(A);
P=eye(n); P=P(p,:);
27
El algoritmo de la factorizaci´on
LU
function [L,U,p] = lutx(A)[n,n] = size(A); p = 1:n; for k = 1:n-1 [r,m] = max(abs(A(k:n,k))); m = m+k-1; if (A(m,k) ∼= 0) if (m ∼= k) A([k m],:) = A([m k],:); p([k m]) = p([m k]); end i = k+1:n; A(i,k) = A(i,k)/A(k,k); j = k+1:n;
A(i,j) = A(i,j) - A(i,k)*A(k,j); end
end
L = tril(A,-1) + eye(n,n); U = triu(A);
Jerarqu´ıa en la memoria de los ordenadores y BLAS
Registros Cach´e
RAM Disco Basic Linear Algebra Subroutines (BLAS)
Operaci´on Definici´on flops (f) memory ref. (m) Ratio q = f/m
BLAS1 y = αx +y 2n 3n + 1 2/3
BLAS2 y = Ax +y 2n2 n2 + 3n 2
BLAS3 C = AB +C 2n3 4n2 n/2
BLAS1 (saxpy): productos internos, escalar por vector, . . . BLAS2: matriz por vector, sistemas triangulares, A+ xyt,. . .
BLAS3: matriz por matriz, sistemas triangulares con muchos vectores independientes, . . .
f ·tarit + m·tmem = f ·tarit ·1 + mf tmem
tarit = f ·tarit ·1 + q1 tmem tarit 29 A ∈ Fm×n, B ∈ Fn×p
rangA+ rangB − n (1)< rang(AB) (2)< m´ın{rangA,rangB}.
(1) ⇔ rangA+ dim(KerA∩ImB) < n
(2) ⇔
rangB − dim(KerA∩ImB) < rangB ⇒ dim(KerA∩ImB) > 0
rangB < rangA+ dim(KerA∩ImB)
rangB < rangA+ dim(KerA∩ImB) < n , dim(KerA∩ImB) > 0
B = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 , A = 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 KerA∩ImB =< e1 >
Siendo m = n = p debe ser n ≥ 5.