# Understand vectorized dot operators

**URL:** <https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388>\
**Category:** New to Julia\
**Tags:** broadcast, broadcasting\
**Created:** [March 21, 2023, 12:09pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388 "2023-03-21T12:09:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed-desoki](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@ahmed-desoki](https://discourse.julialang.org/u/ahmed-desoki)\
**Post date:** [March 21, 2023, 12:09pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/1 "2023-03-21T12:09:48Z")

</div>

Dear folks

I’m an old Matlab user who is trying to switch to Julia. In Matlab, I use array (element-wise) arithmetic a lot.

When I tried to make element-wise arithmetic using Julia, I noticed the following strange behavior:

- Adding a scalar to a vector needs dot “.+”, while multiplying them works fine without the dot!!!
- Multiplying two vectors needs dot “.\*”, while adding them works fine without the dot!!!

I got confused!! Please help me understand when I need the dot and when I don’t need it to make array arithmetic.

One more thing, in Matlab, array arithmetic works with compatible size arrays. Is this available in Julia?

Thanks and regards.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 21, 2023, 12:17pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/2 "2023-03-21T12:17:48Z")

</div>

That’s just maths:

> **[Why can't you add a scalar to a vector?](https://byjus.com/question-answer/why-cant-you-add-a-scalar-to-a-vector/)**
>
> Why can't you add a scalar to a vector?

> **[Multiplication Of Vectors by a Scalar Quantity with examples](https://byjus.com/maths/multiplication-of-vectors-with-scalar/)**
>
> Multiplication of Vectors can be of two types Scalar and Vector Multiplication. The scalar quantity results in change in only magnitude not direction

Also, multiplying vectors does not need a dot, but it needs dimensions to be consistent to either get an inner (dot) product or an outer product:

```julia
julia> v = rand(3);

julia> v * v
ERROR: MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})

julia> v' * v
0.24979883493928956

julia> v * v'
3×3 Matrix{Float64}:
 2.91655e-5 0.00266555 0.000423667
 0.00266555 0.243615 0.0387206
 0.000423667 0.0387206 0.00615433

```

---

<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 21, 2023, 12:27pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/3 "2023-03-21T12:27:10Z")

</div>

> [@ahmed-desoki](#):
>
> Adding a scalar to a vector needs dot “.+”, while multiplying them works fine without the dot!!!

Standard linear-algebra operations (e.g. adding/subtracting vectors or other arrays of the same size or multiplying them by scalars, or performing valid matrix/vector multiplications) does not use a dot. Any other elementwise operation requires a dot.

---

<div class="post-metadata">

**Author:** ![ahmed-desoki](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@ahmed-desoki](https://discourse.julialang.org/u/ahmed-desoki)\
**Post date:** [March 21, 2023, 12:38pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/4 "2023-03-21T12:38:07Z")

</div>

Thanks for your answer, but please help me understand the rule. Matlab rules are so clear.

Why the operation works with addition but fails with multiplication? Why the rule applies to one operator and not on the other??

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 21, 2023, 12:48pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/5 "2023-03-21T12:48:28Z")

</div>

Welcome to the Julia forum!

I feel the Julia rules are clearer (and I also once came from Matlab), because you nearly never “accidentally broadcast” which happened to me in Matlab often and I suddenly hat matrices where I just expected vectors. So maybe to extend on Stevens answer.

Assume you have a vector like

```julia
x = zeros(3)
y = ones(3)

```

they are of same size so we can easily add them with `x+y` but you can even say, please apply plus to both vectors element wise by saying `x .+ y` that is – in the end – the same

for adding a scalar, say `a=1` the types are _not_ equal. One is a number one is a vector. So then we have to clarify what we mean by number+vector (which per se is not defined. But if the think of `.+` as “please add elementwise” `a .+ x ` works fine (please add `a` to each element of `x`).

For multiplication, scaling a vector by a factor is well-defined – vectors form a vector space so that’s why `a*x` works, but similar to adding two vectors (or the .+ for scalars) you can also say “please scale every element of x by a” that is `a .* x`. The thing here is that scaling a vector (ax) is defined as scaling every element of x by a, so that is why that works both the same again (similar to adding vectors).

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 21, 2023, 12:51pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/6 "2023-03-21T12:51:00Z")

</div>

I thought that’s what my (and Steven’s) answer had done - when an operation is meaningfully defined mathematically it works, if it isn’t, it doesn’t.

As explained in the first link I shared, adding a scalar to a vector is not a well defined operation. Therefore, `scalar + vector` will aise a method error. If what you meant is “actually add the scalar to each element of the vector” then you need to be explicit and broadcast the addition by using `.+`.

Adding two vectors of the same length is a well defined operation, so it will work without using an elementwise addition operator (although in this case the result is the same with or without broadcasting).

Vector multiplication is again a well defined concept, but you need to make sure the dimensions are right. You can think of Julia vectors as column vectors, so `v = rand(3)` will create a 3-by-1 vector. You therefore cannot do `v * v` as that would mean multipling (3 \times 1) \times (3 \times 1) which does not work. You can do `v' * v` which multiplies (1 \times 3) \times (3 \times 1) to yield a (1 \times 1) (scalar) output, and you can do `v * v'` which gives a (3 \times 1) \times (1 \times 3) = (3 \times 3) matrix.

If you actually meant “multiply each element of `v` by itself” you again need to be explicit and say `v .* v`

---

<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 21, 2023, 2:10pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/7 "2023-03-21T14:10:48Z")

</div>

> [@nilshg](#):
>
> I thought that’s what my (and Steven’s) answer had done - when an operation is meaningfully defined mathematically it works, if it isn’t, it doesn’t.
> 
> As explained in the first link I shared, adding a scalar to a vector is not a well defined operation.

I wouldn’t phrase it this way. Julia’s `vector .+ scalar` is perfectly well defined and meaningful mathematically, and [symbols can mean whatever we want them to mean](https://commons.wikimedia.org/wiki/File:Humpty_Dumpty_Tenniel-When_I_use_a_word.png). It’s just not an operation of _linear algebra_, and Julia reserves `+` for the ordinary algebraic meaning.

Julia used to be more Matlab-like and supported implicit vectorization/broadcasting of many functions (e.g. `vector + scalar`, `sqrt(vector)`, and so forth). However, this was changed when we got the `f.(x)` “dot syntax” to broadcast any function, since it turns out that this gave us big advantages by being syntactically explicit, as explained in [this blog post](https://julialang.org/blog/2017/01/moredots/).

Interestingly, Julia originally had `vector + scalar`, then it was removed ([julia#5810](https://github.com/JuliaLang/julia/pull/5810)), then it was added back ([julia#7226](https://github.com/JuliaLang/julia/pull/7226)), and then finally it was removed again ([julia#22932](https://github.com/JuliaLang/julia/pull/22932)) once we had fusing `f.(x)` syntax for all functions. See the linked issues for long discussions of the pros/cons — this is not something we decided on a whim, but after extensive debate and trial-and-error.

See also [Idea Julia Lite or 'Juliette'? - #55 by stevengj](https://discourse.julialang.org/t/idea-julia-lite-or-juliette/34076/55)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 21, 2023, 3:19pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/8 "2023-03-21T15:19:15Z")

</div>

> [@ahmed-desoki](#):
>
> Matlab rules are so clear.

I’m not so sure about that (Matlab code):

```julia
>> x = rand(3,1); y = rand(3,1);

>> x + y
ans =
    1.2434
    0.7045
    1.9281

>> 2 * x
ans =
    0.5570
    1.0938
    1.9150

>> 2 + x
ans =
    2.2785
    2.5469
    2.9575

```

These all work. But

```julia
>> x * y
Error using * 
Incorrect dimensions for matrix multiplication.

```

You must use `.*` for this, but `scalar + vector` is apparently fine.

So `vector + vector` and `scalar * vector` works in Julia, because they work in linear algebra. They _also_ work with `.+` and `.*`, but that’s beside the point. Matlab, on the other hand, allows `scalar + vector`, but _not_ `vector * vector`. I don’t see how that is clearer.

> [@ahmed-desoki](#):
>
> One more thing, in Matlab, array arithmetic works with compatible size arrays. Is this available in Julia?

Yes, if you use broadcasting:

```julia
julia> x = rand(3); y = rand(3);

julia> x .+ y'
3×3 Matrix{Float64}:
 0.906503 1.37568 1.38545
 0.726524 1.1957 1.20547
 1.01696 1.48613 1.49591

```

Matlab also allows this, as you say, but without the `.`

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [March 21, 2023, 3:25pm UTC](https://discourse.julialang.org/t/understand-vectorized-dot-operators/96388/9 "2023-03-21T15:25:10Z")

</div>

There are some additional answers here you might find useful.

> [@Broadcasting inconsistency between addition and multiplication](https://discourse.julialang.org/t/broadcasting-inconsistency-between-addition-and-multiplication/69435):
>
> There seems to be inconsistency in broadcasting. A = [1 2 ; 3 4] A\* 2 # Broadcasting works here A.\*2 # Broadcasting same as above A + 2 # Broadcasting fails here. If it works for \* then it should work for all operators A .+2 # Broadcasting works
