Advertisement
Guest User

python 25_03

a guest
Mar 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. class Matrix:
  2.     def __init__(self, values):
  3.         self._values = values
  4.  
  5.     @property
  6.     def values(self):
  7.         return self._values
  8.  
  9.     @values.setter
  10.     def values(self, values):
  11.         if (len(set(map(len, values)))) not in (0, 1):
  12.             raise ValueError(
  13.                 'Rows are not the same length'
  14.             )
  15.         self._values = values
  16.  
  17.     def __str__(self):
  18.         stroca = ''
  19.         for j in self._values:
  20.                 for i in j:
  21.                     stroca = stroca + str(i) + ' '
  22.                 stroca = stroca + '\n'
  23.         return stroca
  24. @property
  25.     def dimensions(self):
  26.         x = len(self._values)
  27.         if x:
  28.             y = len(self.values[0])
  29.             return x, y
  30.         return 0, 0
  31.  
  32.     def __getitem__(self, i):
  33.         x, y = i
  34.         return self._values[x][y]
  35.  
  36. def __multipl__(self, obj):
  37.         if not self.dimensions == 0:
  38.             result = []
  39.             a = 0
  40.             b = 0
  41.             for j in self._values:
  42.                 result.append([])
  43.                 for i in j:
  44.                     result[a].append(i * obj)
  45.                     b += 1
  46.                 a += 1
  47.                 b = 0
  48.         return Matrix(result)
  49.  
  50.     def __mul__(self, obj):
  51.         result = [[obj * j for j in i] for i in self._values]
  52.         return Matrix(result)
  53.  
  54.     __rmul__ = __mul__
  55.  
  56. def __substtraction__(self, obj):
  57.         if not isinstance(obj, Matrix):
  58.             raise ValueError(
  59.                 "Object %s is not type of Matrix" % obj
  60.             )
  61.         if not self.dimensions == obj.dimensions:
  62.             raise ValueError(
  63.                 "Matrix %s is not the same dimensions" % obj
  64.             )
  65.         a = obj.__multipl__(-1)
  66.         result = [
  67.             list(map(sum, zip(*t))) for t in zip(self.values, a.values)]
  68.         return result
  69.  
  70.  
  71. def __add__(self, obj):
  72.         if not isinstance(obj, Matrix):
  73.             raise ValueError(
  74.                 "Object %s is not type of Matrix" % obj
  75.             )
  76.         if not self.dimensions == obj.dimensions:
  77.             raise ValueError(
  78.                 "Matrix %s is not the same dimensions" % obj
  79.             )
  80.         result = [
  81.             list(map(sum, zip(*t))) for t in zip(self.values, obj.values)]
  82.         return result
  83.  
  84.  
  85. m = Matrix([[1, 2, 3], [4, 5, 6]])
  86. n = Matrix([[1, 1, 1], [4, 5, 6]])
  87. print(m)
  88. print(n)
  89. f = -10
  90. print(m.__multipl__(f))
  91. print(m.__mul__(n))
  92. print(m.__substtraction__(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement