# fl.cumsum~

**URL:** <https://framelib.discourse.group/t/fl-cumsum/71>\
**Category:** Feature Requests\
**Created:** [26 November 2020 14:22 UTC](https://framelib.discourse.group/t/fl-cumsum/71 "2020-11-26T14:22:51Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![james.bradbury](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/james.bradbury/32/15_2.png) [@james.bradbury](https://framelib.discourse.group/u/james.bradbury)\
**Post date:** [26 November 2020 14:22 UTC](https://framelib.discourse.group/t/fl-cumsum/71/1 "2020-11-26T14:22:51Z")

</div>

I know a few other people have requested a cumulative sum object, (it might have been @swp1g17). Just putting this here so that its recorded. I did try to do something today where I needed it and struggled to reason around it easily with what objects are there.

---

<div class="post-metadata">

**Author:** ![o.green](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/o.green/32/29_2.png) [@o.green](https://framelib.discourse.group/u/o.green)\
**Post date:** [22 February 2021 12:24 UTC](https://framelib.discourse.group/t/fl-cumsum/71/2 "2021-02-22T12:24:29Z")

</div>

Don’t know if you ever found a way to do this, but I’ve ended up using `fl.biquad~` (somewhat cryptically) for this and frame-wise deltas:

```

----------begin_max5_patcher----------
547.3ocyVFsaaBCEF9Z3ovxWmhrAaBouJUSSDhSlqB1Lvzkoplm8YeLnltXk
wJr1IRHxmbfi+7O+GyywQ3s5ShNL5dzCnnnmiihfPt.QCiiv0kmpNV1Aogqz
00BkAux+eFwICDemb+9wfxcPH81GuKmNFT0WKUGEF31PeMnt2LFkLDsozT8M
o5vWaEUF+jKkUjPVgn4oteRYvYRBA8E2k7Rbr6zpYxPUecWecHJHKCEzh0.E
94OedTTK55JOHthBZB5hOAngyCRSZPZn2fF9FuXjkvsPQHvnTGNCWj+9X9Yi
veEX76hTk3G148Uft+XhQaS7bHFW+WnX2hwbOUDtiQdwGOhakeuub2YDwdPQ
2QQjP3xVHIMidgMiR4e77pUUhPJJayxnnbvBlkANP5+J9tg4LMAkkfXIHdHy
IqXYTRluQIMGDx+q7lr7kQIYfFRWClxOCqYq3fryHZOaMlCGgvktLJp+A2hO
Om4XmHWOHj6anNQLxRRKM88hKjG9nT86udCTKW72tFzo6aqFuUixF50xsSzY
jpRiTqtLIhOofqyStRjoTo72jjtcmnEVBmWoymRossjr58LgbyTpDcAVNca8
+Gqj6cflOSSQ3baMGR3HyCR1TJ85qVN81hxllmDscCYCEw1D3QMLwJVACkJ+
PvihaEOIGymCQJasVVi0u12BSL7ob+Kgfq0VDU8xgGOs3YKIzfQUZ2WrozSB
zGJ9k3eASf50.A
-----------end_max5_patcher-----------

```

---

<div class="post-metadata">

**Author:** ![james.bradbury](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/james.bradbury/32/15_2.png) [@james.bradbury](https://framelib.discourse.group/u/james.bradbury)\
**Post date:** [5 March 2021 22:52 UTC](https://framelib.discourse.group/t/fl-cumsum/71/3 "2021-03-05T22:52:42Z")

</div>

This is interesting. I don’t understand the maths but it works!

---

<div class="post-metadata">

**Author:** ![o.green](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/o.green/32/29_2.png) [@o.green](https://framelib.discourse.group/u/o.green)\
**Post date:** [5 March 2021 23:34 UTC](https://framelib.discourse.group/t/fl-cumsum/71/4 "2021-03-05T23:34:43Z")

</div>

The `fl.biquad~` difference eq  
`y[n] = b[0]x[n] + b[1]x[n-1] + b[2]x[n-2] - a[0]y[n-1] - a[1]y[n-2]`

and cumsum  
`y[n] = x[n] + y[n-1]`

> =\> `a[0] = -1`, `b[0] = 1`

and diff  
`y[n] = x[n] - x[n-1]`

> =\> `b[0]=1`, `b[1]=-1`

---

<div class="post-metadata">

**Author:** ![james.bradbury](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/james.bradbury/32/15_2.png) [@james.bradbury](https://framelib.discourse.group/u/james.bradbury)\
**Post date:** [6 March 2021 12:15 UTC](https://framelib.discourse.group/t/fl-cumsum/71/5 "2021-03-06T12:15:51Z")

</div>

That makes sense!

---

<div class="post-metadata">

**Author:** ![balintlaczko](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@balintlaczko](https://framelib.discourse.group/u/balintlaczko)\
**Post date:** [11 April 2023 17:30 UTC](https://framelib.discourse.group/t/fl-cumsum/71/6 "2023-04-11T17:30:08Z")

</div>

@o.green you are a true genius, this is exactly what I am looking for.

---

<div class="post-metadata">

**Author:** ![a.harker](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/a.harker/32/21_2.png) [@a.harker](https://framelib.discourse.group/u/a.harker)\
**Post date:** [12 April 2023 01:53 UTC](https://framelib.discourse.group/t/fl-cumsum/71/7 "2023-04-12T01:53:27Z")

</div>

Probably I should add something specific here. What are people using this for BTW?

---

<div class="post-metadata">

**Author:** ![balintlaczko](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@balintlaczko](https://framelib.discourse.group/u/balintlaczko)\
**Post date:** [12 April 2023 13:56 UTC](https://framelib.discourse.group/t/fl-cumsum/71/8 "2023-04-12T13:56:02Z")

</div>

Right now I want to create a flexible synth workflow that can render offline so I can use the buffer-based flucoma tools for analysis. The core to this is to be able to create a time-varying phasor signal, that’s where I need the cumsum – turns out @o.green 's biquad hack is perfect for this. As a first step I wanted to implement a non-realtime FM synth (for dataset generation), that’s what you see in fm\_nrt.maxpat. You’ll need the other patches as abstractions for it to work. What I like about the idea of fl.phasor~ and fl.sinewave~ is that you then can use these for building many other types of synths with time-varying signals as input (frames).  
[fl.cumsum~.maxpat](https://framelib.discourse.group/uploads/short-url/9ZxDNmr1AJkwDvlHYvGH3RNp1mZ.maxpat) (2.7 KB)  
[fl.phasor~.maxpat](https://framelib.discourse.group/uploads/short-url/ygAN9oYYUKwXy5oPnS5LaGGg1qz.maxpat) (4.6 KB)  
[fl.sinewave~.maxpat](https://framelib.discourse.group/uploads/short-url/iOiQm9bxFoDvrerGjs4sk28RdHO.maxpat) (3.6 KB)  
[fm\_nrt.maxpat](https://framelib.discourse.group/uploads/short-url/lI3YCpOdlLwcALNK29yAC0dGZF8.maxpat) (16.1 KB)

---

<div class="post-metadata">

**Author:** ![a.harker](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/a.harker/32/21_2.png) [@a.harker](https://framelib.discourse.group/u/a.harker)\
**Post date:** [28 May 2023 15:52 UTC](https://framelib.discourse.group/t/fl-cumsum/71/9 "2023-05-28T15:52:04Z")

</div>

Thanks - this has given me a couple of ideas - one for a simpler object that does sums (do we want any other options - like products as well?) and another about custom filters that can be written as expressions (that is probably a bit of a longer/future job, but I’d hope I might be to use the expression stuff to support that.

I’m going to a add PRs for these on GitHub to remind me. I recently made it _much_ easier to set up new objects so expanding the set should be much easier now.

---

<div class="post-metadata">

**Author:** ![balintlaczko](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@balintlaczko](https://framelib.discourse.group/u/balintlaczko)\
**Post date:** [29 May 2023 11:44 UTC](https://framelib.discourse.group/t/fl-cumsum/71/10 "2023-05-29T11:44:41Z")

</div>

> [@a.harker](#):
>
> (do we want any other options - like products as well?)

I think it would be great to have some kind of a general “lambda” interface, so you can do anything from “x0 + x1” to “x0 \* (2 ^ 1/x1)” or whatever you need. Then you only need to implement one object that can cover all cases. But I guess that can be tricky to implement, so I guess a cumsum~ is a good place to start.

---

<div class="post-metadata">

**Author:** ![a.harker](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/a.harker/32/21_2.png) [@a.harker](https://framelib.discourse.group/u/a.harker)\
**Post date:** [29 May 2023 12:03 UTC](https://framelib.discourse.group/t/fl-cumsum/71/11 "2023-05-29T12:03:31Z")

</div>

Well - filters as expressions could be used for what you want in terms of “lamdas” - whether we think of that as filter or not - that’s captured here:

> <https://github.com/AlexHarker/FrameLib/issues/114>
>
> It might be cool to have an object where you can write filters as expressions an…d use registers to create that.

That one would be a lot more fun to write than the cumulative sum (which is trivial to do), but it’s unclear to me how much work it would be…

On the product question I suppose the question is whether we want fl.cumsum~ only or fl.cumsum~ and fl.cumproduct~ or we make something more like std::accumulate that could do either (or have a limited binary operation to accumulate) - [std::accumulate - cppreference.com](https://en.cppreference.com/w/cpp/algorithm/accumulate)

---

<div class="post-metadata">

**Author:** ![james.bradbury](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/james.bradbury/32/15_2.png) [@james.bradbury](https://framelib.discourse.group/u/james.bradbury)\
**Post date:** [29 May 2023 12:05 UTC](https://framelib.discourse.group/t/fl-cumsum/71/12 "2023-05-29T12:05:51Z")

</div>

Speculatively, something that allows you to do higher order functions from functional programming could solve all these problems in an elegant-ish way.

`fl.map~` already exists and kind of does the same thing but just purely for scaling, and in many cases `fl.expr~` gives you what you need for anything where its not just about scaling. I think something like `fl.reduce~` could be cool, and you get two variables in the box to parse and work with. a bit like `js` but it’s not particularly idiomatic to Max in that sense. You also often need the index with a reduction to do anything like a cumulative sum. Tricky problem I suppose if the idea is to make something more generic than ` fl.cumsum~`

---

<div class="post-metadata">

**Author:** ![a.harker](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/a.harker/32/21_2.png) [@a.harker](https://framelib.discourse.group/u/a.harker)\
**Post date:** [29 May 2023 12:08 UTC](https://framelib.discourse.group/t/fl-cumsum/71/13 "2023-05-29T12:08:16Z")

</div>

These likely won’t be rolled into one object. As @o.green has demonstrated - you can do this with a biquad (so by that measure the object isn’t needed at all) but the point is for things to be easy to access, so cumulative sums (or accumulation) to me is one concept and a lambda/reduce/generic filter/whatever clever thing is a second idea so that you don’t have to roll your own accumulator if you are not in super geek mode, but then there’s something where you can if you wish to…

That’s my current thinking anyway. Thoughts?

---

<div class="post-metadata">

**Author:** ![james.bradbury](https://yyz2.discourse-cdn.com/free1/user_avatar/framelib.discourse.group/james.bradbury/32/15_2.png) [@james.bradbury](https://framelib.discourse.group/u/james.bradbury)\
**Post date:** [29 May 2023 21:31 UTC](https://framelib.discourse.group/t/fl-cumsum/71/14 "2023-05-29T21:31:58Z")

</div>

That all makes sense to me 🙂

---

<div class="post-metadata">

**Author:** ![balintlaczko](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@balintlaczko](https://framelib.discourse.group/u/balintlaczko)\
**Post date:** [30 May 2023 13:04 UTC](https://framelib.discourse.group/t/fl-cumsum/71/15 "2023-05-30T13:04:00Z")

</div>

That sounds great! Personally I am totally okay with just having the `fl.cumsum~` as an abastraction from the biquad. If there was only the “roll-your-own” generic object and I needed cumsum often I’d likely make an `fl.cumsum~` abstraction anyway as a shortcut.
