# Difference between 'x.=y' and 'x=y'

**URL:** <https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 5, 2024, 2:37am UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366 "2024-01-05T02:37:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 5, 2024, 2:37am UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/1 "2024-01-05T02:37:52Z")

</div>

I see codes like this

```julia
x.= x./maximum(abs.(x))

```

Here x is a vector. Personally, I would write

```julia
x = x./maximum(abs.(x))

```

Is there any significant difference between the two?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 5, 2024, 2:48am UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/2 "2024-01-05T02:48:07Z")

</div>

First one is broadcasted/elementwise assignment, which mutates the instance assigned to `x`, second one reassigns `x` entirely. You can use `Meta.@lower` to see how the expressions are lowered, and it tells you a bit of what is happening.  
Before that, you should read a pretty good explanation of dots here:

> **[More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)**
>
> More Dots: Syntactic Loop Fusion in Julia | After a lengthy design process (https://github.com/JuliaLang/julia/issues/8450) and preliminary foundations in Julia 0.5 (/blog/2016-10-11-julia-0.5-highlights#vectorized\_function\_calls), Julia 0.6 includes...

And this gets into improved design and some of the implementation, though if you don’t have to care too much about it, you could just take away `Meta.@lower` for seeing the broadcast loops, and the fact that broadcasting can be customized away from the default of instantiating a full `Array` when some types allow for it, like elementwise addition of ranges.

> **[Extensible broadcast fusion](https://julialang.org/blog/2018/05/extensible-broadcast-fusion/)**
>
> Extensible broadcast fusion | Julia version 0.7 brings with it an exciting new feature: the ability to customize broadcast...

---

<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:** [January 5, 2024, 3:05am UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/3 "2024-01-05T03:05:25Z")

</div>

Just to make it very clear to future readers that are novices in the language:

They are two completely different things.

The first ( `x.= ...`) changes each element of the already existing object denoted by `x` one-by-one with what is being computed. If you use multiple elements of `x` to compute each new value in `x` and you need to compute the new values solely based in the old values (without the new ones mixed in) then you may end up with bugs.

The second (`x = ...`) just creates a completely new object and starts using the name `x` for it.

EDIT: as others pointed out, if both are correct for you (i.e., there is no problem in overwriting the object denoted by `x` during the computation) then `x .= ...` is often preferable because it allocates less memory (reuses the existing object) but I would urge a novice to not care about premature optimization in this context (do it only if it is safe AND memory allocation shows to be a bottleneck).

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 5, 2024, 5:50am UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/4 "2024-01-05T05:50:26Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> Is there any significant difference between the two?

It’s like altering your laptop to add the features that you want, versus buying a new model with those features inbuilt. Both get you the same result, but one is significantly more resource intensive than the other.

---

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 5, 2024, 6:23am UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/5 "2024-01-05T06:23:23Z")

</div>

> [@Benny](#):
>
> Meta.@lower

```julia
julia> Meta.@lower x = f1.(x)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Base.broadcasted(f1, x)
│ %2 = Base.materialize(%1)
│ %3 = Core.get_binding_type(Main, :x)
│ %4 = Base.convert(%3, %2)
│ %5 = Core.typeassert(%4, %3)
│ x = %5
└── return %2
))))

julia> Meta.@lower x .= f1.(x)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = x
│ %2 = Base.broadcasted(f1, x)
│ %3 = Base.materialize!(%1, %2)
└── return %3
))))

```

---

<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:** [January 5, 2024, 2:12pm UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/6 "2024-01-05T14:12:50Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> `x.= x./maximum(abs.(x))`

More dots aren’t _always_ better. Keep then one in `.=`, but change the call to maximum like this:

```julia
x .= x ./ maximum(abs, x)

```

`maximum(abs.(x))` will first create a temporary array and then find the max of that array, while `maximum(abs, x)` does not.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 5, 2024, 7:12pm UTC](https://discourse.julialang.org/t/difference-between-x-y-and-x-y/108366/7 "2024-01-05T19:12:36Z")

</div>

Could also be reduced to a nice single dot 🙂 :

```julia
x ./= maximum(abs, x)

```
