import numpy as np
vector = np.array([1,2,3,4])
print('Vector:\n', vector)
matriz = np.array([[1,2,3],[4,5,6],[7,8,9]])
print('\nMatriz:\n', matriz)
tensor = np.array([
[[1,2,3], [7,8,9]],
[[1,2,3], [7,8,9]],
[[0,0,0], [255,255,255]],
[[0,0,0], [255,255,255]]
])
print('\nMatriz:\n', tensor)
Vector:
[1 2 3 4]
Matriz:
[[1 2 3]
[4 5 6]
[7 8 9]]
Matriz:
[[[ 1 2 3]
[ 7 8 9]]
[[ 1 2 3]
[ 7 8 9]]
[[ 0 0 0]
[255 255 255]]
[[ 0 0 0]
[255 255 255]]]
import matplotlib.pyplot as plt
plt.imshow(tensor, interpolation='nearest')
plt.show()
vector.shape
matriz.shape
tensor.shape
escalar = 5.679
vector = np.array([3,4,5,6])
matriz = np.array([[1,2],[3,4],[5,6]])
tensor = np.array([
[[1,2,3,30],[4,5,6,31],[7,8,9,32]],
[[11,12,13,33],[14,15,16,34],[17,18,19,35]],
[[21,22,23,36],[24,25,26,37],[27,28,29,38]],
[[21,22,23,36],[24,25,26,37],[27,28,29,38]],
[[21,22,23,36],[24,25,26,37],[27,28,29,38]]
])
matriz_T = matriz.T
print(matriz_T)
print(matriz.shape)
print(matriz_T.shape)
[[1 3 5]
[2 4 6]]
(3, 2)
(2, 3)
tensor_T = tensor.T
print(tensor_T)
print(tensor.shape)
print(tensor_T.shape)
[[[ 1 11 21 21 21]
[ 4 14 24 24 24]
[ 7 17 27 27 27]]
[[ 2 12 22 22 22]
[ 5 15 25 25 25]
[ 8 18 28 28 28]]
[[ 3 13 23 23 23]
[ 6 16 26 26 26]
[ 9 19 29 29 29]]
[[30 33 36 36 36]
[31 34 37 37 37]
[32 35 38 38 38]]]
(5, 3, 4)
(4, 3, 5)
matriz = np.array([[1,2],[3,4]])
matriz_T = matriz.T
suma = matriz + matriz_T
suma
# Ejercicio
vector5 = np.arange(5)
matriz5x5 = np.arange(30).reshape(6,5)
vector5 + matriz5x5
escalar = 5.679
vector = np.array([3,4])
matriz = np.array([[1,2],[3,4],[5,6]])
tensor = np.array([
[[1,2,3,30],[4,5,6,31],[7,8,9,32]],
[[11,12,13,33],[14,15,16,34],[17,18,19,35]],
[[21,22,23,36],[24,25,26,37],[27,28,29,38]],
[[21,22,23,36],[24,25,26,37],[27,28,29,38]],
[[21,22,23,36],[24,25,26,37],[27,28,29,38]]
])
A = matriz * vector
B = matriz.dot(vector) #esto saca el producto interno
print(A)
print(B)
[[ 3 8]
[ 9 16]
[15 24]]
[11 25 39]
matriz.dot(vector)
np.dot(matriz, vector)
matriz@vector
np.dot(vector, matriz.T) #la transpongo para que tengan las mismas dimensiones
A = np.arange(2, 26, 2).reshape(4,3)
B = np.arange(6).reshape(3,2)
print(A)
print(B)
print(B.T)
[[ 2 4 6]
[ 8 10 12]
[14 16 18]
[20 22 24]]
[[0 1]
[2 3]
[4 5]]
[[0 2 4]
[1 3 5]]
C = np.dot(A,B)
C
D = np.dot(B,A)
D
ValueError: shapes (3,2) and (4,3) not aligned: 2 (dim 1) != 4 (dim 0)
A = np.arange(6).reshape(3,2)
B = np.arange(1,5).reshape(2,2)
C = np.arange(5,9).reshape(2,2)
# Comprobacion asociativa
A_BC = np.dot(A,B@C)
AB_C = np.dot(A@B,C)
print(A_BC == AB_C)
[[ True True]
[ True True]
[ True True]]
# Comprobacion distributiva
D = np.dot(A,B+C)
E = np.dot(A,B) + np.dot(A,C)
print(D == E)
[[ True True]
[ True True]
[ True True]]
# Comprobacion NO es conmutativa
F = np.dot(B,C)
G = np.dot(C,B)
print(F == G)
[[False False]
[False False]]
A = np.arange(6).reshape(3,2)
B = np.arange(1,5).reshape(2,2)
AB_T = A.dot(B)
B_T_A_T = B.T.dot(A.T)
print(AB_T == B_T_A_T)
False
/shared-libs/python3.7/py-core/lib/python3.7/site-packages/ipykernel_launcher.py:3: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
This is separate from the ipykernel package so we can avoid doing imports until
import matplotlib.pyplot as plt
x = np.arange(-5,6)
x
y_1 = 3*x + 5
y_2 = 2*x + 3
plt.figure()
plt.plot(x, y_1)
plt.plot(x, y_2)
plt.xlim(-5,5)
plt.ylim(-5,5)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
A = np.array([[-3,1],[-2,1]])
print(A)
[[-3 1]
[-2 1]]
b = np.array([[5],[3]])
b
sol_1 = np.array([-2,-1])
sol_1
print(A.dot(sol_1))
[5 3]
'''
Otra forma de resolver ecuaciones usando la librería numpy: (esto se verá más adelante)
3*x + y = 1
1x + 2y = 0
'''
ig = np.array([1,0])
coe = np.array([[3,1],[1,2]])
solv = np.linalg.solve(coe, ig)
x = solv[0]
y = solv[1]
print(x, y)
0.39999999999999997 -0.19999999999999998
identidad = np.eye(4)
print(identidad)
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
vector = np.arange(4).reshape(4,1)
print(identidad.dot(vector))
[[0.]
[1.]
[2.]
[3.]]
A = np.array([[1,0,1],[0,1,1],[-1,1,1]])
A
inversa_A = np.linalg.inv(A)
inversa_A
print(A.dot(inversa_A))
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
np.set_printoptions(suppress=True)
A = np.array([[-3,1],[-2,1]])
A
b = np.array([[5],[3]])
b
inversa_A = np.linalg.inv(A)
inversa_A
x = inversa_A.dot(b)
x
print(A.dot(x))
[[5.]
[3.]]
x = np.arange(-6,6)
y_1 = 3*x+5
y_2 = -x+3
y_3 = 2*x+1
plt.figure()
plt.plot(x, y_1)
plt.plot(x, y_2)
plt.plot(x, y_3)
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
x = np.arange(-6,6)
y_2 = -x+3
y_3 = 2*x+1
plt.figure()
plt.plot(x, y_2)
plt.plot(x, y_3)
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
x = np.arange(-6,6)
y_3 = 2*x+1
plt.figure()
plt.plot(x, y_3)
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
v1 = np.array([2,5])
v2 = np.array([3,2])
%run {'graficarVectores.ipynb'}
graficarVectores([v1,v2], ['orange', 'blue'])
plt.xlim(-1,8)
plt.ylim(-1,8)
v1 = np.array([2,5])
v2 = np.array([3,2])
v1v2 = 2*v1 + 1*v2
v1v2
graficarVectores([v1, v2, v1v2], ['orange', 'blue', 'red'])
plt.xlim(-1,8)
plt.ylim(-1,12)
for a in range(-10, 10):
for b in range(-10,10):
plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a + v2[1]*b, marker = '.', color='orange')
plt.xlim(-100,100)
plt.ylim(-100,100)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
v1 = np.array([1,1])
v2 = np.array([-1,-1])
for a in range(-10, 10):
for b in range(-10,10):
plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a + v2[1]*b, marker = '.', color='orange')
plt.xlim(-25,25)
plt.ylim(-25,25)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
v1 = np.array([1,0])
v2 = np.array([2,-3])
for a in range(-10, 10):
for b in range(-10,10):
plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a + v2[1]*b, marker = '.', color='orange')
plt.xlim(-25,25)
plt.ylim(-25,25)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
from mpl_toolkits.mplot3d import Axes3D
v1 = np.array([1, 0, 0])
v2 = np.array([2, -3, 0])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for a in range(-10, 10):
for b in range(-10,10):
ax.scatter(v1[0]*a + v2[0]*b, v1[1]*a + v2[1]*b, v1[2]*a + v2[2]*b, marker='.', color='orange')
ax.set_xlabel('Eje X')
ax.set_ylabel('Eje Y')
ax.set_zlabel('Eje Z')
plt.show()
v1 = np.array([1,1])
v2 = np.array([-1,-1])
for a in range(-10,10):
for b in range(-10,10):
plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a + v2[1]*b,
marker='.', color='orange')
plt.xlim(-25,25)
plt.ylim(-25,25)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
A = np.array(
[[0,1,0,0],
[0,0,1,0],
[0,1,1,0],
[1,0,0,1]]
)
lambdas, V = np.linalg.eig(A.T)
print(A[lambdas == 0, :])
[[0 1 1 0]]
np.linalg.inv(A)
LinAlgError: Singular matrix
v1 = np.array([2,7])
v2 = np.array([3,5])
v1v2 = v1+v2
print(v1v2)
[ 5 12]
np.linalg.norm(v1v2)
(5**2 + 12**2)**0.5
norma_v1 = np.linalg.norm(v1)
norma_v2 = np.linalg.norm(v2)
norma_v1v2 = np.linalg.norm(v1v2)
print(norma_v1v2 <= (norma_v1 + norma_v2))
True
print(norma_v1 + norma_v2)
13.11106178412582
v1 = np.array([0,0,2,7])
v2 = np.array([0,0,3,5])
v1_aux = np.array([v1[2],v1[3],v2[2],v2[3]])
v1v2 = np.array([0,0,5,12])
import seaborn as sns
plt.quiver([v1[0], v1_aux[0], v1v2[0]],
[v1[1], v1_aux[1], v1v2[1]],
[v1[2], v1_aux[2], v1v2[2]],
[v1[3], v1_aux[3], v1v2[3]],
angles='xy', scale_units='xy', scale=1,
color=sns.color_palette())
plt.xlim(-0.5,6)
plt.ylim(-0.5,15)
vector = np.array([1,2,0,5,6,0])
np.linalg.norm(vector, ord=0) #devuelve el # de valores distintos de 0
vector = np.array([1,-1,1,-1,1])
np.linalg.norm(vector, ord=1) #norma 1 - suma de los valores absolutos
vector = np.array([1,1])
np.linalg.norm(vector, ord=2) #norma 2 (por defecto) - magnitud del vector
vector = np.array([1,2,3,4,5,6])
print(np.linalg.norm(vector, ord=2)**2) #norma 2^2
print(vector.T.dot(vector))
91.0
91
vector = np.array([1,2,3,100])
print(np.linalg.norm(vector, ord=np.inf)) #norma infinito - devuelve el max de los valores absolutos
100.0
v1 = np.array([0,0,0,3])
v2 = np.array([0,0,3,3])
plt.xlim(-2,6)
plt.ylim(-2,6)
plt.quiver([v1[0],v2[0]],
[v1[1],v2[1]],
[v1[2],v2[2]],
[v1[3],v2[3]],
angles='xy', scale_units='xy', scale=1,
color=sns.color_palette()
)
plt.show()
v1 = np.array([0,3])
v2 = np.array([3,3])
v1.T@v2
norma_v1 = np.linalg.norm(v1)
norma_v2 = np.linalg.norm(v2)
norma_v1 * norma_v2 * np.cos(np.deg2rad(45)) #se debe pasar a radianes para usar trigonometricas en numpy
vector = np.array([1,2,3,4,5])
matriz = np.diag(vector)
matriz
matriz[0:4, 0:3]
A = np.diag([2,3,4,5])
A
v1 = np.array([[1,1,1,1]])
A.dot(v1.T)
A_inv = np.diag([1/2,1/3,1/4,1/5])
A_inv
identidad = A.dot(A_inv)
identidad
A_inv_calc = np.linalg.inv(A)
A_inv_calc
A
A.T
simetrica = np.array([[1,2,3],
[2,-1,7],
[3,7,11]])
simetrica
simetrica.T
x = np.array([0,0,2,2])
y = np.array([0,0,2,-2])
plt.quiver([x[0], y[0]],
[x[1], y[1]],
[x[2], y[2]],
[x[3], y[3]],
angles='xy', scale_units='xy', scale=1
)
plt.xlim(-2,4)
plt.ylim(-3,3)
v1 = np.array([2,2])
v2 = np.array([2,-2])
v1@v2.T
# No son ortonormales
print(np.linalg.norm(v1))
print(np.linalg.norm(v2))
2.8284271247461903
2.8284271247461903
v1 = np.array([1,0])
v2 = np.array([0,-1])
v1.dot(v2.T)
# Son ortonormales
print(np.linalg.norm(v1))
print(np.linalg.norm(v2))
1.0
1.0
np.set_printoptions(suppress=True)
matriz = np.array([[1,0,0],
[0,1,0],
[0,0,1]])
matriz
matriz = np.diag([1,1,1])
matriz
matriz = np.eye(3, dtype=int)
matriz
# Si todas dan 0, es que todas sus columnas son ortogonales
print(matriz[:,0].dot(matriz[:,1]))
print(matriz[:,0].dot(matriz[:,2]))
print(matriz[:,1].dot(matriz[:,2]))
0
0
0
# Si todas dan 1, es que todas sus columnas son ortonormales
print(np.linalg.norm(matriz[:,0]))
print(np.linalg.norm(matriz[:,1]))
print(np.linalg.norm(matriz[:,2]))
1.0
1.0
1.0
# Si todas dan 0, es que todas sus filas son ortogonales
print(matriz[0,:].dot(matriz[1,:]))
print(matriz[0,:].dot(matriz[2,:]))
print(matriz[1,:].dot(matriz[2,:]))
0
0
0
# Si todas dan 1, es que todas sus filas son ortonormales
print(np.linalg.norm(matriz[0,:]))
print(np.linalg.norm(matriz[1,:]))
print(np.linalg.norm(matriz[2,:]))
1.0
1.0
1.0
A = np.array([[np.cos(100), -np.sin(100)],
[np.sin(100), np.cos(100)]])
A
print(np.linalg.norm(A[0,:]))
print(np.linalg.norm(A[1,:]))
print(np.linalg.norm(A[0,:]))
print(np.linalg.norm(A[1,:]))
0.9999999999999999
0.9999999999999999
0.9999999999999999
0.9999999999999999
print(A[0,:].dot(A[1,:]))
print(A[:,0].dot(A[:,1]))
7.937715190675968e-18
-7.937715190675968e-18
A_t = A.T
print(A_t.dot(A))
print(A.dot(A_t))
[[ 1. -0.]
[-0. 1.]]
[[1. 0.]
[0. 1.]]
A_inv = np.linalg.inv(A)
A_inv
A_t
print(1/A_t.dot(A))
[[ 1.00000000e+00 -1.25980837e+17]
[-1.25980837e+17 1.00000000e+00]]
matriz = np.arange(1,19,2).reshape(3,3)
matriz
traza = np.trace(matriz)
traza
v1 = np.array([0,1])
v2 = np.array([1,0])
graficarVectores([v1,v2], ['blue', 'red'])
plt.xlim(-0.25, 2)
plt.ylim(-0.25, 2)
plt.show()
A = np.array([[2,0],[0,2]])
print(A)
[[2 0]
[0 2]]
v1_transformado = A.dot(v1)
v2_transformado = A.dot(v2)
graficarVectores([v1_transformado,v2_transformado], ['orange', 'green'])
plt.xlim(-0.25, 2)
plt.ylim(-0.25, 2)
plt.show()
det_A = np.linalg.det(A)
print(det_A)
4.0
area_transformada = abs(v1_transformado[0]-v2_transformado[0]) * abs(v1_transformado[1]-v2_transformado[1])
print(area_transformada)
4
#Ahora con un determinante negativo
B = A * [-1,1]
print(B)
[[-2 0]
[ 0 2]]
det_B = np.linalg.det(B)
print(det_B)
-4.0
v1 = np.array([0,1])
v2 = np.array([1,0])
v1_transformado = B.dot(v1)
v2_transformado = B.dot(v2)
graficarVectores([v1_transformado, v2_transformado, v1, v2],
['blue', 'red', 'orange', 'green'])
plt.xlim(-3, 1.5)
plt.ylim(-0.5, 2.5)
plt.show()