# Broadcasting over a struct with two arrays

**URL:** https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136
**Category:** General Usage
**Tags:** broadcast
**Created:** [July 8, 2019, 12:50pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136 "2019-07-08T12:50:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [July 8, 2019, 12:50pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/1 "2019-07-08T12:50:09Z")

</div>

Hi!

I have a type that looks like

```julia
struct A{X1<:AbstractArray, X2<:AbstractArray}
    x1::X1
    x2::X2
end
a = A([1, 2], [1 2; 3 4])
b = A([1, 4], [-1 2; -3 4])

```

and I’d like to extend broadcasting such that, for example, `a .= b .- a` is equivalent to

```julia
a.x1 .= b.x1 .- a.x1
a.x2 .= b.x2 .- a.x2

```

Is there any example I can follow to do this? I don’t need to combine broadcasting of objects of type `A` with objects of other types.

---

<div class="post-metadata">

### Author: ![francesco.alemanno](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@francesco.alemanno](https://discourse.julialang.org/u/francesco.alemanno)
#### Post date: [July 8, 2019, 12:55pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/2 "2019-07-08T12:55:14Z")

</div>

you should switch to “mutable struct” then.

---

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [July 8, 2019, 12:56pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/3 "2019-07-08T12:56:46Z")

</div>

I don’t need to change `x1` or `x2`, only their content.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 8, 2019, 1:02pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/4 "2019-07-08T13:02:54Z")

</div>

Have you seen [https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting-1](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting-1)?

---

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [July 8, 2019, 1:09pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/5 "2019-07-08T13:09:16Z")

</div>

Yes, I’ve seen that and it was very helpful when I just needed to forward broadcasting to a single wrapped array (although even then I had to look at the code of `StaticArrays` to make it work well because for example it does not mention that it’s a good idea to add a new method to `Base.dataids` like here: [StaticArrays.jl/SizedArray.jl at b150a8c0eeca4b886a02f2ffb16406653a6aa054 · JuliaArrays/StaticArrays.jl · GitHub](https://github.com/JuliaArrays/StaticArrays.jl/blob/b150a8c0eeca4b886a02f2ffb16406653a6aa054/src/SizedArray.jl#L75) ). That’s why I was hoping to just work using a full existing implementation.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [July 8, 2019, 3:39pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/6 "2019-07-08T15:39:06Z")

</div>

Check out

> [@Customizing Broadcasting Recursively for Collections of Arrays](https://discourse.julialang.org/t/customizing-broadcasting-recursively-for-collections-of-arrays/25724):
>
> I previously asked [this question](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459/4) and am aware of the section in the manual about customizing broadcasting. However, rather than defining a new type which behaves like a single array, I would like to define broadcasting recursively for types which contain several arrays (or types which are themselves collections of arrays). That is, imagine we have struct A{T so that broadcasting is already defined for T} a::T b::T end If x::A, y::A, calculate z = x .+ y by calling z.a = x.a .+ y.a (etc.). …

---

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [July 8, 2019, 3:47pm UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/7 "2019-07-08T15:47:18Z")

</div>

Thanks, I didn’t notice that thread. I’ll try something out.

---

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [May 4, 2021, 5:50am UTC](https://discourse.julialang.org/t/broadcasting-over-a-struct-with-two-arrays/26136/8 "2021-05-04T05:50:55Z")

</div>

For future reference: I finally managed to do what I wanted to do here: [Copy and broadcasting for ProductRepr by mateuszbaran · Pull Request #336 · JuliaManifolds/Manifolds.jl · GitHub](https://github.com/JuliaManifolds/Manifolds.jl/pull/336/files) and it works well enough for me. I hope it’s a good reason to post in this (very old) thread 😅 .
