Broadcasting with in-place changes

That works fine too:

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 scalarsas arguments. Call it on arrays withf.(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:

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.)

1 Like