# 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:** 8\
**Page:** 2

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

</div>

Ok. Maybe we are talking a bit different matters. I understand loop fusion with dots, but my question was more about memory allocation. I can formulate it in the following way:

is there a way to preallocate `z` for a function call like  
`z=f(x)`? Lets say I can modify the code of `f`. And I assume `x` and `z` to be arrays.

---

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

</div>

Generally not, because `f` might call a function from a different library which might call another Library etc.

Sometimes functions expose the buffer to the user like `circshift! ` does.

---

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

</div>

> [@roflmaostc](#):
>
> because `f` might call a function from a different library which might call another Library etc.

so can inplace function `f!(z, x)`. Is there any nuances that I miss by assuming the following code translation to be 100% equivalent (assuming z is correctly preallocated in terms of `f!` call):  
` z = f(x)` —\> `f!(z, x)` ?

---

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

</div>

> [@ihgalq](#):
>
> Is there any nuances that I miss by assuming the following code translation to be 100% equivalent:  
> ` z .= f(x)` —\> `f!(z, x)` ?

You’ve already shown in your OP that they are not 100% equivalent!

Again, an example to try to clear things up:

```julia
f(a,b) = a + b 

a = rand(10)
b = rand(10)
z = zeros(10)

z .= f(a,b) # allocates the result for f(a,b) and then copies it element-wise to z.

# Throwing away the preallocated z and just naming the output of f(a,b) as z is faster here:
# z = f(a,b)

f!(z, a, b) = z .= a .+ b

f!(z,a,b) # this doesn't allocate but writes the result of the computation directly into z.

```

Here the timings:

```julia
julia> @btime $z .= f($a,$b);
  91.037 ns (1 allocation: 160 bytes)

julia> @btime $z = f($a,$b);
  75.565 ns (1 allocation: 160 bytes)

julia> @btime f!($z,$a,$b);
  23.057 ns (0 allocations: 0 bytes)

```

And as @roflmaostc has pointed out, while it is trivially possible here, it may be hard or impossible (due to library calls) to write a fully in-place function `f!(z,a,b)`. Doing so automatically is of course even harder / impossible.

---

<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, 2:04pm UTC](https://discourse.julialang.org/t/a-circshift-b-shfts-and-circshift-a-b-shfts/65887/25 "2021-08-05T14:04:38Z")

</div>

@crstnbr  
Sorry that’s not what I asked. Lets forget about vectorization with dots, as I already have got your examples and arguments. As I said, I assumed that `.=` is a general syntax for avoiding copying.

What I wanted to ask, is if there is any way to instead of writing `f!(z, x)` to write smth like `@coolmacro z=f(x)` or even better `z _= f(x)`?

---

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

</div>

> [@ihgalq](#):
>
> What I wanted to ask, is if there is any way to instead of writing `f!(z, x)` to write smth like `@coolmacro z=f(x)` or even better `z _= f(x)` ?

Well, you can of course define a macro `@coolmacro` that, syntactically, transforms `z = f(x)` into `f!(z,x)`. However, as pointed out by @roflmaostc and me above

1. There isn’t always an in-place variant `f!(z,x)` for a given `f`.
2. Generating such `f!(z,x)` might not even be possible, neither manually nor automatically. (Of course, it often is, as in the case of `circshift!`.)

(Also, what about functions with more than one “buffer”, i.e. f!(z1, z2, x)?)

So the answer is, no, there is no `@coolmacro` in (standard) Julia that does what you ask for because of the points above.

---

<div class="post-metadata">

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

</div>

For inspiration you might look at [InplaceOps.jl](https://github.com/simonbyrne/InplaceOps.jl). This only changes functions where it knows that the `f!` version exists. (IIRC there used to be a larger zoo of LinearAlgebra functions which it treated, many of which became `mul!`, and broadcasting was less clever.)

---

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

</div>

Thank you, that’s exactly what I needed to know!

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