# Inverse of cumsum/cumsum!?

**URL:** <https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828>\
**Category:** General Usage\
**Created:** [March 20, 2018, 5:40am UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828 "2018-03-20T05:40:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [March 20, 2018, 5:40am UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/1 "2018-03-20T05:40:22Z")

</div>

Is there an inverse operation for cumsum (resp. cumsum!)?

It’s not diff, exactly, because diff’s output is one element shorter (and so there can’t be a diff!, either).

---

<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:** [March 20, 2018, 8:12am UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/2 "2018-03-20T08:12:22Z")

</div>

It should be the result of concatenating the first element with the output of diff.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [March 20, 2018, 9:10am UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/3 "2018-03-20T09:10:04Z")

</div>

`diff([0; xvec])` would also be correct

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [March 20, 2018, 11:26am UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/4 "2018-03-20T11:26:37Z")

</div>

Picking this up, you could go for

```julia
function cumsum0(dx::Vector, x0 = zero(eltype(dx)))
        n = length(dx) + 1
        x = similar(dx, n)
        x[1] = x0     
        for i in 2:n
                x[i] = x[i-1] + dx[i-1] 
        end
        x
end

```

such that `diff(cumsum0(x)) ≈ x` and `cumsum0(diff(x), x[1]) ≈ x`. I have similar in my `juliarc`.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [March 20, 2018, 12:45pm UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/5 "2018-03-20T12:45:06Z")

</div>

Sorry, I was a bit unclear - of course it’s easy to do. 🙂

I was just wondering if there’s a function in Base for it - especially an allocation-free in-place version (the inverse of `cumsum!`). It would come in handy, at times, and would make things nicely symmetrical.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 20, 2018, 4:00pm UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/6 "2018-03-20T16:00:04Z")

</div>

You might be interested in this issue: [naming: setdiff, diff, gradient · Issue #26361 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/26361)

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [March 21, 2018, 1:44am UTC](https://discourse.julialang.org/t/inverse-of-cumsum-cumsum/9828/7 "2018-03-21T01:44:36Z")

</div>

`diffset()` might not be too bad, though it could still be mistaken as a complement (A union B) type of operation  
Technically it should be like an A complement union B sort of operator, so maybe `compunion()` would be ok  
It might be nice to have a `Base.Sets ` pigeon-hole for specifically set-based operators
