# Not clear why cumsum! doesn't work in this simple case

**URL:** https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510
**Category:** General Usage
**Created:** [April 30, 2020, 10:20pm UTC](https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510 "2020-04-30T22:20:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [April 30, 2020, 10:20pm UTC](https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510/1 "2020-04-30T22:20:04Z")

</div>

The source vector is a column of x and the destination vector is a different column of x. There is nothing in the documentation or source code that says that the base array can’t be the same. Seems to defeat the purpose for some common cases. See the example.

It’s easy enough to use the non-inplace version.

Well, it doesn’t work when I do an example with x and y–different base arrays. It appears that it doesn’t like slices for some reason. Is there something about slices that makes the implementation infeasible?

Example:

```julia
julia> x = [collect(1:5) collect(4:8) zeros(Int,5)]

5×3 Array{Int64,2}:
 1 4 0
 2 5 0
 3 6 0
 4 7 0
 5 8 0

julia> display(x)
5×3 Array{Int64,2}:
 1 4 0
 2 5 0
 3 6 0
 4 7 0
 5 8 0

julia> cumsum!(x[:,3], x[:,2], dims=1)
5-element Array{Int64,1}:
  4
  9
 15
 22
 30

julia> x
5×3 Array{Int64,2}:
 1 4 0
 2 5 0
 3 6 0
 4 7 0
 5 8 0

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [April 30, 2020, 10:23pm UTC](https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510/2 "2020-04-30T22:23:27Z")

</div>

You’re making a copy when you slice `x`. That copy is modified but `x` is unchanged. If you use views instead, then you get (presumably) the desired result:

```julia
julia> @views cumsum!(x[:,3], x[:,2], dims=1)
5-element view(::Array{Int64,2}, :, 3) with eltype Int64:
  4
  9
 15
 22
 30

julia> x
5×3 Array{Int64,2}:
 1 4 4
 2 5 9
 3 6 15
 4 7 22
 5 8 30

```

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [May 1, 2020, 4:26am UTC](https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510/3 "2020-05-01T04:26:06Z")

</div>

Woops! Sometimes I forget when we copy and when we get an alias to the same data in memory.

That’s easy to fix. No problem doing this with the macro. Is this the best way for this use case?

Thanks.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 1, 2020, 5:41am UTC](https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510/4 "2020-05-01T05:41:37Z")

</div>

Other than looping manually, yes, that should be pretty good. Creating the views is not that expensive compared to copying the array contents (depending on the size of the input array, of course).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [May 1, 2020, 3:17pm UTC](https://discourse.julialang.org/t/not-clear-why-cumsum-doesnt-work-in-this-simple-case/38510/5 "2020-05-01T15:17:07Z")

</div>

On master creating views is now basically free, so in 1.5 using views will be just as good as writing out the loops.
