Matrix times Vector
Write a Python function that takes the dot product of a matrix and a vector. return -1 if the matrix could not be dotted with the vector
Example:
Input:
a = [[1, 2],[2, 4]]
, b = [1, 2]
Output:
[5, 10]
Reasoning:
1*1 + 2*2 = 5; 1*2+ 2*4 = 10
Code:
import numpy as np
def matrix_dot_vector(a, b):
a = np.array(a)
b = np.array(b)
if a.shape[1] != b.shape[0]:
return -1
return np.array(a) @ np.array(b)