Skip to content

Commit 49ac174

Browse files
authored
Fix ambiguity error in randn(2)[namedoneto(2, "i")] (#41)
1 parent bcd7edd commit 49ac174

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NamedDimsArrays"
22
uuid = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.4.3"
4+
version = "0.4.4"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/abstractnameddimsarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ end
523523
function Base.getindex(a::AbstractArray, I1::NamedViewIndex, Irest::NamedViewIndex...)
524524
return copy(view(a, I1, Irest...))
525525
end
526+
# Disambiguate from `Base.getindex(A::Array, I::AbstractUnitRange{<:Integer})`.
527+
function Base.getindex(a::Array, I1::AbstractNamedUnitRange{<:Integer})
528+
return copy(view(a, I1))
529+
end
526530

527531
function Base.view(a::AbstractArray, I1::Name, Irest::Name...)
528532
return nameddimsarray(a, name.((I1, Irest...)))

test/test_basics.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ using Test: @test, @test_throws, @testset
6868
@test dims(na, ("j", "i")) == (2, 1)
6969
@test na[1, 1] == a[1, 1]
7070

71+
a = randn(elt, 3, 4)
72+
na = nameddimsarray(a, ("i", "j"))
7173
for na′ in (
7274
similar(na, Float32, (j, i)),
7375
similar(na, Float32, NaiveOrderedSet((j, i))),
@@ -83,6 +85,8 @@ using Test: @test, @test_throws, @testset
8385
@test na′ na
8486
end
8587

88+
a = randn(elt, 3, 4)
89+
na = nameddimsarray(a, ("i", "j"))
8690
for na′ in (
8791
similar(na, (j, i)),
8892
similar(na, NaiveOrderedSet((j, i))),
@@ -114,8 +118,19 @@ using Test: @test, @test_throws, @testset
114118
@test size(na, i) == si
115119
@test size(na, j) == sj
116120

121+
# Regression test for ambiguity error with
122+
# `Base.getindex(A::Array, I::AbstractUnitRange{<:Integer})`.
123+
i = namedoneto(2, "i")
124+
a = randn(elt, 2)
125+
na = a[i]
126+
@test na isa NamedDimsArray{elt}
127+
@test dimnames(na) == ("i",)
128+
@test dename(na) == a
129+
117130
# aliasing
118-
a′ = randn(2, 2)
131+
a′ = randn(elt, 2, 2)
132+
i = Name("i")
133+
j = Name("j")
119134
a′ij = @view a′[i, j]
120135
a′ij[i[1], j[2]] = 12
121136
@test a′ij[i[1], j[2]] == 12
@@ -126,7 +141,9 @@ using Test: @test, @test_throws, @testset
126141
@test a′ij[i[2], j[1]] == 21
127142
@test a′[2, 1] == 21
128143

129-
a′ = randn(2, 2)
144+
a′ = randn(elt, 2, 2)
145+
i = Name("i")
146+
j = Name("j")
130147
a′ij = a′[i, j]
131148
a′ij[i[1], j[2]] = 12
132149
@test a′ij[i[1], j[2]] == 12
@@ -137,6 +154,8 @@ using Test: @test, @test_throws, @testset
137154
@test a′ij[i[2], j[1]] 21
138155
@test a′[2, 1] 21
139156

157+
a = randn(elt, 3, 4)
158+
na = nameddimsarray(a, ("i", "j"))
140159
a′ = dename(na)
141160
@test a′ isa Matrix{elt}
142161
@test a′ == a

0 commit comments

Comments
 (0)