← Lesson
Matrices
Joeven
Run
Reset
Python loads on first run
def shape(M): return (len(M), len(M[0]) if M else 0) def dot(u, v): return sum(a * b for a, b in zip(u, v)) def mat_vec(W, x): m, n = shape(W) if len(x) != n: raise ValueError("need length " + str(n) + ", got " + str(len(x))) return [dot(row, x) for row in W] def add_vec(u, v): return [a + b for a, b in zip(u, v)] W = [ [1.0, 0.0, 2.0], [0.0, 1.0, -1.0], ] b = [0.5, -0.5] x = [1.0, 4.0, 3.0] y = add_vec(mat_vec(W, x), b) print("W shape", shape(W)) print("x", x) print("W x", mat_vec(W, x)) print("W x + b", y)
Run to execute this in your browser. Nothing is sent to a server.