python - Multiplying each element of a numpy array and summing it up -


i have 2 numpy arrays, x , y. x of size m, , y of size n. need multiply each element of y every element of x, , sum up.

something [sum(x[0]*y) sum(x[1]*y) sum(x[n]*y)]

this mean

np.sum(x[:, np.newaxis] * y, axis=1) 

however typically x , y large , doing

x[:, np.newaxis] * y 

creates huge temporary array, blows stuff. there better way of implementing this?

if you're multiplying each element of y every element of x, multiply elements of x first, use multiply array y number , sum:

num = x.prod()  (num * y).sum() 

edit: array specify can obtained multiplying array x sum of elements of y:

x * y.sum() 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -