# Broadcasting on views

**URL:** <https://discourse.julialang.org/t/broadcasting-on-views/118459>\
**Category:** General Usage\
**Tags:** broadcast, views\
**Created:** [August 21, 2024, 2:53pm UTC](https://discourse.julialang.org/t/broadcasting-on-views/118459 "2024-08-21T14:53:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pratikvn](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pratikvn](https://discourse.julialang.org/u/pratikvn)\
**Post date:** [August 21, 2024, 2:53pm UTC](https://discourse.julialang.org/t/broadcasting-on-views/118459/1 "2024-08-21T14:53:40Z")

</div>

Hello,

I would like to perform a broadcast operation on views with indices stored in a struct. I am not sure how exactly to acheive this with Julia syntax.

Essentially I have a struct like so:

```julia
struct Foo
     id::Integer
     work::AbstractArray
end

```

where `work` contains a list of indices, and a vector of type Foo, `bar = [Foo(1, [1,2]), Foo(2, [3,4])]`.

I would like to perform a broadcasting `dot` operation for example, but with indices from `bar`. So essentially, I would like to iterate through the array of `Foo` and create views for two vecs using the indices `work`.

```julia
out = broadcast(dot, view(vec1, ?bar.work), view(vec2, ?bar.work))

```

I know I can do this with a for loop:

```julia
out = zeros(Float, size(Bar))
idx=1
for i in eachindex(bar)
    out[idx] = dot(view(vec1, i.work), view(vec2, i.work)) 
    idx++
end

```

but I think this would be more efficient and scalable with a broadcast, as for me the indices are guaranteed to be indepedent.

---

<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:** [August 21, 2024, 3:17pm UTC](https://discourse.julialang.org/t/broadcasting-on-views/118459/2 "2024-08-21T15:17:26Z")

</div>

> [@pratikvn](#):
>
> but I think this would be more efficient and scalable with a broadcast,

`broadcast` is not more efficient than a ([properly](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-tips)) handwritten loop in Julia. Loops in Julia are fast.

I’m a little confused by your code, because your example loop doesn’t seem functional (e.g. what is `i.work` where `i` is an index?). But if I understand you correctly, you want something like:

```julia
out = dot.(view.(Ref(vec1), getproperty.(bar, :work)),
           view.(Ref(vec2), getproperty.(bar, :work)))

```

though honestly it seems cleaner to me to use a comprehension:

```julia
@views out = [dot(vec1[foo.work], vec2[foo.work]) for foo in bar]

```

---

<div class="post-metadata">

**Author:** ![pratikvn](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pratikvn](https://discourse.julialang.org/u/pratikvn)\
**Post date:** [August 22, 2024, 11:22am UTC](https://discourse.julialang.org/t/broadcasting-on-views/118459/3 "2024-08-22T11:22:45Z")

</div>

The `i.work` was a misunderstanding on my part of Julia syntax (I assumed that `i` was of type `Foo` rather than just an index). But it seems like you are right. Atleast for this problem, a regular for loop (with pre-allocation) seems to be the way to go.

If I wrap that into a function, then I think it should be acceptable for me. Thanks for your help!
