Skip to content
Advertisement

Can this for loop be vectorized?

JavaScript

Can this for loop be vectorized maybe by expanding dimensions and then collapsing it?

I got the hint from somewhere that I can replace

JavaScript

with

JavaScript

Advertisement

Answer

It can be vectorized by expanding dimensions as you suggested. I think the secret sauce is using np.tril to zero out terms in the progression before summing:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement