En esta actividad se practicarán conceptos asociados a programación en python.
#!/usr/bin/env python
class Matrix(object):
def __init__(self, data, shape):
print "Initializing object"
self.data=data
self.shape=shape
def imprimir(self):
for i in xrange(self.shape[0]):
print self.data[self.shape[1]*i:self.shape[1]*(i+1)]
print "good bye"
def add(self, o_matrix):
result=Matrix(self.data, self.shape)
for i in xrange(self.shape[0]):
for j in xrange(self.shape[1]):
result.data[i*result.shape[1]+j]=self.data[i*self.shape[1]+j]+o_matrix.data[i*o_matrix.shape[1]+j]
return(result)
def __len__(self):
return(self.shape[0]*self.shape[1])
def main():
data=[1,2,3,4,5,6,7,8,9,10,11,12]
shape=(4,3)
matrix1=Matrix(data, shape)
matrix1.imprimir()
data2=[1]*shape[0]*shape[1]
data2=range(12)
data2.reverse()
matrix2=Matrix(data2, shape)
matrix2.imprimir()
matrix3=matrix1.add(matrix2)
matrix3.imprimir()
print len(matrix3)
if __name__=="__main__":
main()
Se evaluará el funcionamiento correcto de la suma y multiplicación de dos matrices usando objetos en python sin utilizar numpy.
~~DISCUSSION~~