# How about direct sum ⊕ as vector attach operator?

**URL:** https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182
**Category:** Internals & Design
**Created:** [January 24, 2024, 5:45am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182 "2024-01-24T05:45:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)
#### Post date: [January 24, 2024, 5:45am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/1 "2024-01-24T05:45:52Z")

</div>

## Background

I know that Julia’s design is very reasonable, especially from the viewpoint of mathematics; it’s my favorite feature of Julia. For example, Julia uses `*` as a string join operator instead of `+`, unlike many programming languages, because it makes sense in the context of (abstract) algebra:

\text{"hello"} \ast \text{"world"} = \text{"helloworld"}

is much more natural than

\text{"hello"} + \text{"world"} = \text{"helloworld"}

obviously. But sometimes I miss some operators in Python. In this context, `+` serves as a vector concatenator.

```python
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1,2]
>>> y = [3,4]
>>> x + y
[1, 2, 3, 4]

```

Of course, I think `vcat(x, y)` or `[x; y]` in Julia are also great.

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

julia> y = [3,4]
2-element Vector{Int64}:
 3
 4

julia> vcat(x, y)
4-element Vector{Int64}:
 1
 2
 3
 4

julia> [x; y]
4-element Vector{Int64}:
 1
 2
 3
 4

```

I understand that this grammatical consistency is important for reading or writing code. Most significantly, I know that `x + y` is totally weird in a mathematical sense.

Then how about \oplus? Usually, in general mathematics, **a direct sum** \oplus (`\oplus`) refers to a Cartesian product of vector space or some algebraic structures. For instance, the plane \mathbb{R}^{2} and the z-axis \mathbb{R}^{1} could be merged in 3-dimensional space \mathbb{R}^{3} by \mathbb{R}^{3} = \mathbb{R}^{2} \oplus \mathbb{R}^{1}. In a coordinate system, it’s exactly the same as vector concatenation, (x, y, z) = (x, y) \oplus (z).

## Implementation

Below are simple implementations. If you love these, you can use them personally even if Julia doesn’t change. I believe all of you guys have a personal library, just for yourselves. 😂

### Binary operation

Please note that I didn’t check for performance issues. `vcat` or other ways could be more efficient and fast.

```julia
julia> ⊕(x, y) = [x; y]
⊕ (generic function with 1 method)

julia> [1, 2] ⊕ [3, 4]
4-element Vector{Int64}:
 1
 2
 3
 4

```

### \bigoplus\_{k} A\_{k}

Since \oplus is a binary operation, we can generalize it using `reduce`.

```julia
julia> reduce(⊕, [[1, 2], [3, 4], [5, 6, 7]])
7-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
 7

```

The following is a method form.

```julia
julia> ⊕(x, y...) = reduce(⊕, [x, y...])
⊕ (generic function with 2 methods)

julia> ⊕([1, 2], [3, 4], [5, 6, 7])
7-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
 7

```

### Clean code

```julia
⊕(x, y) = [x; y]
⊕(x, y...) = reduce(⊕, [x, y...])

```

Any comments or ideas are helpful to me. Thank you for your attention!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 24, 2024, 6:04am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/2 "2024-01-24T06:04:46Z")

</div>

You can of course introduce this definition for the operator in your own code or package, but adding it to Julia Base is likely not a good idea, at least not as an exported symbol. Doing so would break most code that already defines this operator for themselves. Here’s a list of registered packages that already make use of this operator:  
[https://juliahub.com/ui/Search?q=⊕&type=symbols](https://juliahub.com/ui/Search?q=%E2%8A%95&type=symbols)

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [January 24, 2024, 7:16am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/3 "2024-01-24T07:16:31Z")

</div>

Base is allowed to export new names; it won’t break any code that defines or imports this operator, only code where there is ambiguity.

`⊕` seems fine if that’s what it means — are there other mathematical usages of `⊕` that would conflict with this meaning?

* * *

Looking through the [other languages](https://rosettacode.org/wiki/Array_concatenation), I see infix operators:

```julia
,
++
∾
+
~
&
@
|||
<<

```

of which Julia has available

```julia
++
∾
~
@
|||

```

* * *

It would be nice to even have a function to concatenate sequences. `vcat` maybe should’ve been that but it does weird stuff a lot of the time.

```julia
julia> vcat("a", "b")
2-element Vector{String}:
 "a"
 "b"

```

I’d be happy enough with an `append` or `concat`.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [January 24, 2024, 7:50am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/4 "2024-01-24T07:50:01Z")

</div>

Cf. [https://github.com/JuliaLang/julia/issues/11030](https://github.com/JuliaLang/julia/issues/11030) and [RFC: deprecate \* in favor of new ++ concatenation operator by stevengj · Pull Request #22461 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/22461) for some related old discussions.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 24, 2024, 10:07am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/5 "2024-01-24T10:07:25Z")

</div>

> [@jar1](#):
>
> Base is allowed to export new names; it won’t break any code that defines or imports this operator, only code where there is ambiguity.

Even if “base is allowed to export new names”, it might not be a good idea and in the best interest of the users of the language.

And it does break importing code

```julia
julia> module Foo
       export sin
       sin(x) = 0
       end
Main.Foo

julia> using .Foo

julia> sin(2)
WARNING: both Foo and Base export "sin"; uses of it in module Main must be qualified
ERROR: UndefVarError: `sin` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [January 24, 2024, 10:15am UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/6 "2024-01-24T10:15:26Z")

</div>

That’s the ambiguous case. The unambiguous case is

```julia
julia> module Foo
              export sin
              sin(x) = 0
              end
Main.Foo

julia> using .Foo: sin

julia> sin(2)
0

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [January 24, 2024, 2:27pm UTC](https://discourse.julialang.org/t/how-about-direct-sum-as-vector-attach-operator/109182/7 "2024-01-24T14:27:04Z")

</div>

> **[GitHub - tlienart/PlusPlus.jl: ++ as array vcat and string concat](https://github.com/tlienart/PlusPlus.jl)**
>
> ++ as array vcat and string concat. Contribute to tlienart/PlusPlus.jl development by creating an account on GitHub.
