# Sumproduct function in Julia

**URL:** https://discourse.julialang.org/t/sumproduct-function-in-julia/46053
**Category:** New to Julia
**Created:** [September 4, 2020, 12:08pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053 "2020-09-04T12:08:31Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![alexlowth](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@alexlowth](https://discourse.julialang.org/u/alexlowth)
#### Post date: [September 4, 2020, 12:08pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/1 "2020-09-04T12:08:31Z")

</div>

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 = [a_e, (a_e) + (b_f), (a_e) + (b_f) + (c_g)]

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

Thanks.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 4, 2020, 12:17pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/2 "2020-09-04T12:17:20Z")

</div>

~~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](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/4) `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`.)

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [September 4, 2020, 12:20pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/3 "2020-09-04T12:20:43Z")

</div>

That only gives the last element though. Maybe something like

```julia
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

```

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [September 4, 2020, 12:22pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/4 "2020-09-04T12:22:53Z")

</div>

Any reason not to do:

```julia
cumsum(vec_1 .* vec_2)

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [September 4, 2020, 12:24pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/5 "2020-09-04T12:24:22Z")

</div>

The intermediate allocation of `vec_1 .* vec_2` I guess.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 4, 2020, 12:34pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/6 "2020-09-04T12:34:49Z")

</div>

> [@fredrikekre](#):
>
> > [@johnmyleswhite](#):
> >
> > Any reason not to do: `cumsum(vec_1 .* vec_2)`
> 
> The intermediate allocation of `vec_1 .* vec_2` I guess.

Probably a premature optimization for someone porting from Excel…

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [September 4, 2020, 12:43pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/7 "2020-09-04T12:43:40Z")

</div>

Good point!

---

<div class="post-metadata">

### Author: ![alexlowth](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@alexlowth](https://discourse.julialang.org/u/alexlowth)
#### Post date: [September 4, 2020, 1:04pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/8 "2020-09-04T13:04:10Z")

</div>

Thanks for the quick responses guys, the cumsum(vec\_1.\*vec\_2) was exactly what I was after.

Happy Friday!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 17, 2024, 6:57pm UTC](https://discourse.julialang.org/t/sumproduct-function-in-julia/46053/9 "2024-09-17T18:57:28Z")

</div>

3 posts were split to a new topic: [Optimizing sums of products (dot products)](https://discourse.julialang.org/t/optimizing-sums-of-products-dot-products/119515)
