# Should + always be commutative?

**URL:** <https://discourse.julialang.org/t/should-always-be-commutative/55906>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 24, 2021, 2:07am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906 "2021-02-24T02:07:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 24, 2021, 2:07am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/1 "2021-02-24T02:07:12Z")

</div>

From [the manual](https://docs.julialang.org/en/v1/manual/strings/):

> While `*` may seem like a surprising choice to users of languages that provide `+` for string concatenation, this use of `*` has precedent in mathematics, particularly in abstract algebra.
> 
> In mathematics, `+` usually denotes a _commutative_ operation, where the order of the operands does not matter. An example of this is matrix addition, where `A + B == B + A` for any matrices `A` and `B` that have the same shape. In contrast, `*` typically denotes a _noncommutative_ operation, where the order of the operands _does_ matter. An example of this is matrix multiplication, where in general `A * B != B * A` . As with matrix multiplication, string concatenation is noncommutative: `greet * whom != whom * greet` . As such, `*` is a more natural choice for an infix string concatenation operator, consistent with common mathematical use.

Sometimes I define my own operator methods. Should I use `+` only for commutative operations?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 24, 2021, 2:16am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/2 "2021-02-24T02:16:01Z")

</div>

I think this will always be decided on a case by case basis. That said, it’s hard to think of anything non-commutative that `+` would be an appropriate operator for. Do you have anything specific in mind?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 24, 2021, 2:59am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/3 "2021-02-24T02:59:09Z")

</div>

For example, I’m thinking of declarative plotting tools that combine plot layers with `+`, like [ggplot2 (in R)](https://ggplot2.tidyverse.org/reference/geom_point.html#examples) and [AlgebraOfGraphics.jl in Julia](https://github.com/JuliaPlots/AlgebraOfGraphics.jl). In ggplot, the order of the arguments determines the layer order (i.e. which goes on top).

---

<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:** [February 24, 2021, 3:12am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/4 "2021-02-24T03:12:06Z")

</div>

> [@jzr](#):
>
> In ggplot, the order of the arguments determines the layer order (i.e. which goes on top).

Yes, I would tend to recommend `*` or some other operator, rather than `+`, for this, in keeping with the style of Base.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 24, 2021, 3:24am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/5 "2021-02-24T03:24:21Z")

</div>

That’s good to know. Are there other operator conventions that I can learn about?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 24, 2021, 3:28am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/6 "2021-02-24T03:28:19Z")

</div>

(cc @piever in case you have thoughts on this)

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [February 24, 2021, 3:33am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/7 "2021-02-24T03:33:43Z")

</div>

In Gadfly, we use `push!`, [discussion here](https://github.com/GiovineItalia/Gadfly.jl/issues/332#issuecomment-46936686)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 24, 2021, 3:50am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/8 "2021-02-24T03:50:31Z")

</div>

The one other thing I would consider is that in general, if you have both `+` and `*` defined, they probably should distribute with each other (ie `a*(b+c)=a*b+a*c`). Again, this isn’t a hard requirement, but if you don’t have a ring structure, you should at least think carefully before using `+` and `*`.

---

<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:** [February 24, 2021, 5:40am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/9 "2021-02-24T05:40:44Z")

</div>

Does floating-point arithmetic satisfy the distributive law? Intuitively I would think that not always.

---

<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:** [February 24, 2021, 5:41am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/10 "2021-02-24T05:41:58Z")

</div>

No:

```julia
julia> 0.1 * (0.1 + 0.3)
0.04000000000000001

julia> (0.1 * 0.1) + (0.1 * 0.3)
0.04

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 24, 2021, 5:44am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/11 "2021-02-24T05:44:19Z")

</div>

Yeah floating point sucks ☹. The good news is for floating point, I think you do actually get a good error bar where it will be equal up to 1 ULP.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 24, 2021, 5:52am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/12 "2021-02-24T05:52:36Z")

</div>

You heard it here first folks. `+(::AbstractFloat, ::AbstractFloat)` and `*(::AbstractFloat, ::AbstractFloat)` are now illegal.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [February 24, 2021, 6:56am UTC](https://discourse.julialang.org/t/should-always-be-commutative/55906/13 "2021-02-24T06:56:46Z")

</div>

In AlgebraOfGraphics, `+` and `*` are very close to forming a semiring structure (other than drawing order, I would say everything works, including distributivity), so I think the overloading is justified. One can always pass an explicit `z`-level to the layer to customize what is “drawn on top”.

The `Base` way to implement it would be something like `vcat` (to combine lists of layers) and `kron` (to do all pairwise products of layers, which is currently done via `*`), but I think that makes the code less readable. I had initially though about using `⊕, ⊗` (semiring up to isomorphism) but that was not considered particularly user-friendly or easy to type. I think that’s an important consideration for something like plotting, where you often just want to explore something quickly.
