Sesión 05.ipynb - Colaboratory
Sesión 05.ipynb - Colaboratory
Sesión 05.ipynb - Colaboratory
ipynb - Colaboratory
Matrices
import numpy as np
A=np.array([1,2,3,5-1,])
type(A)
numpy.ndarray
A[0]
A[[1,2]]
array([2, 3])
Manejo de indices
A.dtype
dtype('int64')
L=list(A)
L[0]
L[[1,3]]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-2c640c2d1580> in <module>
----> 1 L[[1,3]]
L[1:3]
[2, 3]
A[A>0]
array([1, 2, 3, 4])
A[-1]
A[-2]
A>0
np.logical_and(A>0,A<3)
A[np.logical_and(A>0,A<3)]
array([1, 2])
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 1/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
(A>0).all()
True
(A>0).any()
True
Redimensionamiento
A=np.array([1,2,3,-2])
A=A.reshape((2,2))
print(A)
[[ 1 2]
[ 3 -2]]
A=A.reshape((2,2),order='F')
print(A)
[[ 1 2]
[ 3 -2]]
A.ndim
A.shape
(2, 2)
A.size
B=np.array([2,3,4,5])
B.shape
(4,)
B=np.array([[2],[3],[4],[5]])
B.shape
(4, 1)
B=np.array([[2,3,4,5]])
B.shape
(1, 4)
B=np.array([[1,3],[2,1]])
print(B)
[[1 3]
[2 1]]
A+B
array([[ 2, 5],
[ 5, -1]])
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 2/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
A*B
array([[ 1, 6],
[ 6, -2]])
A/B
array([[ 1. , 0.66666667],
[ 1.5 , -2. ]])
B**2
array([[1, 9],
[4, 1]])
np.exp(A)
np.sin(A)
np.sqrt(np.abs(A))
array([[1. , 1.41421356],
[1.73205081, 1.41421356]])
C=np.array([1,3])
C[:,None]
array([[1],
[3]])
Inner y dot
a=np.array([1,2])
b=np.array([3,4])
print(np.dot(a,b))
11
print(np.inner(a,b))
11
a=np.array([[1,2],[3,4]])
b=np.array([[11,12],[13,14]])
print(np.dot(a,b))
[[37 40]
[85 92]]
print(np.inner(a,b))
[[35 41]
[81 95]]
print(np.inner(a,b.T))
[[37 40]
[85 92]]
print(a@b)
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 3/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
[[37 40]
[85 92]]
array([[ 1, 2],
[ 3, -2]])
traza=np.trace(A)
print(traza)
-1
norma=np.linalg.norm(A)
print(norma)
4.242640687119285
det=np.linalg.det(A)
print(det)
-8.000000000000002
Normas matriciales
norma1: max(sum(abs(x),axis=0))
print(np.linalg.norm(A,1))
4.0
print(np.linalg.norm(A,2))
3.6225827286091983
print(np.linalg.norm(A,np.inf))
5.0
Ejercicio
Verifique el teorema de Cayley Hamilton para una matriz, es decir, si p(x) , entonces p(A)
= det(A − xI ) = 0 .
trace_A=np.trace(A)
det_A=np.linalg.det(A)
I=np.eye(2)
A@A-trace_A*A+det_A*I
array([[-1.77635684e-15, 0.00000000e+00],
[ 0.00000000e+00, -1.77635684e-15]])
Valores propios
print(eigen_val)
[ 2.37228132 -3.37228132]
print(eigen_vec)
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 4/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
[[ 0.82456484 -0.41597356]
[ 0.56576746 0.90937671]]
Comprobamos que Av = λv
A@eigen_vec[:,0]
array([1.95609977, 1.34215959])
eigen_val[0]*eigen_vec[:,0]
array([1.95609977, 1.34215959])
A@eigen_vec[:,0]-eigen_val[0]*eigen_vec[:,0]
array([2.22044605e-16, 0.00000000e+00])
np.diag(eigen_val)@eigen_vec
Ejemplo
Diagonalizar la matriz
A=
1 2 4
⎡ ⎤
⎢2 1 −4 ⎥
⎣ ⎦
0 0 3
A=np.array([[1,2,4],[2,1,-4],[0,0,3]])
eigen_val,eigen_vec=eig(A)
print([email protected](eigen_val)@np.linalg.inv(eigen_vec))
[[ 1. 2. 4.]
[ 2. 1. -4.]
[ 0. 0. 3.]]
print(eigen_vec)
print(np.diag(eigen_val))
[[ 3. 0. 0.]
[ 0. -1. 0.]
[ 0. 0. 3.]]
Ejemplo
Resuelva el sistema
x + 2y = 10
3x + 4y = 20
A=np.array([[1,2],[3,4]])
b=np.array([10,20])
x=solve(A,b)
print(x)
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 5/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
[0. 5.]
Ejercicio
Implemente las transformaciones rigidas como funciones que acepten un array (x, y, 1) y devuelva un (x′ , y ′ , 1) .
1. Traslación
1 0 h1 x
⎡ ⎤⎡ ⎤
Th (x, y) = ⎢ 0 1 h2 ⎥ ⎢ y ⎥
⎣ ⎦⎣ ⎦
0 0 1 1
def T(x,h):
h1=h[0]
h2=h[1]
matT=np.array([[1,0,h1],[0,1,h2],[0,0,1]])
z=matT@x
return z
x=np.array([2,1,1])
h1=np.array([1,3,1])
h2=np.array([2,2,1])
print(T(x,h1))
[3 4 1]
def mT(h):
h1=h[0]
h2=h[1]
matT=np.array([[1,0,h1],[0,1,h2],[0,0,1]])
return matT
mT(h1)@x
array([3, 4, 1])
mT(h2)@x
array([4, 3, 1])
mT(h1)@mT(h2)@x
array([5, 6, 1])
mT(h1+h2)@x
array([5, 6, 1])
2. Rotación
cos θ − sin θ 0 x
⎡ ⎤⎡ ⎤
Rθ (x, y) = ⎢ sin θ cos θ 0⎥⎢y ⎥
⎣ ⎦⎣ ⎦
0 0 1 1
def rotacion(x,theta):
ct=np.cos(theta)
st=np.sin(theta)
matR=np.array([[ct,-st,0],[st,ct,0],[0,0,1]])
z=matR@x
return z
x=np.array([2,1,1])
theta=np.pi/2
print(rotacion(x,theta))
[-1. 2. 1.]
def mR(theta):
ct=np.cos(theta)
st=np.sin(theta)
matR=np.array([[ct,-st,0],[st,ct,0],[0,0,1]])
return matR
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 6/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
mR(np.pi/2)@x
mR(np.pi/3)@x
array([0.1339746 , 2.23205081, 1. ])
mR(np.pi/3+np.pi/2)@x
array([-2.23205081, 0.1339746 , 1. ])
Ejemplo
Pruebe girar un cuadrado de vértices A(1, 1), B(3, 1), C (3, 3) y D(1, 3) alrededor de un eje perpendicular al plano que pase por P (−2, 2).
figura=np.array([[1,3,3,1,1],[1,1,3,3,1],[1,1,1,1,1]])
print(figura)
[[1 3 3 1 1]
[1 1 3 3 1]
[1 1 1 1 1]]
h=np.array([-2,2,1])
theta=np.pi/3
T1=mT(-h)
R1=mR(theta)
figura1=mT(h)@mR(theta)@mT(-h)@figura
T1
array([[ 1, 0, 2],
[ 0, 1, -2],
[ 0, 0, 1]])
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 7/8
16/12/22, 12:59 Sesión 05.ipynb - Colaboratory
https://colab.research.google.com/drive/1xl7LnNhPch3VVLfIzr7OZFRDvthf1zrU#scrollTo=uJ5h2rNFEAmo&printMode=true 8/8