In [4]:
import numpy as np
In [5]:
x = [1,2,3]
y = [4,5,6]
x + y
Out[5]:
In [6]:
B = np.array([[1,2,3], [4,5,6]]) # habiendo corrido import numpy as np
In [7]:
B + 2*B # Python sabe sumar y multiplicar arrays como algebra lineal
Out[7]:
In [8]:
np.matmul(B.transpose(), B) # B^t*B
Out[8]:
In [9]:
B[1,1]
Out[9]:
In [10]:
B[1,:]
Out[10]:
In [11]:
B[:,2]
Out[11]:
In [12]:
B[0:2,0:2]
Out[12]:
In [13]:
B.shape
Out[13]:
In [14]:
vec = np.array([1,2,3])
print(vec)
In [62]:
class Array:
"Una clase minima para algebra lineal"
def __init__(self, list_of_rows):
"Constructor"
self.data = list_of_rows
self.shape = (len(list_of_rows), len(list_of_rows[0]))
In [63]:
A = Array([[1,2,3], [4,5,6]])
A.__dict__ # el campo escondido __dict__ permite acceder a las propiedades de clase de un objeto
Out[63]:
In [64]:
A.data
Out[64]:
In [65]:
A.shape
Out[65]:
In [1]:
class Array:
"Una clase minima para algebra lineal"
def __init__(self, list_of_rows):
"Constructor y validador"
# obtener dimensiones
self.data = list_of_rows
nrow = len(list_of_rows)
# ___caso vector: redimensionar correctamente
if not isinstance(list_of_rows[0], list):
nrow = 1
self.data = [[x] for x in list_of_rows]
# ahora las columnas deben estar bien aunque sea un vector
ncol = len(self.data[0])
self.shape = (nrow, ncol)
# validar tamano correcto de filas
if any([len(r) != ncol for r in self.data]):
raise Exception("Las filas deben ser del mismo tamano")
def __repr__(self):
return "\n".join(str(x) for x in self.data)
def __str__(self):
return "\n".join(repr(x) for x in self.data)
In [2]:
A = (Array([[1,2,3], [4,5,6]]))
In [5]:
print(A)
In [6]:
A
Out[6]:
In [7]:
class Array:
"Una clase minima para algebra lineal"
def __init__(self, list_of_rows):
"Constructor y validador"
# obtener dimensiones
self.data = list_of_rows
nrow = len(list_of_rows)
# ___caso vector: redimensionar correctamente
if not isinstance(list_of_rows[0], list):
nrow = 1
self.data = [[x] for x in list_of_rows]
# ahora las columnas deben estar bien aunque sea un vector
ncol = len(self.data[0])
self.shape = (nrow, ncol)
# validar tamano correcto de filas
if any([len(r) != ncol for r in self.data]):
raise Exception("Las filas deben ser del mismo tamano")
def __getitem__(self, idx):
return self.data[idx[0]][idx[1]]
In [8]:
A=Array([[2,6,12],[5,10,15]])
A[0,2]
Out[8]:
In [9]:
class Array:
"Una clase minima para algebra lineal"
def __init__(self, list_of_rows):
"Constructor y validador"
# obtener dimensiones
self.data = list_of_rows
nrow = len(list_of_rows)
# ___caso vector: redimensionar correctamente
if not isinstance(list_of_rows[0], list):
nrow = 1
self.data = [[x] for x in list_of_rows]
# ahora las columnas deben estar bien aunque sea un vector
ncol = len(self.data[0])
self.shape = (nrow, ncol)
# validar tamano correcto de filas
if any([len(r) != ncol for r in self.data]):
raise Exception("Las filas deben ser del mismo tamano")
def __setitem__(self, idx, value):
self.data[idx[0]][idx[1]] = value
def __repr__(self):
return "\n".join(str(x) for x in self.data)
def __str__(self):
return "\n".join(repr(x) for x in self.data)
In [10]:
A=Array([[2,6,12],[5,10,15]])
A[0,2]=8
In [12]:
A.data
Out[12]:
In [14]:
print(A)
In [15]:
A = Array([[1,2], [3,4]])
A[0,0]
In [16]:
##ejercicio 3.1
def zeros (x, y):
zeros = [[0 for x in range(x)] for y in range(y)]
return zeros
In [18]:
M=zeros(5,5)
In [19]:
print(M)
In [20]:
zeros(5,5)
Out[20]:
In [21]:
#Ejercicio 3.2
def eye(n):
matrix = [[0]*n for i in range(n)]
for i in range(n):
matrix[i][i] = 1
return matrix
In [22]:
eye(5)
Out[22]:
In [115]:
np.array([[1,2], [3,4]]).transpose()
Out[115]:
EJERCICIO 4
In [121]:
matrix=[[1,2],[3,4]]
In [123]:
[[row[i] for row in matrix] for i in range(2)]
Out[123]:
In [124]:
"hola " + "tu"
Out[124]:
In [125]:
[1,2,3] + [2,3,4]
Out[125]:
In [157]:
np.array([1,2,3]) + np.array([2,3,4])
Out[157]:
In [127]:
np.array([1,2,3]) + 10 # Broadcasted sum, es muy util
Out[127]:
In [128]:
class Array:
"Una clase minima para algebra lineal"
def __init__(self, list_of_rows):
"Constructor y validador"
# obtener dimensiones
self.data = list_of_rows
nrow = len(list_of_rows)
# ___caso vector: redimensionar correctamente
if not isinstance(list_of_rows[0], list):
nrow = 1
self.data = [[x] for x in list_of_rows]
# ahora las columnas deben estar bien aunque sea un vector
ncol = len(self.data[0])
self.shape = (nrow, ncol)
# validar tamano correcto de filas
if any([len(r) != ncol for r in self.data]):
raise Exception("Las filas deben ser del mismo tamano")
def __add__(self, other):
"Hora de sumar"
if isinstance(other, Array):
if self.shape != other.shape:
raise Exception("Las dimensiones son distintas!")
rows, cols = self.shape
newArray = Array([[0. for c in range(cols)] for r in range(rows)])
for r in range(rows):
for c in range(cols):
newArray.data[r][c] = self.data[r][c] + other.data[r][c]
return newArray
elif isinstance(2, (int, float, complex)): # en caso de que el lado derecho sea solo un numero
rows, cols = self.shape
newArray = Array([[0. for c in range(cols)] for r in range(rows)])
for r in range(rows):
for c in range(cols):
newArray.data[r][c] = self.data[r][c] + other
return newArray
else:
return NotImplemented # es un tipo de error particular usado en estos metodos
In [155]:
A = Array([[1,2], [3,4]])
B = Array([[5,6], [7,8]])
C = A + B
C.data
Out[155]:
In [153]:
D = A + 10
In [131]:
D.data
Out[131]: