Skip to content

Commit

Permalink
Add Numeric Matrix Process_up to stage 3
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchiang-tw committed Jun 29, 2020
1 parent a9945ee commit 0df532e
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Numeric Matrix Processor/Numeric Matrix Processor_stage_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
def get_matrix():

h, w = map(int,input().split())

matrix = [ [] for _ in range(h) ]

for y in range(h):
matrix[y] = [*map(int, input().split())]

matrix_container[idx] = matrix

return matrix

# ---------------------------------------------------------

def add_matrices( matrix_1, matrix_2 ):

h_1, w_1 = len(matrix_1), len(matrix_1[0])
h_2, w_2 = len(matrix_2), len(matrix_2[0])

if (h_1 != h_2) and (w_1 != w_2):
raise Exception('ERROR')

result = [[0 for _ in range(w_1) ] for _ in range(h_1) ]

for y in range(h_1):
for x in range(w_1):
result[y][x] = matrix_1[y][x] + matrix_2[y][x]

return result

# ---------------------------------------------------------

def print_matrix( matrix):

h, w = len(matrix), len(matrix[0])

for y in range(h):
for x in range(w):
print(matrix[y][x], end=" ")

print()

return

# ---------------------------------------------------------


matrix_container = [None, None]

for idx in range(2):

matrix = get_matrix()
matrix_container[idx] = matrix


first_matrix = matrix_container[0]
second_matrix = matrix_container[1]

try:
result = add_matrices(first_matrix, second_matrix)
print_matrix(result)

except Exception as e:
print(e)
84 changes: 84 additions & 0 deletions Numeric Matrix Processor/Numeric Matrix Processor_stage_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
def get_matrix():

h, w = map(int,input().split())

matrix = [ [] for _ in range(h) ]

for y in range(h):
matrix[y] = [*map(int, input().split())]

matrix_container[idx] = matrix

return matrix

# ---------------------------------------------------------

def get_constant():

return int(input())

# ---------------------------------------------------------

def add_matrices( matrix_1, matrix_2 ):

h_1, w_1 = len(matrix_1), len(matrix_1[0])
h_2, w_2 = len(matrix_2), len(matrix_2[0])

if (h_1 != h_2) and (w_1 != w_2):
raise Exception('ERROR')

result = [[0 for _ in range(w_1) ] for _ in range(h_1) ]

for y in range(h_1):
for x in range(w_1):
result[y][x] = matrix_1[y][x] + matrix_2[y][x]

return result

# ---------------------------------------------------------

def multiply_by_number( matrix_1, constant ):

h_1, w_1 = len(matrix_1), len(matrix_1[0])

result = [[0 for _ in range(w_1) ] for _ in range(h_1) ]

for y in range(h_1):
for x in range(w_1):
result[y][x] = constant * matrix_1[y][x]

return result

# ---------------------------------------------------------

def print_matrix( matrix):

h, w = len(matrix), len(matrix[0])

for y in range(h):
for x in range(w):
print(matrix[y][x], end=" ")

print()

return

# ---------------------------------------------------------


matrix_container = [None, None]

for idx in range(1):

matrix = get_matrix()
matrix_container[idx] = matrix

constant = get_constant()
first_matrix = matrix_container[0]

try:
result = multiply_by_number(first_matrix, constant)
print_matrix(result)

except Exception as e:
print(e)
156 changes: 156 additions & 0 deletions Numeric Matrix Processor/Numeric Matrix Processor_stage_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
def get_matrix(msg1, msg2, mode):

print(msg1)


h, w = map(int,input().split())

matrix = [ [] for _ in range(h) ]

print(msg2)
for y in range(h):

if mode == "int":
matrix[y] = [*map(int, input().split())]

elif mode == "float":
matrix[y] = [*map(float, input().split())]


return matrix

# ---------------------------------------------------------

def get_constant():
print("Enter constant:")
return float(input())

# ---------------------------------------------------------

def add_matrices( matrix_1, matrix_2 ):

h_1, w_1 = len(matrix_1), len(matrix_1[0])
h_2, w_2 = len(matrix_2), len(matrix_2[0])

if (h_1 != h_2) and (w_1 != w_2):
raise Exception('ERROR')

result = [[0 for _ in range(w_1) ] for _ in range(h_1) ]

for y in range(h_1):
for x in range(w_1):
result[y][x] = matrix_1[y][x] + matrix_2[y][x]

return result

# ---------------------------------------------------------

def multiply_by_number( matrix_1, constant ):

h_1, w_1 = len(matrix_1), len(matrix_1[0])

result = [[0 for _ in range(w_1) ] for _ in range(h_1) ]

for y in range(h_1):
for x in range(w_1):
result[y][x] = constant * matrix_1[y][x]

return result

# ---------------------------------------------------------

def multiply_by_matrix( matrix_1, matrix_2 ):

h_1, w_1 = len(matrix_1), len(matrix_1[0])
h_2, w_2 = len(matrix_2), len(matrix_2[0])

if w_1 != h_2:
raise Exception("Error")


result = [[0 for _ in range(w_2) ] for _ in range(h_1) ]

for y in range(h_1):
for x in range(w_2):
result[y][x] = sum([matrix_1[y][k] * matrix_2[k][x] for k in range(w_1)])

return result

# ---------------------------------------------------------

def print_matrix( matrix):

print("The result is:")

h, w = len(matrix), len(matrix[0])

for y in range(h):
for x in range(w):
print(matrix[y][x], end=" ")

print()


print() # new line after result output

return

# ---------------------------------------------------------

def menu():

msg = """1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice:"""

msg_mat_size = "Enter size of matrix:"
msg_mat_content = "Enter matrix"

msg_mat_1_size = "Enter size of first matrix:"
msg_mat_2_size = "Enter size of second matrix:"

msg_mat_1_content = "Enter first matrix:"
msg_mat_2_content = "Enter second matrix:"

choice = -1

while choice != 0:

print(msg)

try:

choice = int(input())
matrix_container = [None, None]
if choice == 1:

matrix_container[0] = get_matrix(msg_mat_1_size, msg_mat_1_content, "float")
matrix_container[1] = get_matrix(msg_mat_2_size, msg_mat_2_content, "float")

result = add_matrices(matrix_container[0], matrix_container[1])
print_matrix(result)

elif choice == 2:

matrix_container[0] = get_matrix(msg_mat_size, msg_mat_content, "float")
constant = get_constant()

result = multiply_by_number(matrix_container[0], constant)
print_matrix(result)

elif choice == 3:
matrix_container[0] = get_matrix(msg_mat_1_size, msg_mat_1_content, "float")
matrix_container[1] = get_matrix(msg_mat_2_size, msg_mat_2_content, "float")

result = multiply_by_matrix(matrix_container[0], matrix_container[1])
print_matrix(result)

except Exception as e:
print(e)

# ---------------------------------------------------------

menu()

0 comments on commit 0df532e

Please sign in to comment.