# Accumulation in the for loop

**URL:** https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870
**Category:** General Usage
**Tags:** question
**Created:** [January 19, 2022, 3:28pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870 "2022-01-19T15:28:51Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 3:28pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/1 "2022-01-19T15:28:52Z")

</div>

My problem can be simply understand by the following case:  
when I write the following program, I hope that `c=a[1:5]+b[1:3]`

```julia
a=[1 2 3 4 5]
b=[1 2 3]
c = 0
for i in 1:5, j in 1:3
    c += a[i] + b[j]  
end

```

but that is equal to

```julia
a = [1 2 3 4 5]
b = [1 2 3]
c = 0
for i in 1:5
    for j in 1:3
        c += a[i] + b[j]
    end
end

```

how can I write `c=a[1:5]+b[1:3]` in one formula with for loop instead of array or sum function, because  
for my real program, sum or array is too slow compare with for loop

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [January 19, 2022, 3:46pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/2 "2022-01-19T15:46:44Z")

</div>

I think you want something along the lines of

```julia
for v in [a, b] # iterate among the various "containers"
    for i in eachindex(v) # iterate along the indices of each container
        c += v[i]
    end
end

```

That being said, if you have a list of arrays, e.g. `vs = [rand(100) for _ in 1:100]`, you could use the `sum(f, v)` method (apply `f` to each entry and sum the results) and just do

```julia
sum(sum, vs) # add together the sum of each array

```

I think this should be pretty fast (and not allocate memory), so it’s probably good to benchmark to see if it really is the bottleneck in your program.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 19, 2022, 4:06pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/3 "2022-01-19T16:06:39Z")

</div>

> [@xspeng](#):
>
> c=a[1:5]+b[1:3]

What exactly are you expecting? Here you are summing a vector of length 5 with another vector of length 3, it is not clear to me what you want as a result.

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 4:09pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/4 "2022-01-19T16:09:41Z")

</div>

My fault, I just wnat to add each element of array `a` and arry `b`, here the example is too simple, actually, I also need to make some complex calculation for each element of array a and array b and then sum them togher with for loop

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 19, 2022, 4:13pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/5 "2022-01-19T16:13:04Z")

</div>

```julia
julia> some_complex_calculation(x) = x + sin(x)
some_complex_calculation (generic function with 1 method)

julia> a = [1,2,3,4,5]; b = [6,7,8,9,10]; # same length (and comas, if these are column-vectors)

julia> c = some_complex_calculation.(a) .+ some_complex_calculation.(b) # note the dots
5-element Vector{Float64}:
  7.562055486608971
 10.566284025544471
 12.13047825468325
 12.655315989933829
 13.497054614447492

```

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 4:14pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/6 "2022-01-19T16:14:22Z")

</div>

Yeah, seems work, I will try, if I have more questions, will ask you again, thanks

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 4:22pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/7 "2022-01-19T16:22:23Z")

</div>

My initial plan is doing this, but this will take on a lot of memory allocation, so I plan to use for loop to expand the equations, and add them with for loop and it works better, but now, I have to add two formulas with different array length together(add each element of each formulas together), it’s easy to realize it with dot, but not easy with for loop since their length is not the same, that’s why I ask this question, it’s also hard to explain clearly my question, thanks anyway

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 19, 2022, 4:43pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/8 "2022-01-19T16:43:55Z")

</div>

There one would only be allocating the output array `c`, which could be also preallocated and overwritten with `c .=` . And that would be equivalent to the loop, roughly.

But anyway, I do not get exactly what you want, I’m happy others did. Good luck 🙂

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 5:01pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/9 "2022-01-19T17:01:21Z")

</div>

Maybe this example is more close to my question

```julia
a=rand(5)
b=rand(5)
c=rand(5)
d=rand(3)
e=rand(3)
y=dot(a,b.*exp(c.+2))+dot(d,e) # the initial formula which will cause a lot of memory allocation

```

I just want to realize the same purpose with for loop, but `a,b,c,` is longer than `d,e` array, and I cannot use for loop to combine them together, initially there is no `dot(d,e)`, I can write it like

```julia
for i in 1:length(a)
    y+=a[i]*b[i]*exp(c[i]+2)
end

```

but now with `dot(d,e)`, how can I realize the same aim as this `y=dot(a,b.*exp(c.+2))+dot(d,e)` with for loop

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 19, 2022, 5:07pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/10 "2022-01-19T17:07:10Z")

</div>

Compute the `dot(d,e)` before the loop and add it as a scalar in the loop to each element.

Ps. The allocations in your code are from the `b.*exp(c.+2)` expression, which generates a new array. It is fine to use a loop to solve that, and one-liners also are possible.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [January 19, 2022, 5:11pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/11 "2022-01-19T17:11:24Z")

</div>

> [@xspeng](#):
>
> but now with `dot(d,e)` , how can I realize the same aim as this `y=dot(a,b.*exp(c.+2))+dot(d,e)` with for loop

You can just have `y = dot(d,e)` before the loop, or `y += dot(d,e)` after. You want to iterate over these two things one after the other, that’s what the code with two `dot`s does. Your first post takes the product of the iteration spaces, you want the union.

> [@xspeng](#):
>
> `y = dot(A, B.*exp(C.+2))`

This (edited to make capitals!) could be

```julia
mapreduce(+, A, B, C) do a,b,c
  a' * b * exp(c+2)
end

```

but unfortunately that’ll be no faster at the moment, as it just calls `map` which materialises the array before summing.

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 5:11pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/12 "2022-01-19T17:11:31Z")

</div>

I cannot do that in real programs, since here just one equation, in reality, there are hundreds of equations

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 5:15pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/13 "2022-01-19T17:15:13Z")

</div>

and both the first part and the second part will cause allocation, remember here is a simple example, in reality both parts are very long equations with temperory allocation, but without array calculation ( like .+, .-), it will be faster, and that’s why I want for loop

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 19, 2022, 5:21pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/14 "2022-01-19T17:21:57Z")

</div>

I think there are some misconceptions there about what allocates and what does not, but it is hard to tell without a more realistic example. Equations being long or short, being many or a few, do not really relate to the issues.

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 5:23pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/15 "2022-01-19T17:23:26Z")

</div>

Yeah, maybe I am wrong, I already learned a lot from your answer, I will try them later, thanks

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 5:33pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/16 "2022-01-19T17:33:09Z")

</div>

![Capture](https://global.discourse-cdn.com/julialang/original/3X/0/4/046757d64fe1894d989aedf032f263618fcb2f30.png)

Or maybe you can see this equation, this is part of my program. Let’s see dy[1], it includes three dot calculation, the first one is for 5 element array, the second and the third dot calculation is for 3 element array. My initial formula doesn’t have the second and the third dot, so I just write it like this

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/c/c/cc7f4d86fe54ae62d421dee9d13a6177cee3b94d.png)

and it do decrease the allocation from ~900k to 41  
Now with the second and third one, I want to do the same thing with for loop, but the array size is different

Or maybe I can use the for loop twice

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 19, 2022, 5:56pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/17 "2022-01-19T17:56:34Z")

</div>

I am stupid, I think using the for loop twice will be good enough, thanks for every reply, I learn a lot and julia is fantastic

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [January 21, 2022, 2:31pm UTC](https://discourse.julialang.org/t/accumulation-in-the-for-loop/74870/18 "2022-01-21T14:31:09Z")

</div>

Hi, if you have time, could you please help me see another question about distributed problems, here is the link

> [Distributed solving ODEs problem](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983)
