Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix(object):
- def __init__(self, m, n):
- self._height = m
- self._width = n
- self._matrix = [[0 for column in range(n)] for row in range(m)]
- def __str__(self):
- string = ''
- for row in self._matrix:
- for number in row:
- string += str(number) + ' '
- string = string[:-1] + '\n'
- return string[:-1]
- def __getitem__(self, key):
- if (isinstance(key[0], int)and abs(key[0]) >= self._height) or(isinstance(key[1], int) and abs(key[1]) >= self._width):
- raise IndexError
- if key[0] is Ellipsis and key[1] is Ellipsis:
- raise TypeError
- elif key[0] is Ellipsis:
- ret = list()
- for column in self._matrix:
- ret.append(column[key[1]])
- return ret
- elif key[1] is Ellipsis:
- ret = list()
- for item in self._matrix[key[0]]:
- ret.append(item)
- return ret
- else:
- return self._matrix[key[0]][key[1]]
- def __setitem__(self, key, data):
- if (isinstance(key[0], int)and abs(key[0]) >= self._height) or(isinstance(key[1], int) and abs(key[1]) >= self._width):
- raise IndexError
- if not key[0] is Ellipsis and not key[1] is Ellipsis and not isinstance(data, int):
- raise TypeError
- if key[0] is Ellipsis or key[1] is Ellipsis:
- if not isinstance(data, list) and not isinstance(data, tuple):
- raise TypeError
- for item in data:
- if not isinstance(item, int):
- raise TypeError
- if key[0] is Ellipsis and key[1] is Ellipsis:
- raise TypeError
- elif key[0] is Ellipsis:
- if len(data) != self._height:
- raise ValueError
- for i in range(len(self._matrix)):
- self._matrix[i][key[1]] = data[i]
- elif key[1] is Ellipsis:
- if len(data) != self._width:
- raise ValueError
- for i in range(0, len(self._matrix[key[0]])):
- self._matrix[key[0]][i] = data[i]
- else:
- self._matrix[key[0]][key[1]] = data
- def _add(self, other, new):
- if isinstance(other, int):
- for x in xrange(0, self._height):
- for y in xrange(0, self._width):
- new[x, y] = self[x, y] + other
- elif isinstance(other, Matrix):
- if other._width != self._width or other._height != self._height:
- raise ValueError
- for x in xrange(0, self._width):
- for y in xrange(0, self._height):
- new[y, x] = self[y, x] + other[y, x]
- else:
- raise TypeError
- return new
- def __add__(self, other):
- return self._add(other, Matrix(self._height, self._width))
- def __iadd__(self, other):
- return self._add(other, self)
- def copy(self):
- ret = Matrix(self._height, self._width)
- ret._matrix = list(self._matrix)
- return ret
- def transpose(self):
- new = self.copy()
- new._matrix = zip(*new._matrix)
- return new
- def __mul__(self, other):
- if isinstance(other, int):
- new = Matrix(self._height, self._width)
- for x in xrange(0, self._height):
- for y in xrange(0, self._width):
- new[x, y] = self[x, y] * other
- return new
- elif isinstance(other, Matrix):
- if self._width != other._height:
- raise ValueError
- new = Matrix(other._width, self._height)
- size = self._width
- result = [[sum(map(lambda x, y: x * y,
- [self[x, y2] for y2 in range(size)],
- [other[x2, y] for x2 in range(size)]))
- for y in range(size)]
- for x in range(size)]
- for x in xrange(0, len(result)):
- for y in xrange(0, len(result[0])):
- new[x, y] = result[x][y]
- return new
- else:
- raise TypeError
Add Comment
Please, Sign In to add comment