# Vectorizing negation?

**URL:** https://discourse.julialang.org/t/vectorizing-negation/77721
**Category:** New to Julia
**Tags:** question, numbers, numerics
**Created:** [March 10, 2022, 10:11pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721 "2022-03-10T22:11:29Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jecs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jecs/32/1781_2.png) [@jecs](https://discourse.julialang.org/u/jecs)
#### Post date: [March 10, 2022, 10:11pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/1 "2022-03-10T22:11:29Z")

</div>

Hi, I was wondering if it’s possible at all to negate elements in a collection using vectorization. So something like `-.(1,2,3)`. I tried this in Julia, but it doesn’t seem to be accepted syntax.

I could use `-1 .* (1,2,3)` but that is rather ugly, no? Is there a better way?

---

<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: [March 10, 2022, 10:17pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/2 "2022-03-10T22:17:20Z")

</div>

```julia
julia> v = (1,2,3)
(1, 2, 3)

julia> .-v
(-1, -2, -3)

julia> (-).(v)
(-1, -2, -3)

```

---

<div class="post-metadata">

### Author: ![jecs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jecs/32/1781_2.png) [@jecs](https://discourse.julialang.org/u/jecs)
#### Post date: [March 10, 2022, 10:18pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/3 "2022-03-10T22:18:08Z")

</div>

Oh, my. Thank you!

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 10, 2022, 10:18pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/4 "2022-03-10T22:18:41Z")

</div>

Funny parser

```julia
julia> 0.-(1,2,3)
ERROR: syntax: invalid syntax "0.-"; add space(s) to clarify
Stacktrace:
 [1] top-level scope
   @ none:1

julia> 0 .-(1,2,3)
(-1, -2, -3)

```

Edit: irrelevant, obviously 0. is parsed as floating point

---

<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: [March 10, 2022, 10:20pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/5 "2022-03-10T22:20:11Z")

</div>

The trick is that for some reason in the murky past, we decided that `a +. b` was weird looking and it was better to write `a .+ b`, so now all ‘operators’ need a dot in front of them instead of behind them.

```julia
julia> √.v
ERROR: syntax: invalid identifier name "."
Stacktrace:
 [1] top-level scope
   @ none:1

julia> .√v
(1.0, 1.4142135623730951, 1.7320508075688772)

```

However, if you wrap an operator in parens, or assign it to a variable, then it is like any other callable value. E.g.

```julia
julia> neg = -
- (generic function with 214 methods)

julia> neg.(v)
(-1, -2, -3)

```

---

<div class="post-metadata">

### Author: ![jecs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jecs/32/1781_2.png) [@jecs](https://discourse.julialang.org/u/jecs)
#### Post date: [March 10, 2022, 10:21pm UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/6 "2022-03-10T22:21:30Z")

</div>

Oh, I see. So it’s a by-product of the convention for dot-syntax vectorization that was adopted. Thank you for the detailed explanation.

---

<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 11, 2022, 12:13am UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/7 "2022-03-11T00:13:35Z")

</div>

> [@Mason](#):
>
> The trick is that for some reason in the murky past, we decided that `a +. b` was weird looking and it was better to write `a .+ b` , so now all ‘operators’ need a dot in front of them instead of behind them.

The `.+` style comes from Matlab and pre-dates Julia’s `f.(x)` syntax (which I like to think of as `.(`, dotting the “function-call operator” `(`).

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [March 11, 2022, 1:05am UTC](https://discourse.julialang.org/t/vectorizing-negation/77721/8 "2022-03-11T01:05:31Z")

</div>

> [@Mason](#):
>
> The trick is that for some reason in the murky past, we decided that `a +. b` was weird looking and it was better to write `a .+ b` , so now all ‘operators’ need a dot in front of them instead of behind them.

I am almost sure that there was an ambiguity problem or something related that forced the dot side for function and operators to be opposite, not just “weird looking”. 😁
