# Broadcast with container for multidimensional array

**URL:** <https://discourse.julialang.org/t/broadcast-with-container-for-multidimensional-array/34825>\
**Category:** General Usage\
**Created:** [February 18, 2020, 7:45pm UTC](https://discourse.julialang.org/t/broadcast-with-container-for-multidimensional-array/34825 "2020-02-18T19:45:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxPauer](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@MaxPauer](https://discourse.julialang.org/u/MaxPauer)\
**Post date:** [February 18, 2020, 7:45pm UTC](https://discourse.julialang.org/t/broadcast-with-container-for-multidimensional-array/34825/1 "2020-02-18T19:45:59Z")

</div>

Hi,

I’ve got a container type for a multidimensional array, and I want to redirect the iterate function to the ‘inner’ array.  
This works fine, except it does not preserve dimensions when combined with a broadcast call.  
I’m probably missing something very basic here, but I can only seem to get it to broadcast linearly…

E.g.:

```julia
struct A
	arr::Array{Bool}
end

Base.iterate(a::A) = iterate(a.arr)
Base.iterate(a::A, state) = iterate(a.arr, state)
Base.length(a::A) = length(a.arr)

x = fill(false, 1,2,3)
# typeof(x) == Array{Bool,3}
println(.!x)
# Bool [1 1]
# Bool [1 1]
# Bool [1 1]
# -> makes sense

a = A(x)
# typeof(a) == A
println(.!a)
# Bool [1, 1, 1, 1, 1, 1]
# -> not what I'd expect

```

Thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![MaxPauer](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@MaxPauer](https://discourse.julialang.org/u/MaxPauer)\
**Post date:** [February 18, 2020, 8:08pm UTC](https://discourse.julialang.org/t/broadcast-with-container-for-multidimensional-array/34825/2 "2020-02-18T20:08:59Z")

</div>

Now that I think about it again, the problem seems to be more broadcast related…  
My iterate function does what it is supposed to do: iterate over every element in my array.  
So the real question is: How do i get the broadcast operation for my container type to work like the broadcast on the ‘inner’ type?

EDIT: edited the original question to match my new insights 😃

---

<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:** [February 18, 2020, 9:32pm UTC](https://discourse.julialang.org/t/broadcast-with-container-for-multidimensional-array/34825/3 "2020-02-18T21:32:49Z")

</div>

Essentially the problem is that you’ve not “taught” Julia what the shape of your custom struct is yet. The only thing that Julia knows is that it’s iterable with a one-dimensional length.

Depending upon what you’re doing, you may want to define its size or even subtype `<: AbstractMatrix{Bool}`. Many many more details in the interfaces chapter: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/)

---

<div class="post-metadata">

**Author:** ![MaxPauer](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@MaxPauer](https://discourse.julialang.org/u/MaxPauer)\
**Post date:** [February 19, 2020, 4:56pm UTC](https://discourse.julialang.org/t/broadcast-with-container-for-multidimensional-array/34825/4 "2020-02-19T16:56:25Z")

</div>

Thank you, that was the page I needed, and the function I was missing was `Base.IteratorSize` 🙂
