I'm attempting to compose a function which adds two matrices to pass the accompanying doctests:
>>> a = [[1, 2], [3, 4]]
>>> b = [[2, 2], [2, 2]]
>>> add_matrices(a, b)
[[3, 4], [5, 6]]
>>> c = [[8, 2], [3, 4], [5, 7]]
>>> d = [[3, 2], [9, 2], [10, 12]]
>>> add_matrices(c, d)
[[11, 4], [12, 6], [15, 19]]
Because of that, I wrote a function:
def add(x, y):
return x + y
And I also wrote another function:
def add_matrices(c, d):
for i in range(len(c)):
print map(add, c[i], d[i])
Can anyone tell me, what would be the simplest way to add two matrices?