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

37 lines
753 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Devolve o número de linhas da matriz M.
def matrows(M):
return len(M)
# Complete para devolver o número de colunas da matriz M.
def matcols(M):
return len(M[0])
# Complete a função para devolver uma matriz com m×n zeros.
def matzeros(m, n):
M = []
for i in range(m):
M.append(n * [0])
return M
def matzerosTEST(m, n):
M = matzeros(m, n)
M[0][1] = 1 # should change just 1 element!
return M
# Complete a função para multiplicar a matriz A pela matriz B.
def matmult(A, B):
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
def matmultTEST(A, B):
C = matmult(A, B)
return A, B, C