3

is there a way to vectorize this?

waveheight=zeros(10000)
for t in range(10000):
    for j in range(N_frequencysteps):
        waveheight[t] = waveheight[t] + (Abs_S_newwave[j] * cos (K[j] * x - (omega[j] * ((t*0.01) - TimeShift)) + TSi_omega[j] + arg_S_newwave[j]))

2 Answers 2

4
waveheight = (Abs_S_newwave[:,None] * cos(K[:,None] * x - (omega[:,None] * ((arange(10000)[None,:]*0.01) - TimeShift)) + TSi_omega[:,None] + arg_S_newwave[:,None])).sum(axis=0)

This works if all arrays of length N_frequencysteps are 1-D numpy arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

At least one step of vectorization would be not to iterate over the elements of waveheight:

waveheight=zeros(10000)
ts = arange(10000)
for j in range(N_frequencysteps):
    waveheight += (Abs_S_newwave[j] * cos(K[j] * x - (omega[j] * 
             ((ts*0.01) - TimeShift)) + TSi_omega[j] + arg_S_newwave[j]))

This assumes that all other variables are scalars.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.