python - Numpy "double"-broadcasting - is it possible? -


is possible use "double"-broadcasting remove loop in following code? in other words, broadcast across entire time array t same-dimensioned arrays freqs , phases.

freqs = np.arange(100) phases = np.random.randn(len(freqs)) t = np.arange(0, 500)  signal = np.zeros(len(t)) in xrange(len(signal)):     signal[i] = np.sum(np.cos(freqs*t[i] + phases)) 

you can reshape t 2d array adding new axis it, trigger broadcasting when multiplied/added 1d array, , later on use numpy.sum collapse axis:

np.sum(np.cos(freqs * t[:,none] + phases), axis=1) #                      add new axis        remove sum 

testing:

(np.sum(np.cos(freqs * t[:,none] + phases), axis=1) == signal).all() # true 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -