# Broadcasting of minus as prefix operator

**URL:** https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353
**Category:** General Usage
**Tags:** question
**Created:** [August 31, 2018, 7:42am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353 "2018-08-31T07:42:04Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [August 31, 2018, 7:42am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/1 "2018-08-31T07:42:04Z")

</div>

Hello, I have the following technical question: when using - as prefix operator, does it get broadcasted as it would be an operator with a dot, i.e. is this

```julia
x = 2
y = [1,2,3]
z = -x .+ y

```

equivalent to

```julia
x = 2
y = [1,2,3]
z = -1 .* x .+ y

```

because it would matter in terms of performance.

At least the `@code_lowered` after defining a function is not the same:

```julia
f(x,y) = -x .+ y
CodeInfo(
91 1 ─ %1 = Base.Broadcast.materialize │
   │ %2 = Base.Broadcast.broadcasted │
   │ %3 = -x │
   │ %4 = (%2)(Main.:+, %3, y) │
   │ %5 = (%1)(%4) │
   └── return %5 │
)

```

```julia
f(x,y) = -1 .* x .+ y
CodeInfo(
91 1 ─ %1 = Base.Broadcast.materialize │
   │ %2 = Base.Broadcast.broadcasted │
   │ %3 = Base.Broadcast.broadcasted │
   │ %4 = (%3)(Main.:*, -1, x) │
   │ %5 = (%2)(Main.:+, %4, y) │
   │ %6 = (%1)(%5) │
   └── return %6    

```

Thanks.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 31, 2018, 7:46am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/2 "2018-08-31T07:46:28Z")

</div>

The Unary `-` operator has [higher precedence](https://docs.julialang.org/en/stable/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1) than the addition operator, so yes.

Performance wise, they’re virtually equivalent:

```julia
julia> using BenchmarkTools

julia> f(x,y) = -x .+ y
f (generic function with 1 method)

julia> g(x,y) = -1 .* x .+ y
g (generic function with 1 method)

julia> @btime f($x, $y)
  32.161 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 -1
  0
  1

julia> @btime g($x, $y)
  32.193 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 -1
  0
  1

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [August 31, 2018, 7:46am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/3 "2018-08-31T07:46:55Z")

</div>

It is equivalent to `z = (-x) .+ y`. At least that’s what I take from `@code_lowered`:

```julia
julia> f() = z = -x .+ y
f (generic function with 1 method)

julia> @code_lowered f()
CodeInfo(
1 1 ─ %1 = Base.Broadcast.materialize │
  │ %2 = Base.Broadcast.broadcasted │
  │ %3 = -Main.x │
  │ %4 = (%2)(Main.:+, %3, Main.y) │
  │ %5 = (%1)(%4) │
  │ z = %5 │
  └── return %5 │
)

```

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [August 31, 2018, 7:53am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/4 "2018-08-31T07:53:45Z")

</div>

Sorry, I was a little bit misleading in my example, I wanted `x` to be an array as well. Now I have the test and it is **not** the same speed !!!

```julia
julia> using BenchmarkTools

julia> x = [4,5,6];

julia> y = [1,2,3];

julia> f(x,y) = -1 .* x .+ y ;

julia> g(x,y) = -x .+ y ;

julia> @code_lowered f(x,y)
CodeInfo(
1 1 ─ %1 = Base.Broadcast.materialize │
  │ %2 = Base.Broadcast.broadcasted │
  │ %3 = Base.Broadcast.broadcasted │
  │ %4 = (%3)(Main.:*, -1, x) │
  │ %5 = (%2)(Main.:+, %4, y) │
  │ %6 = (%1)(%5) │
  └── return %6 │
)

julia> @code_lowered g(x,y)
CodeInfo(
1 1 ─ %1 = Base.Broadcast.materialize │
  │ %2 = Base.Broadcast.broadcasted │
  │ %3 = -x │
  │ %4 = (%2)(Main.:+, %3, y) │
  │ %5 = (%1)(%4) │
  └── return %5 │
)

julia> @btime f($x,$y)
  107.821 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 -3
 -3
 -3

julia> @btime g($x,$y)
  201.928 ns (2 allocations: 224 bytes)
3-element Array{Int64,1}:
 -3
 -3
 -3

```

This is bad, I would say. Because `-x` is much more convenient than `-1 .* x` when dealing with longer expressions.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 31, 2018, 7:58am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/5 "2018-08-31T07:58:14Z")

</div>

If `x` is an array, just dot the `-` as well:

```julia
julia> f(x,y) = .-x .+ y
f (generic function with 1 method)

julia> g(x,y) = -1 .* x .+ y
g (generic function with 1 method)

julia> @btime f($x,$y)
  39.354 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 -3
 -3
 -3

julia> @btime g($x,$y)
  39.315 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 -3
 -3
 -3

```

The reason it’s gotten slower if you don’t dot it as well is because if an expression has all dots, the compiler can fuse all the broadcasts into a single call, greatly reducing overhead. The same can be achieved with `@.` which dots every operator/function call:

```julia
julia> h(x,y) = @. -x + y
h (generic function with 1 method)

julia> @btime h($x,$y)
  39.315 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 -3
 -3
 -3

```

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [August 31, 2018, 8:00am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/6 "2018-08-31T08:00:49Z")

</div>

Thanks, I accidentally tried `-.x .+ y` before writing this post and this did not worked, of cause.

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [August 31, 2018, 8:08am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/7 "2018-08-31T08:08:26Z")

</div>

> [@Sukera](#):
>
> The reason it’s gotten slower if you don’t dot it as well is because if an expression has all dots, the compiler can fuse all the broadcasts into a single call, greatly reducing overhead. The same can be achieved with `@.` which dots every operator/function call:

Yes, I know, just did not realiesed that using `.-` is the right way for writing it, because usually for a function you use `f.()` so I thougt it should be `-.x`.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 31, 2018, 8:11am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/8 "2018-08-31T08:11:56Z")

</div>

`-` is an operator though, and those usually go with the dot beforehand:

```julia
julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b = [3,4,5]
3-element Array{Int64,1}:
 3
 4
 5

julia> a .+ b
3-element Array{Int64,1}:
 4
 6
 8

```

The other way around is even in “function syntax” wrong:

```julia
julia> .-(a)
3-element Array{Int64,1}:
 -1
 -2
 -3

julia> -.(a)
ERROR: syntax: numeric constant "-." cannot be implicitly multiplied because it ends with "."

```

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [August 31, 2018, 8:12am UTC](https://discourse.julialang.org/t/broadcasting-of-minus-as-prefix-operator/14353/9 "2018-08-31T08:12:29Z")

</div>

Yeah, will remember that in future 😉
