# A .= circshift(b, shfts) and circshift!(a, b, shfts)

**URL:** <https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887>\
**Category:** New to Julia\
**Created:** [August 5, 2021, 1:07pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887 "2021-08-05T13:07:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:07pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/1 "2021-08-05T13:07:59Z")

</div>

Hi all,

I’m learning Julia by reimplementing some of my numerical algorithms.

As far as I understand from the manual and language design, the following two operations should be equivalent:

```julia
a .= circshift(b, shifts)
    0.422910 seconds (13 allocations: 762.940 MiB)

```

and

```julia
circshift!(a, b, shifts)
    0.164945 seconds (11 allocations: 432 bytes)

```

However it looks that they are executed differently and memcpy takes place in `.=` variant. Am I missing something, or is it a bug\language inconsistency?

Thanks.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 5, 2021, 1:10pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/2 "2021-08-05T13:10:27Z")

</div>

The first version results in an allocated array which entries are then in-place assigned to `a`. Therefore there is an allocated array somewhere within the `circshift` function which consumes memory.

---

<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 5, 2021, 1:13pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/3 "2021-08-05T13:13:05Z")

</div>

Sort of rephrasing what @roflmaostc pointed out:

`circshift` doesn’t know that there will be an in-place assignment afterwards (i.e. it can not be “fused”).

`circshift!`, however, does know that there is already preallocated memory which can be used.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [August 5, 2021, 1:15pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/4 "2021-08-05T13:15:05Z")

</div>

You can use `ShiftedArrays.circshift` for a lazy, non-allocating version of `circshift` (see [docs](https://juliaarrays.github.io/ShiftedArrays.jl/latest/#Shifting-the-data-circularly)):

```julia
julia> using ShiftedArrays, BenchmarkTools

julia> a, b = rand(100), rand(100);

julia> @btime $a .= ShiftedArrays.circshift($b, 1)
  128.619 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:15pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/5 "2021-08-05T13:15:20Z")

</div>

> [@roflmaostc](#):
>
> e within the `circshift` f

Thank for replies. I understand what is going on. Standard `circshift` has to create a buffer. But here [https://docs.julialang.org/en/v1/manual/functions/#man-vectorized](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized) it is stated that `X .= ...` allows to preallocate output. Which is not followed for `circshift`. Does output preallocation works only for trivial math?

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:16pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/6 "2021-08-05T13:16:17Z")

</div>

do you know if these view abstractions will work efficiently with ffts?

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 5, 2021, 1:18pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/7 "2021-08-05T13:18:58Z")

</div>

In `ffts` the output will be indeed a `ShiftedArrays` view and hence that in-place update should work.  
However, `fft` itself allocates intermediate arrays.  
There is also `ffts! `

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:19pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/8 "2021-08-05T13:19:35Z")

</div>

See my previous reply. But in this case, what is the whole idea of `X.=...` syntax? It seems to be trivial that if two implementation are given, e.g:  
`f(x)`  
and  
`f!(out, x)`  
then `z.=f(x)` can be evaluated as `f!(z, x)`?

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:22pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/9 "2021-08-05T13:22:55Z")

</div>

“`ffts!`” ? Did you mean inplace `fft` or efficient `fft` with shift? I couldn’t find it in AbstractFFTs.jl

---

<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 5, 2021, 1:24pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/10 "2021-08-05T13:24:50Z")

</div>

The dot essentially means “element-wise”. So `z .= ...` means write `...` element-wise into x (think of a for loop). So, a typical use case is something like `z .= f.(x)` where the operations “apply f element-wise to x” and “assign the result of f.(x) element-wise to z” get **fused** into a single “for loop” so to avoid a temporary allocation for `f.(x)`.

If you don’t do element-wise applications, like you do with `circshift`, the dot syntax doesn’t help much (as you’ve seen in your first case). That’s why dedicated in-place functions like `circshift!` exist.

Hope that clears things up a bit?

---

<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 5, 2021, 1:30pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/11 "2021-08-05T13:30:12Z")

</div>

It might be instructive to compare and understand the difference between

```julia
x .= a .+ b # x .= f.(x)

```

and

```julia
x .= a + b # x .= f(x) (no broadcasting of `f`)

```

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:30pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/12 "2021-08-05T13:30:13Z")

</div>

Ok, so `x .=` is intended to work only for trivial element-wise operations only? I.e. in your example, if I had a manually vectorized function `f(x)` then  
`z .= f(x)`  
would copy data, but autovectorized  
`z .= f.(x)`  
would do everything inplace?

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 5, 2021, 1:33pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/13 "2021-08-05T13:33:36Z")

</div>

Oh sorry, I thought by “ffts” you are referring to a package I maintain 😃

Look, here at [FourierTools.jl](https://bionanoimaging.github.io/FourierTools.jl/dev/helpers/#FourierTools.ffts!)

---

<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 5, 2021, 1:34pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/14 "2021-08-05T13:34:48Z")

</div>

The thing is that, in contrast to MATLAB (as far as I remember), you wouldn’t want to define a vectorized version of `f`, say `f(y::Matrix{Float64})`, but only a scalar variant `f(x::Float64)` to then simply use `f.(y)`.

Example:

```julia
# Let's say we want to compute the `exp(sin(x))` of all elements of a matrix
f(x::Float64) = exp(sin(x))

M = rand(10,10) # our matrix
R = zeros(10,10) # preallocated matrix to store the result

R .= f.(M)

```

---

<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 5, 2021, 1:39pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/15 "2021-08-05T13:39:13Z")

</div>

> [@ihgalq](#):
>
> Ok, so `x .=` is intended to work only for trivial element-wise operations only?

Well, whatever you mean by “trivial” 😄

But yes, there is no generic magical way in Julia (or any other language?) to autogenerate in-place versions of “non-trivial” matrix functions.

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:39pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/16 "2021-08-05T13:39:22Z")

</div>

The thing is, in my code I “vectorize” over matrices. But thanks for your help! I think I see the logic behind the syntax. Only comment I have, is that docs ulitmately state that `.=` allows to preallocate input, but this mechanism is actually very limited, as we just discussed.

If I may ask another question. Is there any standard macro that can translate `z .= f(x)` to `f!(z, x)` ? It would be very helpful for algorithm readability.

---

<div class="post-metadata">

**Author:** ![ihgalq](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@ihgalq](https://discourse.julialang.org/u/ihgalq)\
**Post date:** [August 5, 2021, 1:41pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/17 "2021-08-05T13:41:45Z")

</div>

Very cool, thanks for the reference. Are you planning to add inplace `ft!` ? That’s exactly what I would need as well :))

---

<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 5, 2021, 1:42pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/18 "2021-08-05T13:42:33Z")

</div>

> [@ihgalq](#):
>
> The thing is, in my code I “vectorize” over matrices.

Well, either use dot-broadcasting then or define in-place versions of your functions. But maybe it isn’t even necessary to “vectorize over matrices”? Perhaps a simple loop will do as well? (I don’t know. Depends on context of course.)

> [@ihgalq](#):
>
> Only comment I have, is that docs ulitmately state that `.=` allows to preallocate input,

`.=` doesn’t preallocate anything. It writes into preallocated memory if you will. Where is this mentioned in the docs? Can you point out the paragraph / line?

---

<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 5, 2021, 1:44pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/19 "2021-08-05T13:44:01Z")

</div>

> [@ihgalq](#):
>
> Is there any standard macro that can translate `z .= f(x)` to `f!(z, x)` ?

Well, as we’ve tried to explain above, these two things do very different operations, so why would you want a macro to translate one to the other? Also, not for every `f` there is an in-place pendant `f!`.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 5, 2021, 1:44pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/20 "2021-08-05T13:44:27Z")

</div>

`ft!` is not possible since `fft! ` can’t act in place on a ShiftedArrays view. Hence FFTW.jl needs to collect the array.  
The performance drawback is usually not that critical but if you run out of memory that’s of course an issue.

[Next page](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887.md?page=2)
