# Yota.jl getindex error

**URL:** https://discourse.julialang.org/t/yota-jl-getindex-error/54595
**Category:** General Usage
**Created:** [February 4, 2021, 6:28am UTC](https://discourse.julialang.org/t/yota-jl-getindex-error/54595 "2021-02-04T06:28:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DeepQ](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@DeepQ](https://discourse.julialang.org/u/DeepQ)
#### Post date: [February 4, 2021, 6:28am UTC](https://discourse.julialang.org/t/yota-jl-getindex-error/54595/1 "2021-02-04T06:28:21Z")

</div>

I am trying to calculate the gradient of this function:

```julia
using Yota 

f(x) = sum(getindex(x, [1 3; 4 2]))
s = rand(2,2)
val, g = Yota.grad(f, s)

```

If s = rand(2) my code works but the above code returns this error:

```julia

> Failed to find a derivative for %11 = hvcat(%6, %7, %8, %9, %10) ::Matrix{Int64} at position one

```

Why isn’t multidimensional indexing supported by default? Should I use @diffrule macro?  
I edited my post.

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [February 4, 2021, 10:21am UTC](https://discourse.julialang.org/t/yota-jl-getindex-error/54595/2 "2021-02-04T10:21:17Z")

</div>

What version of Julia are you using? I can’t run your function on Julia 1.5.0:

```julia
ulia> using Yota

julia> f(x) = sum(exp.(x), getindex(x, [1 3; 4 2]))
f (generic function with 1 method)

julia> s = rand(2,2)
2×2 Array{Float64,2}:
 0.235071 0.271145
 0.994342 0.97109

julia> f(x) = sum(exp.(x), getindex(x, [1 3; 4 2]))
f (generic function with 1 method)

julia> f(s)
ERROR: MethodError: objects of type Array{Float64,2} are not callable
Use square brackets [] for indexing an Array

```

Most likely this issue is not related to `getindex()`, but to `sum()`. I _guess_ you try to use this method of `sum()`:

```julia
sum(A::AbstractArray, w::StatsBase.UnitWeights; dims)

```

Which we don’t have a diff rule for. Is it the case?

---

<div class="post-metadata">

### Author: ![DeepQ](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@DeepQ](https://discourse.julialang.org/u/DeepQ)
#### Post date: [February 4, 2021, 10:41am UTC](https://discourse.julialang.org/t/yota-jl-getindex-error/54595/3 "2021-02-04T10:41:28Z")

</div>

No. If I use these functions I still see the same error:

```julia
f2(x) = sum(getindex(exp.(x), [1 3; 4 2]))
f3(x) = sum(exp.(getindex(x, [1 3; 4 2]))

```

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [February 4, 2021, 11:36am UTC](https://discourse.julialang.org/t/yota-jl-getindex-error/54595/4 "2021-02-04T11:36:42Z")

</div>

Ok, with Julia 1.5.0 and Yota 0.4.4 I see another error related to the [recent changes](https://github.com/dfdx/Yota.jl/pull/74) in `getindex()`:

> ERROR: BoundsError: attempt to access 1×2×2 Array{Float64,3} at index [1:1, 4, 1]

The changes were needed to support the case with repeating indices, e.g. `getindex(x, [1, 2, 1])` (see [related issue](https://github.com/FluxML/Zygote.jl/issues/821) in Zygote if you’re curious about the whole story), but implementation is bit of a hack, so we get what we get.

I see 2 options for you here:

1. If you don’t plan to use repeating indices, just use Yota@0.4.2 - I just checked your `f2()` and `f3()` on it and they work perfectly fine.
2. If your code may have repeating indices, you will have to wait for [this PR](https://github.com/FluxML/NNlib.jl/pull/255) in NNlib, which I will then use to replace my hacky implementation of scatter operations.
