# Sum + squeeze as one function

**URL:** <https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471>\
**Category:** General Usage\
**Created:** [March 5, 2017, 10:41am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471 "2017-03-05T10:41:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [March 5, 2017, 10:41am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/1 "2017-03-05T10:41:08Z")

</div>

Is there a built-in version of `sum(A, dims)` that automatically squeezes the summed out dimension? Something like:

```julia
sumsqueeze(A, dim) = squeeze(sum(A, dim), dim)

```

Surely, I can just combine 2 functions, but in my use case I generate this code and having it as a single built-in function makes it much easier to optimize it later.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 6, 2017, 8:31am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/2 "2017-03-06T08:31:34Z")

</div>

What’s wrong with the definition you’re using right now?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [March 6, 2017, 9:40am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/3 "2017-03-06T09:40:24Z")

</div>

As I’ve told, I’d prefer a single built-in function to make further optimizations easier. Just to give you a sense of what I’m doing:

1. In [one of my packages](https://github.com/dfdx/Espresso.jl) I do a lot of code generation. Generated code may be executed elsewhere, so it’s better to rely only on built-in features rather than introduce my own `sumsqueeze`.
2. Each function call is parsed into a single expression, so `B = squeeze(sum(A, dim), dim)` is actually represented as:

```julia
tmp1 = sum(A, dim)
B = squeeze(tmp1)

```

which is harder to match to existing optimization patterns. Still doable, but given amount of discussion around `sum`/`squeeze` on GitHub I wanted to make sure there’s no simpler solution before going this way.

---

<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:** [March 6, 2017, 8:59pm UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/5 "2017-03-06T20:59:30Z")

</div>

> [@dfdx](#):
>
> As I’ve told, I’d prefer a single built-in function to make further optimizations easier.

There’s no difference between a “built-in” function and defining `sumsqueeze(A, dim) = squeeze(sum(A, dim), dim)` yourself, except for the module in which the function is defined.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [March 6, 2017, 10:07pm UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/6 "2017-03-06T22:07:41Z")

</div>

The difference is that `sum` and `squeeze` are always there while `sumsqueeze` would require additional dependency. Here’s a use case:

1. I create a module `A` that generates expression `ex`.
2. This expression is sent to another worker / saved to a file / manually copy-pasted to another module `B`.

If `ex` includes `sumsqueeze`, `B` should import it and thus it depends on `A`. If only built-in functions are used, no dependency is introduced.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 6, 2017, 10:27pm UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/7 "2017-03-06T22:27:14Z")

</div>

It is probably a better idea to not have `sumsqueeze` defined in `A`, but rather a third module `C` which `B` depends on.

---

<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:** [March 7, 2017, 2:02am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/8 "2017-03-07T02:02:54Z")

</div>

> [@dfdx](#):
>
> I create a module A that generates expression ex.  
> This expression is sent to another worker / saved to a file / manually copy-pasted to another module B.

It’s somewhat unusual in Julia to be doing code generation where the generated code is used via copy and paste. Julia provides a very powerful macro facility for metaprogramming, in which generated expressions can be directly inserted into code at parse time

One of the many advantages of using macros for code generation is that if a module A defines a macro `@foo`, then the code generated by `@foo` can call functions defined only in module A, even if `@foo` is called by someone else’s code or module.

---

<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:** [March 7, 2017, 4:44am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/9 "2017-03-07T04:44:37Z")

</div>

Relevant issue so you can understand why reducers don’t already squeeze:

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

In short, you often want to keep those extra dimensions, but not always. Since it’s easier to get rid of them than to try to put them back after they’re gone, we do it that way. It’s a tradeoff. One potential pattern for addressing this would be to define this `squeeze` method:

```julia
squeeze(f, A, dims) = squeeze(f(A, dims), dims)

```

But getting the type signature right might be a little tricky.

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [March 28, 2017, 12:40pm UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/10 "2017-03-28T12:40:39Z")

</div>

> [@fengyang.wang](#):
>
> It is probably a better idea to not have sumsqueeze defined in A, but rather a third module C which B depends on.

and I guess he’s just asking if that C could be/is Base 🙂

It may be the case that `squeeze(f, A, dims)` is sufficiently simple, such that there is no need to have it in Base, but I think it _is_ that case that a lot of people hit this “problem” now and again.

What is the type signature problem you’re referring to @StefanKarpinski ?

---

<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:** [March 28, 2017, 12:45pm UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/11 "2017-03-28T12:45:31Z")

</div>

> [@pkofod](#):
>
> What is the type signature problem you’re referring to @StefanKarpinski ?

I was thinking that `squeeze` was variadic and since leading function arguments are a little tricky to type sufficiently loosely (because non-functions can be callable), that would be a problem. But `squeeze` isn’t variadic – it takes a scalar or a tuple for `dims` – so that’s not an issue.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [January 15, 2018, 11:51am UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/12 "2018-01-15T11:51:22Z")

</div>

Hi, is there any update on this?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 1, 2018, 2:01pm UTC](https://discourse.julialang.org/t/sum-squeeze-as-one-function/2471/13 "2018-03-01T14:01:19Z")

</div>

I packaged a trivial workaround that I find useful:

[https://github.com/tpapp/Squeezing.jl](https://github.com/tpapp/Squeezing.jl)

Basically

```julia
julia> @squeezing sum(ones(3, 2), 1)
2-element Array{Float64,1}:
 3.0
 3.0

```
