From 506172279a15d4fac19b11a38e31ff9c21d24d44 Mon Sep 17 00:00:00 2001 From: Shivam Arora Date: Wed, 31 Oct 2018 15:17:11 +0530 Subject: [PATCH] Addition and multiplication algorithm of two square matrix --- matrix/matrix_multiplication_addition.py | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 matrix/matrix_multiplication_addition.py diff --git a/matrix/matrix_multiplication_addition.py b/matrix/matrix_multiplication_addition.py new file mode 100644 index 000000000..c387c43d4 --- /dev/null +++ b/matrix/matrix_multiplication_addition.py @@ -0,0 +1,36 @@ +def add(matrix_a, matrix_b): + rows = len(matrix_a) + columns = len(matrix_a[0]) + matrix_c = [] + for i in range(rows): + list_1 = [] + for j in range(columns): + val = matrix_a[i][j] + matrix_b[i][j] + list_1.append(val) + matrix_c.append(list_1) + return matrix_c + + +def multiply(matrix_a, matrix_b): + matrix_c = [] + n = len(matrix_a) + for i in range(n): + list_1 = [] + for j in range(n): + val = 0 + for k in range(n): + val = val + matrix_a[i][k] * matrix_b[k][j] + list_1.append(val) + matrix_c.append(list_1) + return matrix_c + + +def main(): + matrix_a = [[12, 10], [3, 9]] + matrix_b = [[3, 4], [7, 4]] + print(add(matrix_a, matrix_b)) + print(multiply(matrix_a, matrix_b)) + + +if __name__ == '__main__': + main()