def matrix_times_matrix(m1: Matrix, m2: Matrix) -> Matrix: nr1, nc1 = shape(m1) nr2, nc2 = shape(m2) assert nc1 == nr2, "must have (# of columns in m1) == (# of rows in m2)" def entry_fn(i: int, j: int) -> float: """dot product of i-th row of m1 with j-th column of m2""" return sum(m1[i][k] * m2[k][j] for k in range(nc1)) return make_matrix(nr1, nc2, entry_fn)
def matrix_times_matrix(m1: Matrix, m2: Matrix) -> Matrix: nr1, nc1 = shape(m1) nr2, nc2 = shape(m2) assert nc1 == nr2, "liczba kolumn m1 musi być równa liczbie wierszy m2" def entry_fn(i: int, j: int) -> float: """Wynik funkcji dot na i-tym rzędzie macierzy m1 i j-tej kolumnie macierzy m2""" return sum(m1[i][k] * m2[k][j] for k in range(nc1)) return make_matrix(nr1, nc2, entry_fn)
def matrix_times_vector(m: Matrix, v: Vector) -> Vector: nr, nc = shape(m) n = len(v) assert nc == n, "must have (# of cols in m) == (# of elements in v)" return [dot(row, v) for row in m] # output has length nr
def matrix_times_vector(m: Matrix, v: Vector) -> Vector: nr, nc = shape(m) n = len(v) assert nc == n, "liczba kolumn w m musi być równa liczbie elementów v" return [dot(row, v) for row in m] # wynik ma długość nr