function for adding the two matrices .. note:: Matrix addition requires both the matrices to be of same size. That is both the matrices should be of nxn dimensional.
(self)
| 26 | |
| 27 | |
| 28 | def add(self): |
| 29 | ''' |
| 30 | function for adding the two matrices |
| 31 | |
| 32 | .. note:: |
| 33 | |
| 34 | Matrix addition requires both the matrices to be of same size. |
| 35 | That is both the matrices should be of nxn dimensional. |
| 36 | ''' |
| 37 | |
| 38 | # check if both the matrices are of same shape |
| 39 | if not (len(self.matrix_one) == len(self.matrix_two)) or not (len(self.matrix_one[0]) == len(self.matrix_two[0])): |
| 40 | raise Exception('Both Matrices should be of same dimensions') |
| 41 | |
| 42 | added_matrix = [[0 for i in range(len(self.matrix_one))] for j in range(len(self.matrix_two))] |
| 43 | |
| 44 | # iterate through rows |
| 45 | for row in range(len(self.matrix_one)): |
| 46 | # iterate through columns |
| 47 | for column in range(len(self.matrix_one[0])): |
| 48 | added_matrix[row][column] = self.matrix_one[row][column] + self.matrix_two[row][column] |
| 49 | |
| 50 | return added_matrix |
| 51 | |
| 52 | def subtract(self): |
| 53 | ''' |
no outgoing calls