# Broadcasting with in-place changes

**URL:** https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719
**Category:** New to Julia
**Tags:** broadcast, inplace
**Created:** [December 15, 2022, 10:58pm UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719 "2022-12-15T22:58:02Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Eveee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eveee/32/45034_2.png) [@Eveee](https://discourse.julialang.org/u/Eveee)
#### Post date: [December 15, 2022, 10:58pm UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/1 "2022-12-15T22:58:02Z")

</div>

Hi all,

I am learning broadcasting in julia. I would like to adapt one vector in-place by multiplying it with the contents of another vector, without creating a copy. I tried this:

```julia
function mymultiply!(a,b)
    a*=b
    return ;
end

a=[1,2,3,4]
b=[10,100,1000,10000]
mymultiply!.(a,b)
a

```

But this does not adapt a. Is this not passing through a pointer and is a new allocation made? How do I avoid this and keep it in-place?

---

<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: [December 15, 2022, 11:10pm UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/2 "2022-12-15T23:10:40Z")

</div>

> [@Eveee](#):
>
> I would like to adapt one vector in-place by multiplying it with the contents of another vector,

`a .*= b`

---

<div class="post-metadata">

### Author: ![Eveee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eveee/32/45034_2.png) [@Eveee](https://discourse.julialang.org/u/Eveee)
#### Post date: [December 15, 2022, 11:19pm UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/3 "2022-12-15T23:19:35Z")

</div>

I get the following error when trying that:

```julia
ERROR: MethodError: no method matching copyto!(::Int64, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Tuple{}, typeof(*), Tuple{Int64, Int64}})
Closest candidates are:
  copyto!(::AbstractArray, ::Base.Broadcast.Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}}) at broadcast.jl:916
  copyto!(::AbstractArray, ::Base.Broadcast.Broadcasted) at broadcast.jl:913
  copyto!(::AbstractArray, ::Any) at abstractarray.jl:898
Stacktrace:
 [1] materialize!
   @ .\broadcast.jl:871 [inlined]
 [2] materialize!
   @ .\broadcast.jl:868 [inlined]
 [3] mymultiply!(a::Int64, b::Int64)
   @ Main project.jl:88
 [4] _broadcast_getindex_evalf
   @ .\broadcast.jl:670 [inlined]
 [5] _broadcast_getindex
   @ .\broadcast.jl:643 [inlined]
 [6] getindex
   @ .\broadcast.jl:597 [inlined]
 [7] copy
   @ .\broadcast.jl:899 [inlined]
 [8] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(mymultiply!), Tuple{Vector{Int64}, Vector{Int64}}})
   @ Base.Broadcast .\broadcast.jl:860
 [9] top-level scope
   @ project.jl:94

```

---

<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: [December 15, 2022, 11:29pm UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/4 "2022-12-15T23:29:08Z")

</div>

> [@Eveee](#):
>
> I get the following error when trying that:

Not sure what you did, but:

```julia
julia> a=[1,2,3,4];

julia> b=[10,100,1000,10000];

julia> a .*= b;

julia> a
4-element Vector{Int64}:
    10
   200
  3000
 40000

```

---

<div class="post-metadata">

### Author: ![Eveee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eveee/32/45034_2.png) [@Eveee](https://discourse.julialang.org/u/Eveee)
#### Post date: [December 15, 2022, 11:38pm UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/5 "2022-12-15T23:38:27Z")

</div>

Oh yes thanks that works indeed! I tried adapting my function like this:

```julia
function mymultiply!(a,b)
    a.*=b
    return ;
end

```

Which resulted in the error.

The suggestion a.\* =b does work for something simple like multiplication, but if I want to do something more complicated for which I’d like to write my own function, could I adapt mymultiply to obtain the same behavior as a.\* =b?

---

<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: [December 16, 2022, 12:07am UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/6 "2022-12-16T00:07:51Z")

</div>

> [@Eveee](#):
>
> I tried adapting my function like this:

That works fine too:

```julia
julia> a=[1,2,3,4]; b=[10,100,1000,10000];

julia> function mymultiply!(a,b)
           a.*=b
           return ;
       end
mymultiply! (generic function with 1 method)

julia> mymultiply!(a,b)

julia> a
4-element Vector{Int64}:
    10
   200
  3000
 40000

```

I think what’s confusing you may be that you are _also_ trying to call `mymultiply!.(a,b)` with a dot. To use broadcasting, you generally want to do **one but not both** of:

1. Write a function `f(x,y)` that takes _arrays_ as arguments. _Inside_ `f(x,y)`, use as many broadcast operations as you want. You can even modify `x` or `y` in-place if you want (in which case it is conventional to name `f!` with a `!`).
2. Write a function `f(x,y)` that takes _scalars_`as arguments. Call it on arrays with`f.(a,b)`, and assign the results in-place with (for example) `a .= f.(a,b)`.

Do one or the other! For example, the 2nd strategy in this case would be:

```julia
function mymultiply(a,b)
    return a * b
end
a .= mymultiply.(a,b) # apply to arrays a,b, assigning result in-place to a

```

(You can’t do `a .*= b` in a function that takes _scalars_ as arguments, because scalars are immutable. And `a += b` is equivalent to `a = a + b` which does not mutate the argument `a`.)

---

<div class="post-metadata">

### Author: ![Eveee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eveee/32/45034_2.png) [@Eveee](https://discourse.julialang.org/u/Eveee)
#### Post date: [December 16, 2022, 12:15am UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/7 "2022-12-16T00:15:11Z")

</div>

That’s clear, thanks so much! In the second case, does this mean I am temporarily allocating memory to store a\*b before I assign it to a?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [December 16, 2022, 12:15am UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/8 "2022-12-16T00:15:59Z")

</div>

> **[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...

---

<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: [December 16, 2022, 12:17am UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/9 "2022-12-16T00:17:24Z")

</div>

> [@Eveee](#):
>
> In the second case, does this mean I am temporarily allocating memory to store a\*b before I assign it to a?

No. `a .= mymultiply.(a,b)` is exactly equivalent to `broadcast!(mymultiply, a, a,b)`, which is conceptually equivalent to:

```julia
for i in 1:length(a)
    a[i] = mymultiply(a[i], b[i])
end

```

which doesn’t allocate any arrays.

---

<div class="post-metadata">

### Author: ![Eveee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eveee/32/45034_2.png) [@Eveee](https://discourse.julialang.org/u/Eveee)
#### Post date: [December 16, 2022, 12:18am UTC](https://discourse.julialang.org/t/broadcasting-with-in-place-changes/91719/10 "2022-12-16T00:18:45Z")

</div>

Perfect, that resolves it all, thank you!
