Guest User

Untitled

a guest
Oct 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. class Matrix(object):
  2. def __init__(self, m, n):
  3. self._height = m
  4. self._width = n
  5. self._matrix = [[0 for column in range(n)] for row in range(m)]
  6. def __str__(self):
  7. string = ''
  8. for row in self._matrix:
  9. for number in row:
  10. string += str(number) + ' '
  11. string = string[:-1] + '\n'
  12. return string[:-1]
  13. def __getitem__(self, key):
  14. if (isinstance(key[0], int)and abs(key[0]) >= self._height) or(isinstance(key[1], int) and abs(key[1]) >= self._width):
  15. raise IndexError
  16. if key[0] is Ellipsis and key[1] is Ellipsis:
  17. raise TypeError
  18. elif key[0] is Ellipsis:
  19. ret = list()
  20. for column in self._matrix:
  21. ret.append(column[key[1]])
  22. return ret
  23. elif key[1] is Ellipsis:
  24. ret = list()
  25. for item in self._matrix[key[0]]:
  26. ret.append(item)
  27. return ret
  28. else:
  29. return self._matrix[key[0]][key[1]]
  30. def __setitem__(self, key, data):
  31. if (isinstance(key[0], int)and abs(key[0]) >= self._height) or(isinstance(key[1], int) and abs(key[1]) >= self._width):
  32. raise IndexError
  33. if not key[0] is Ellipsis and not key[1] is Ellipsis and not isinstance(data, int):
  34. raise TypeError
  35. if key[0] is Ellipsis or key[1] is Ellipsis:
  36. if not isinstance(data, list) and not isinstance(data, tuple):
  37. raise TypeError
  38. for item in data:
  39. if not isinstance(item, int):
  40. raise TypeError
  41. if key[0] is Ellipsis and key[1] is Ellipsis:
  42. raise TypeError
  43. elif key[0] is Ellipsis:
  44. if len(data) != self._height:
  45. raise ValueError
  46. for i in range(len(self._matrix)):
  47. self._matrix[i][key[1]] = data[i]
  48. elif key[1] is Ellipsis:
  49. if len(data) != self._width:
  50. raise ValueError
  51. for i in range(0, len(self._matrix[key[0]])):
  52. self._matrix[key[0]][i] = data[i]
  53. else:
  54. self._matrix[key[0]][key[1]] = data
  55. def _add(self, other, new):
  56. if isinstance(other, int):
  57. for x in xrange(0, self._height):
  58. for y in xrange(0, self._width):
  59. new[x, y] = self[x, y] + other
  60. elif isinstance(other, Matrix):
  61. if other._width != self._width or other._height != self._height:
  62. raise ValueError
  63. for x in xrange(0, self._width):
  64. for y in xrange(0, self._height):
  65. new[y, x] = self[y, x] + other[y, x]
  66. else:
  67. raise TypeError
  68. return new
  69. def __add__(self, other):
  70. return self._add(other, Matrix(self._height, self._width))
  71. def __iadd__(self, other):
  72. return self._add(other, self)
  73. def copy(self):
  74. ret = Matrix(self._height, self._width)
  75. ret._matrix = list(self._matrix)
  76. return ret
  77. def transpose(self):
  78. new = self.copy()
  79. new._matrix = zip(*new._matrix)
  80. return new
  81. def __mul__(self, other):
  82. if isinstance(other, int):
  83. new = Matrix(self._height, self._width)
  84. for x in xrange(0, self._height):
  85. for y in xrange(0, self._width):
  86. new[x, y] = self[x, y] * other
  87. return new
  88. elif isinstance(other, Matrix):
  89. if self._width != other._height:
  90. raise ValueError
  91. new = Matrix(other._width, self._height)
  92. size = self._width
  93. result = [[sum(map(lambda x, y: x * y,
  94. [self[x, y2] for y2 in range(size)],
  95. [other[x2, y] for x2 in range(size)]))
  96. for y in range(size)]
  97. for x in range(size)]
  98. for x in xrange(0, len(result)):
  99. for y in xrange(0, len(result[0])):
  100. new[x, y] = result[x][y]
  101. return new
  102. else:
  103. raise TypeError
Add Comment
Please, Sign In to add comment