MCPcopy Index your code
hub / github.com/OmkarPathak/pygorithm / add

Method add

pygorithm/math/matrix_operations.py:28–50  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 '''

Callers 2

test_matrix_additionMethod · 0.95
searchFunction · 0.80

Calls

no outgoing calls

Tested by 1

test_matrix_additionMethod · 0.76