uaveiro-leci/1ano/fp/extra1/ex03.py

37 lines
753 B
Python
Raw Normal View History

2022-12-01 22:22:35 +00:00
# Devolve o número de linhas da matriz M.
def matrows(M):
2023-01-30 16:48:43 +00:00
return len(M)
2022-12-01 22:22:35 +00:00
# Complete para devolver o número de colunas da matriz M.
def matcols(M):
2023-01-30 16:48:43 +00:00
return len(M[0])
2022-12-01 22:22:35 +00:00
# Complete a função para devolver uma matriz com m×n zeros.
def matzeros(m, n):
2023-01-30 16:48:43 +00:00
M = []
for i in range(m):
M.append(n * [0])
return M
2022-12-01 22:22:35 +00:00
def matzerosTEST(m, n):
2023-01-30 16:48:43 +00:00
M = matzeros(m, n)
M[0][1] = 1 # should change just 1 element!
return M
2022-12-01 22:22:35 +00:00
# Complete a função para multiplicar a matriz A pela matriz B.
def matmult(A, B):
2023-01-30 16:48:43 +00:00
assert matcols(A) == matrows(B)
C = [[sum(a * b for a, b in zip(A_row, B_col))
for B_col in zip(*B)]
for A_row in A]
return C
2022-12-01 22:22:35 +00:00
2023-01-30 16:48:43 +00:00
def matmultTEST(A, B):
C = matmult(A, B)
return A, B, C