Iterating an action over fields of tuples with similar structure

Dear all,
I’m trying to find a way to repeat an operation over the fields of tuples, which have consistent size.
For example, let’s consider the sum A = B + C, but applied to each field of a tuple.

    size1 = (10, 10)
    size2 = (11, 11)

    a  = (zeros(size1), zeros(size2))
    b  = ( ones(size1),  ones(size2))
    c  = ( ones(size1),  ones(size2))

    # Two lines
    @. a[1] = b[1] + c[1] 
    @. a[2] = b[2] + c[2] 

I would like to replace the two line statement by a single line. Is it possible to write a macro that could used to that end?

@my_macro a = b + c 

Thanks for the hints!

1 Like

I think this is one of the (comparatively) rare times that map is useful in Julia

a = map(.+, b, c)

seems to do what you want

actually, since matrices add element-wise, just plain a = b .+ c works fine. Are you doing something more complex?

1 Like

Thanks for this one! Haven’t thought about a map.
Yes, I do more complex things than .+, I just took this for purpose of a MWE.
There could be things like a = b.*c - d +e and things with shifted indices (finite difference operations).

1 Like

Would this not just be

a = @. b +c 


 a = @. b*c-d+e

Hmmm nice ones too! Is there a way such to avoid that a gets allocated? Could something like a = @. b +c be made in-place?