def matrix_vector_mul(M, v): """ Returns the product of matrix M and vector v. Consider using brackets notation v[...] in your procedure to access entries of the input vector. This avoids some sparsity bugs. >>> N1 = Mat(({1, 3, 5, 7}, {'a', 'b'}), {(1, 'a'): -1, (1, 'b'): 2, (3, 'a'): 1, (3, 'b'):4, (7, 'a'): 3, (5, 'b'):-1}) >>> u1 = Vec({'a', 'b'}, {'a': 1, 'b': 2}) >>> N1*u1 == Vec({1, 3, 5, 7},{1: 3, 3: 9, 5: -2, 7: 3}) True >>> N1 == Mat(({1, 3, 5, 7}, {'a', 'b'}), {(1, 'a'): -1, (1, 'b'): 2, (3, 'a'): 1, (3, 'b'):4, (7, 'a'): 3, (5, 'b'):-1}) True >>> u1 == Vec({'a', 'b'}, {'a': 1, 'b': 2}) True >>> N2 = Mat(({('a', 'b'), ('c', 'd')}, {1, 2, 3, 5, 8}), {}) >>> u2 = Vec({1, 2, 3, 5, 8}, {}) >>> N2*u2 == Vec({('a', 'b'), ('c', 'd')},{}) True >>> M3 = Mat(({0,1},{'a','b'}),{(0,'a'):1, (0,'b'):1, (1,'a'):1, (1,'b'):1}) >>> v3 = Vec({'a','b'},{'a':1,'b':1}) >>> M3*v3 == Vec({0, 1},{0: 2, 1: 2}) True """ assert M.D[1] == v.D result = Vec(M.D[0], {}) for key in M.f: if result.f.get(key[0]) == None: resultf = 0 else: resultf = result.f.get(key[0]) if v.f.get(key[1]) == None: vf = 0 else: vf = v.f.get(key[1]) result.f[key[0]] = resultf + vf * M.f[key] return result
def matrix_vector_mul(M, v): """ Returns the product of matrix M and vector v. Consider using brackets notation v[...] in your procedure to access entries of the input vector. This avoids some sparsity bugs. >>> N1 = Mat(({1, 3, 5, 7}, {'a', 'b'}), {(1, 'a'): -1, (1, 'b'): 2, (3, 'a'): 1, (3, 'b'):4, (7, 'a'): 3, (5, 'b'):-1}) >>> u1 = Vec({'a', 'b'}, {'a': 1, 'b': 2}) >>> N1*u1 == Vec({1, 3, 5, 7},{1: 3, 3: 9, 5: -2, 7: 3}) True >>> N1 == Mat(({1, 3, 5, 7}, {'a', 'b'}), {(1, 'a'): -1, (1, 'b'): 2, (3, 'a'): 1, (3, 'b'):4, (7, 'a'): 3, (5, 'b'):-1}) True >>> u1 == Vec({'a', 'b'}, {'a': 1, 'b': 2}) True >>> N2 = Mat(({('a', 'b'), ('c', 'd')}, {1, 2, 3, 5, 8}), {}) >>> u2 = Vec({1, 2, 3, 5, 8}, {}) >>> N2*u2 == Vec({('a', 'b'), ('c', 'd')},{}) True >>> M3 = Mat(({0,1},{'a','b'}),{(0,'a'):1, (0,'b'):1, (1,'a'):1, (1,'b'):1}) >>> v3 = Vec({'a','b'},{'a':1,'b':1}) >>> M3*v3 == Vec({0, 1},{0: 2, 1: 2}) True """ assert M.D[1] == v.D result = Vec( M.D[0], {}) for key in M.f : if result.f.get( key[0] ) == None : resultf = 0 else : resultf = result.f.get( key[0] ) if v.f.get(key[1]) == None : vf = 0 else : vf = v.f.get(key[1]) result.f[key[0]] = resultf + vf * M.f[key] return result
def vector_matrix_mul(v, M): """ returns the product of vector v and matrix M Consider using brackets notation v[...] in your procedure to access entries of the input vector. This avoids some sparsity bugs. >>> v1 = Vec({1, 2, 3}, {1: 1, 2: 8}) >>> M1 = Mat(({1, 2, 3}, {'a', 'b', 'c'}), {(1, 'b'): 2, (2, 'a'):-1, (3, 'a'): 1, (3, 'c'): 7}) >>> v1*M1 == Vec({'a', 'b', 'c'},{'a': -8, 'b': 2, 'c': 0}) True >>> v1 == Vec({1, 2, 3}, {1: 1, 2: 8}) True >>> M1 == Mat(({1, 2, 3}, {'a', 'b', 'c'}), {(1, 'b'): 2, (2, 'a'):-1, (3, 'a'): 1, (3, 'c'): 7}) True >>> v2 = Vec({'a','b'}, {}) >>> M2 = Mat(({'a','b'}, {0, 2, 4, 6, 7}), {}) >>> v2*M2 == Vec({0, 2, 4, 6, 7},{}) True >>> v3 = Vec({'a','b'},{'a':1,'b':1}) >>> M3 = Mat(({'a', 'b'}, {0, 1}), {('a', 1): 1, ('b', 1): 1, ('a', 0): 1, ('b', 0): 1}) >>> v3*M3 == Vec({0, 1},{0: 2, 1: 2}) True """ assert M.D[0] == v.D result = Vec(M.D[1], {}) for key in M.f: if result.f.get(key[1]) == None: resultf = 0 else: resultf = result.f.get(key[1]) if v.f.get(key[0]) == None: vf = 0 else: vf = v.f.get(key[0]) result.f[key[1]] = resultf + vf * M.f[key] return result
def vector_matrix_mul(v, M): """ returns the product of vector v and matrix M Consider using brackets notation v[...] in your procedure to access entries of the input vector. This avoids some sparsity bugs. >>> v1 = Vec({1, 2, 3}, {1: 1, 2: 8}) >>> M1 = Mat(({1, 2, 3}, {'a', 'b', 'c'}), {(1, 'b'): 2, (2, 'a'):-1, (3, 'a'): 1, (3, 'c'): 7}) >>> v1*M1 == Vec({'a', 'b', 'c'},{'a': -8, 'b': 2, 'c': 0}) True >>> v1 == Vec({1, 2, 3}, {1: 1, 2: 8}) True >>> M1 == Mat(({1, 2, 3}, {'a', 'b', 'c'}), {(1, 'b'): 2, (2, 'a'):-1, (3, 'a'): 1, (3, 'c'): 7}) True >>> v2 = Vec({'a','b'}, {}) >>> M2 = Mat(({'a','b'}, {0, 2, 4, 6, 7}), {}) >>> v2*M2 == Vec({0, 2, 4, 6, 7},{}) True >>> v3 = Vec({'a','b'},{'a':1,'b':1}) >>> M3 = Mat(({'a', 'b'}, {0, 1}), {('a', 1): 1, ('b', 1): 1, ('a', 0): 1, ('b', 0): 1}) >>> v3*M3 == Vec({0, 1},{0: 2, 1: 2}) True """ assert M.D[0] == v.D result = Vec( M.D[1], {}) for key in M.f : if result.f.get( key[1] ) == None : resultf = 0 else : resultf = result.f.get( key[1] ) if v.f.get(key[0]) == None : vf = 0 else : vf = v.f.get(key[0]) result.f[key[1]] = resultf + vf * M.f[key] return result