Sumproduct function in Julia

Hey guys,

I am quite a beginner at using Julia and this is my first post, so apologies if there is an obvious answer! I would like to find out if there is an easy way to perform the sumproduct excel function in Julia.

As an example, I currently have the following vectors:

vec_1 = [a,b,c]
vec_2 = [e,f,g]

I would like to end up with a vector containing:

vec_3 = [ae, (ae) + (bf), (ae) + (bf) + (cg)]

I would be able to do this manually, but am quite interested if there is a streamlined way :grinning:

Thanks.

This is an inner product (a dot product). vec_1' * vec_2 (or just vec_1'vec_2) will do what you want. Sorry, I misread your question; this is cumsum(vec_1 .* vec_2).

(Technically, if your vectors are complex numbers, then vec_1' * vec_2 will also conjugate the elements of vec_1 in the product. If you needed an “unconjugated” dot product for complex vectors, you could do transpose(vec_2) * vec_2.)

1 Like

That only gives the last element though. Maybe something like

julia> a, b, c, e, f, g = 1:6;

julia> vec_1 = [a,b,c];

julia> vec_2 = [e,f,g];

julia> cumsum(x*y for (x,y) in zip(vec_1, vec_2))
3-element Array{Int64,1}:
  4
 14
 32
2 Likes

Any reason not to do:

cumsum(vec_1 .* vec_2)
1 Like

The intermediate allocation of vec_1 .* vec_2 I guess.

2 Likes

Probably a premature optimization for someone porting from Excel…

1 Like

Good point!

Thanks for the quick responses guys, the cumsum(vec_1.*vec_2) was exactly what I was after.

Happy Friday!