# Vectorizing negation?

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?

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

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

julia> (-).(v)
(-1, -2, -3)
``````
8 Likes

Oh, my. Thank you!

Funny parser

``````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

1 Like

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> √.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> neg = -
- (generic function with 214 methods)

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

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.

1 Like

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” `(`).

7 Likes

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”.

1 Like