From e3640fc34c83c05de9a0717c4b0365b61a1a50f2 Mon Sep 17 00:00:00 2001 From: Will Tebbutt Date: Wed, 20 Mar 2019 18:26:50 +0000 Subject: [PATCH] Fix indexing + add regression test (#140) --- src/sensitivities/indexing.jl | 2 +- test/sensitivities/indexing.jl | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/sensitivities/indexing.jl b/src/sensitivities/indexing.jl index b49afa04..6e4791cb 100644 --- a/src/sensitivities/indexing.jl +++ b/src/sensitivities/indexing.jl @@ -11,7 +11,7 @@ function ∇(Ā, ::typeof(getindex), ::Type{Arg{1}}, p, y, ȳ, A, inds...) return Ā end function ∇(Ā, ::typeof(getindex), ::Type{Arg{1}}, p, y::AbstractArray, ȳ::AbstractArray, A, inds...) - Ā[inds...] .+= reshape(ȳ, size(y)...) + @views Ā[inds...] .+= reshape(ȳ, size(y)...) return Ā end function ∇(::typeof(getindex), ::Type{Arg{1}}, p, y, ȳ, A, inds...) diff --git a/test/sensitivities/indexing.jl b/test/sensitivities/indexing.jl index 498b3548..75c60943 100644 --- a/test/sensitivities/indexing.jl +++ b/test/sensitivities/indexing.jl @@ -1,15 +1,22 @@ @testset "Indexing" begin - let + @testset "Int" begin leaf = Leaf(Tape(), 5 * [1, 1, 1, 1, 1]) y = getindex(leaf, 1) @test unbox(y) == 5 @test ∇(y, one(unbox(y)))[leaf] == [1, 0, 0, 0, 0] end - let + @testset "Vector" begin x = Leaf(Tape(), 10 * [1, 1, 1]) y = x[2:3] @test unbox(y) == [10, 10] @test ∇(y, oneslike(unbox(y)))[x] == [0, 1, 1] end + + @testset "Overlapping indices (#139)" begin + x = Leaf(Tape(), 10 * [1, 1, 1]) + y = x[[2, 3, 3]] + @test unbox(y) == [10, 10, 10] + @test ∇(y, oneslike(unbox(y)))[x] == [0, 1, 2] + end end