# Cumsum! safe to use in-place?

**URL:** https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690
**Category:** General Usage
**Tags:** question
**Created:** [May 3, 2020, 5:55pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690 "2020-05-03T17:55:47Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![dmitrip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmitrip/32/13994_2.png) [@dmitrip](https://discourse.julialang.org/u/dmitrip)
#### Post date: [May 3, 2020, 5:55pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/1 "2020-05-03T17:55:47Z")

</div>

Is it safe to use `cumsum!(x, x)` to replace the contents of `x` with `cumsum(x)` (in order to avoid allocating another vector to hold the result)? Am I relying on implementation details that may change?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 3, 2020, 6:17pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/2 "2020-05-03T18:17:53Z")

</div>

You can just write a `for` loop instead and guarantee to yourself what the function does.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 3, 2020, 6:27pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/3 "2020-05-03T18:27:55Z")

</div>

Seems ok:

```julia
julia> r = rand(10);

julia> cumsum(r) == cumsum!(r, r)
true

```

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [May 3, 2020, 6:28pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/4 "2020-05-03T18:28:02Z")

</div>

From a quick test, `cumsum!(x, x)` seems to return the same results as `cumsum!(copy(x), x)`.

**Edit:** I’m actually not sure what the below _should_ do — `prod!` returns a result of different shape than its input. I was thinking of the cumulative product, which is a different function, and does return the same result in both cases.

* * *

Looking at a similar function, `prod!(x, x)` does not return the same results as `prod!(copy(x), x)`, so the fact that `cumsum!` works seems like it’s an implementation detail.

I think the `prod!` behavior is a footgun. It should be possible to check for aliasing and either error or revert to a slower algorithm that returns the correct result, rather than quietly returning an incorrect one.

```julia
julia> x = [2, 3, 4]
3-element Array{Int64,1}:
 2
 3
 4

julia> prod!(copy(x), x)
3-element Array{Int64,1}:
 2
 3
 4

julia> prod!(x, x)
3-element Array{Int64,1}:
 1
 1
 1

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 3, 2020, 6:31pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/5 "2020-05-03T18:31:40Z")

</div>

> [@yurivish](#):
>
> but I don’t see it documented anywhere.

Shouldn’t it be documented if it _doesn’t_ work? I remember seeing this specified in some `!` functions, though I cannot remember which ones right now, of course.

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [May 3, 2020, 6:39pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/6 "2020-05-03T18:39:02Z")

</div>

> [@DNF](#):
>
> Shouldn’t it be documented if it _doesn’t_ work?

That would be better. It would be nice if functions like this always either returned a correct result or threw an error, with clearly documented exceptions when error checking is impossible or too expensive.

---

<div class="post-metadata">

### Author: ![dmitrip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmitrip/32/13994_2.png) [@dmitrip](https://discourse.julialang.org/u/dmitrip)
#### Post date: [May 3, 2020, 6:57pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/7 "2020-05-03T18:57:18Z")

</div>

Seems safest to write a `for` loop as @dpsanders suggested, or to write a single-argument `inplace_accumulate!(op, x)` that wraps this `for` loop.  
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: [May 3, 2020, 7:02pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/8 "2020-05-03T19:02:16Z")

</div>

> [@dpsanders](#):
>
> You can just write a `for` loop instead and guarantee to yourself what the function does.

Yes, although `cumsum` uses pairwise summation for improved accuracy over a simple loop.

---

<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: [May 3, 2020, 7:04pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/9 "2020-05-03T19:04:02Z")

</div>

> [@DNF](#):
>
> Shouldn’t it be documented if it _doesn’t_ work? I remember seeing this specified in some `!` functions, though I cannot remember which ones right now, of course.

Arguably, if it works for `input == output`, there should just be a single-argument `cumsum!(x)` function. (This should make an easy PR for anyone who wants to take it on.)

(Since the `cumsum!` implementation uses each input exactly once, IIRC, then it should indeed be safe to overwrite the input.)

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [May 3, 2020, 7:13pm UTC](https://discourse.julialang.org/t/cumsum-safe-to-use-in-place/38690/10 "2020-05-03T19:13:40Z")

</div>

xref

[https://github.com/JuliaLang/julia/issues/26412](https://github.com/JuliaLang/julia/issues/26412)

(Edit: I am on a roll today… my apologies. this is not actually related.)
