# Two index-summation loop

**URL:** https://discourse.julialang.org/t/two-index-summation-loop/86664
**Category:** New to Julia
**Tags:** question
**Created:** [September 2, 2022, 12:52am UTC](https://discourse.julialang.org/t/two-index-summation-loop/86664 "2022-09-02T00:52:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Longblackcoffee](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@Longblackcoffee](https://discourse.julialang.org/u/Longblackcoffee)
#### Post date: [September 2, 2022, 12:52am UTC](https://discourse.julialang.org/t/two-index-summation-loop/86664/1 "2022-09-02T00:52:02Z")

</div>

Hi everyone.

I am trying to program the following summation into Julia:

y\_i = c\_i + \sum\_{r=1}^{i-1} b\_r\*y\_{i-r}

So for example, in the following toy numerical example:

```julia
using DataFrames
test = DataFrame(months = 0:3, y = 0
test[!,:y] = convert.(Float64,test[!,:y])
c_test = [0,1,1.1,1.2]
b_test = [0,2,2.1,2.2]

```

the correct code would spit out the following column-vector (working out can be provided if required):

y' = [0,1,3.1,9.5,29.01]

I tried different ways of using forloops but got nowhere with them. I know the following lines are completely wrong but I don’t know where to even begin to fix it

```julia
for i in 2:nrow(test)
    for r in 2:nrow(test)
        test[i,2] = c_test[i,1] + sum(b_test[r,1]*test[i-r,2])
    end
end

```

Any help would be really appreciated, tysm!

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 2, 2022, 1:07am UTC](https://discourse.julialang.org/t/two-index-summation-loop/86664/2 "2022-09-02T01:07:53Z")

</div>

I think your “correct result” is wrong

```julia
julia> for i in eachindex(y)
           second_term = 0.0
           for r=1:i-1
               second_term += b_test[r] * y[i-r]
           end
           y[i] = c_test[i] + second_term
       end

julia> y
4-element Vector{Float64}:
 0.0
 1.0
 1.1
 3.2

```

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 2, 2022, 8:40am UTC](https://discourse.julialang.org/t/two-index-summation-loop/86664/3 "2022-09-02T08:40:21Z")

</div>

> [@Longblackcoffee](#):
>
> y\_i = c\_i + \sum\_{r=1}^{i-1} b\_r\*y\_{i-r}

It seems the expression you want is:  
 y\_i = c\_i + \sum\_{r=1}^{i-1} b\_r\*y\_{i-r+1}

i.e. y\_{i-r+1} instead of y\_{i-r} . Implementing that gives the result you expect.

```julia
julia> test= DataFrame(months = 0:3, y = 0.0)
4×2 DataFrame
 Row │ months y       
     │ Int64 Float64 
─────┼─────────────────
   1 │ 0 0.0
   2 │ 1 0.0
   3 │ 2 0.0
   4 │ 3 0.0

julia> for i in 1:nrow(test)
         test.y[i] = c_test[i]
         for r in 1:i - 1
           test.y[i] += b_test[r] * test.y[i - r + 1]
         end
       end

julia> test
4×2 DataFrame
 Row │ months y       
     │ Int64 Float64 
─────┼─────────────────
   1 │ 0 0.0
   2 │ 1 1.0
   3 │ 2 3.1
   4 │ 3 9.5

```
