Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition and multiplication algorithm of two square matrix
- Loading branch information
Shivam Arora
committed
Oct 31, 2018
1 parent
f0d5bc6
commit 5061722
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |