Algebra Lineal¶
NumPy es una biblioteca popular en Python para el álgebra lineal, que es una rama de las matemáticas que se enfoca en el estudio de vectores, matrices y sistemas de ecuaciones lineales. NumPy proporciona una gran cantidad de funciones y métodos para realizar operaciones de álgebra lineal de manera eficiente en Python.
Aquí hay algunos ejemplos de operaciones de álgebra lineal que se pueden realizar con NumPy:
- Vectores/Matrices especializadas
In [1]:
Copied!
import numpy as np
import numpy as np
In [2]:
Copied!
# Arreglo de ceros: np.zeros(shape)
print("Zeros:")
print( np.zeros((3,3)) )
# Arreglo de ceros: np.zeros(shape)
print("Zeros:")
print( np.zeros((3,3)) )
Zeros: [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
In [3]:
Copied!
# Arreglos de uno: np.ones(shape)
print("\nOnes:")
print( np.ones((3,3)) )
# Arreglos de uno: np.ones(shape)
print("\nOnes:")
print( np.ones((3,3)) )
Ones: [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]
In [4]:
Copied!
# Arreglo vacio: np.empty(shape)
print("\nEmpty:")
print( np.empty([2, 2]) )
# Arreglo vacio: np.empty(shape)
print("\nEmpty:")
print( np.empty([2, 2]) )
Empty: [[0.00000000e+000 1.07814025e-311] [1.07814025e-311 1.07814025e-311]]
In [5]:
Copied!
# Rango de valores: np.range(start, stop, step)
print("\nRange:")
np.arange(0., 10., 1.)
# Rango de valores: np.range(start, stop, step)
print("\nRange:")
np.arange(0., 10., 1.)
Range:
Out[5]:
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
In [6]:
Copied!
# Grilla de valores: np.linspace(start, end, n_values)
print("\nRegular grid:")
print( np.linspace(0., 1., 9) )
# Grilla de valores: np.linspace(start, end, n_values)
print("\nRegular grid:")
print( np.linspace(0., 1., 9) )
Regular grid: [0. 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1. ]
In [7]:
Copied!
# fijar semilla
np.random.seed(42)
# Sequencia de valores aleatorios: np.random
print("\nRandom sequences:")
print( np.random.uniform(10, size=6) )
# fijar semilla
np.random.seed(42)
# Sequencia de valores aleatorios: np.random
print("\nRandom sequences:")
print( np.random.uniform(10, size=6) )
Random sequences: [6.62913893 1.44357124 3.41205452 4.61207364 8.59583224 8.59604932]
- Operaciones con Matrices
In [8]:
Copied!
# crear matrices
A = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
B = np.array([
[9,8,7],
[6,5,4],
[3,2,1]]
)
print(f"Matrix A: \n {A}\n")
print(f"Matrix B: \n {B}")
# crear matrices
A = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
B = np.array([
[9,8,7],
[6,5,4],
[3,2,1]]
)
print(f"Matrix A: \n {A}\n")
print(f"Matrix B: \n {B}")
Matrix A: [[1 2 3] [4 5 6] [7 8 9]] Matrix B: [[9 8 7] [6 5 4] [3 2 1]]
In [9]:
Copied!
# sumar dos matrices
print("Sum:")
print( A+B )
# sumar dos matrices
print("Sum:")
print( A+B )
Sum: [[10 10 10] [10 10 10] [10 10 10]]
In [10]:
Copied!
# restar dos matrices
print("\nSubtraction")
print( A-B )
# restar dos matrices
print("\nSubtraction")
print( A-B )
Subtraction [[-8 -6 -4] [-2 0 2] [ 4 6 8]]
In [11]:
Copied!
# producto uno a uno
print("\nProduct")
print( A*B )
# producto uno a uno
print("\nProduct")
print( A*B )
Product [[ 9 16 21] [24 25 24] [21 16 9]]
In [12]:
Copied!
# producto matricial
print("\nMatricial Product")
print( np.dot(A,B) )
# producto matricial
print("\nMatricial Product")
print( np.dot(A,B) )
Matricial Product [[ 30 24 18] [ 84 69 54] [138 114 90]]
In [13]:
Copied!
# potencia
print("\n Power")
print( A**2 )
# potencia
print("\n Power")
print( A**2 )
Power [[ 1 4 9] [16 25 36] [49 64 81]]
- Algunas funciones especiales:
In [14]:
Copied!
# crear matriz
A = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
# crear matriz
A = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
In [15]:
Copied!
print("funcion exponencial")
print( np.exp(A) )
print("funcion exponencial")
print( np.exp(A) )
funcion exponencial [[2.71828183e+00 7.38905610e+00 2.00855369e+01] [5.45981500e+01 1.48413159e+02 4.03428793e+02] [1.09663316e+03 2.98095799e+03 8.10308393e+03]]
In [16]:
Copied!
print("funcion seno")
print( np.sin(A) )
print("funcion seno")
print( np.sin(A) )
funcion seno [[ 0.84147098 0.90929743 0.14112001] [-0.7568025 -0.95892427 -0.2794155 ] [ 0.6569866 0.98935825 0.41211849]]
In [17]:
Copied!
print("funcion coseno")
print( np.cos(A))
print("funcion coseno")
print( np.cos(A))
funcion coseno [[ 0.54030231 -0.41614684 -0.9899925 ] [-0.65364362 0.28366219 0.96017029] [ 0.75390225 -0.14550003 -0.91113026]]
In [18]:
Copied!
print("funcion tangente")
print( np.tan(A) )
print("funcion tangente")
print( np.tan(A) )
funcion tangente [[ 1.55740772 -2.18503986 -0.14254654] [ 1.15782128 -3.38051501 -0.29100619] [ 0.87144798 -6.79971146 -0.45231566]]
- Operaciones álgebra líneal:
In [19]:
Copied!
# crear matriz
A = np.array([[1,2],
[3,4]])
# crear matriz
A = np.array([[1,2],
[3,4]])
In [20]:
Copied!
# matriz transpuesta
print("Transpose: ")
print( A.T )
# matriz transpuesta
print("Transpose: ")
print( A.T )
Transpose: [[1 3] [2 4]]
In [21]:
Copied!
# determinante
print("determinant")
print( round(np.linalg.det(A) ,2))
# determinante
print("determinant")
print( round(np.linalg.det(A) ,2))
determinant -2.0
In [22]:
Copied!
# Matriz Inversa
print("Inverse")
print( np.linalg.inv(A) )
# Matriz Inversa
print("Inverse")
print( np.linalg.inv(A) )
Inverse [[-2. 1. ] [ 1.5 -0.5]]
In [23]:
Copied!
# traza
print("Trace")
print( np.trace(A))
# traza
print("Trace")
print( np.trace(A))
Trace 5
In [24]:
Copied!
# Valores y vectores propios
eigenvalues, eigenvectors = np.linalg.eig(A)
print("eigenvalues")
print( eigenvalues )
print("\neigenvectors")
print( eigenvectors )
# Valores y vectores propios
eigenvalues, eigenvectors = np.linalg.eig(A)
print("eigenvalues")
print( eigenvalues )
print("\neigenvectors")
print( eigenvectors )
eigenvalues [-0.37228132 5.37228132] eigenvectors [[-0.82456484 -0.41597356] [ 0.56576746 -0.90937671]]
In [25]:
Copied!
# descomposicion QR
Q,R = np.linalg.qr(A)
print("QR decomposition:")
print("\nQ")
print( Q )
print("\nR")
print( R )
# descomposicion QR
Q,R = np.linalg.qr(A)
print("QR decomposition:")
print("\nQ")
print( Q )
print("\nR")
print( R )
QR decomposition: Q [[-0.31622777 -0.9486833 ] [-0.9486833 0.31622777]] R [[-3.16227766 -4.42718872] [ 0. -0.63245553]]
- Resolver sitema de ecuaciones:
In [26]:
Copied!
# crear matriz
A = np.array([[1,2],
[3,4]])
# sistemas lineales: Ax = b
b = np.array([[5.], [7.]])
# crear matriz
A = np.array([[1,2],
[3,4]])
# sistemas lineales: Ax = b
b = np.array([[5.], [7.]])
In [27]:
Copied!
print("linear system: Ax=b")
print("\nx = ")
print( np.linalg.solve(A, b) )
print("linear system: Ax=b")
print("\nx = ")
print( np.linalg.solve(A, b) )
linear system: Ax=b x = [[-3.] [ 4.]]