# Interaction between broadcasting and custom indexing

**URL:** <https://discourse.julialang.org/t/interaction-between-broadcasting-and-custom-indexing/3506>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [May 3, 2017, 11:12pm UTC](https://discourse.julialang.org/t/interaction-between-broadcasting-and-custom-indexing/3506 "2017-05-03T23:12:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [May 3, 2017, 11:12pm UTC](https://discourse.julialang.org/t/interaction-between-broadcasting-and-custom-indexing/3506/1 "2017-05-03T23:12:47Z")

</div>

Hi,

I have some weird interaction between broadcasting and custom indices for a type. This is a minimal example.

Say I have this type, which holds data in a matrix format

```julia
struct Foo <: AbstractMatrix{Float64}
    data::Matrix{Float64}
    nm::Tuple{Int, Int}
    Foo(data) = new(data, size(data))
end

```

and I then implement the custom indexing behaviour I need using Cartesian indices

```julia

# reflect indices
@inline mapindices(k::Int, j::Int) = 
    (ifelse(k≥0, k, -k)+1, ifelse(j≥0, j, -j)+1)

# ~~cartesian indexing~~
@noinline function Base.getindex(f::Foo, k::Int, j::Int)
    f.data[mapindices(k, j)...]
end

@noinline function Base.setindex!(f::Foo, val, k::Int, j::Int)
    f.data[mapindices(k, j)...] = val
    val
end

```

In some cases, however, I prefer to use linear indices for performance, so I define:

```julia
# ~~linear indexing~~
@noinline function Base.getindex(f::Foo, i::Int)
    f.data[i]
end

@noinline function Base.setindex!(f::Foo, val, i::Int)
    f.data[i] = val
    val
end

```

I then implement `linearindices` and `IndexStyle`, so that by default the `IndexLinear` behaviour is used, over the indices of the underlying data, while `indices` allows bounds checking when the special cartesian indexing I have defined above is used.

```julia
function Base.indices(f::Foo)
    n, m = f.nm 
    return -n+1:n-1, -m+1:m-1
end
Base.linearindices(f::Foo) = linearindices(f.data)

# tell broadcast to use fast linear ndexing
Base.IndexStyle(::Type{Foo}) = IndexLinear()

```

Here is where my issue arises. I might not be understanding completely how indexing and broadcast interact, so here is the problem.

```julia
# ~~~tests~~~
# this represents the data
# 4 3 4
# 2 1 2
# 4 3 4
u = Foo([1 2; 
         3 4])
# this is what the `mapindices` function above does, it just mirrors the data.

# here is some other data
b = Foo([1 2; 
         3 4])

# this is what it looks like
display(u); println()
@show indices(u)
@show linearindices(u)
@show IndexStyle(u)

```

Now, when I use the new dot syntax in v0.6, I want broadcast to use the fast linear indexing, to loop over the underlying data efficiently. However

```julia
# use broadcast
u .= u .+ b

# results in
# ERROR: LoadError: BoundsError: attempt to access 2×2 Array{Float64,2} at index [5]

```

where it seems to me that it is trying to access the underlying data at index 5, ignoring my definition of `linearindices`.

Any help is welcome.

Davide

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [May 4, 2017, 10:01pm UTC](https://discourse.julialang.org/t/interaction-between-broadcasting-and-custom-indexing/3506/2 "2017-05-04T22:01:38Z")

</div>

Bump.

This is a show stopper at the moment for me. Has anyone successfully integrated broadcasting behaviour and custom indices?
