Skip to content

Commit 9ff937e

Browse files
authored
Index CartesionProductUnitRange with CartesianPair (#26)
1 parent fd48bed commit 9ff937e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "KroneckerArrays"
22
uuid = "05d0b138-81bc-4ff7-84be-08becefb1ccc"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.1.19"
4+
version = "0.1.20"
55

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

src/cartesianproduct.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ function Base.checkindex(::Type{Bool}, inds::CartesianProductUnitRange, i::Carte
9494
return checkindex(Bool, arg1(inds), arg1(i)) && checkindex(Bool, arg2(inds), arg2(i))
9595
end
9696

97+
# Reverse map from CartesianPair to linear index in the range.
98+
function Base.getindex(inds::CartesianProductUnitRange, i::CartesianPair)
99+
i′ = (findfirst(==(arg1(i)), arg1(inds)), findfirst(==(arg2(i)), arg2(inds)))
100+
return inds[LinearIndices((length(arg1(inds)), length(arg2(inds))))[i′...]]
101+
end
102+
97103
using Base.Broadcast: DefaultArrayStyle
98104
for f in (:+, :-)
99105
@eval begin

test/test_basics.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ elts = (Float32, Float64, ComplexF32, ComplexF64)
3636
@test length(r) == 6
3737
@test first(r) == 1
3838
@test last(r) == 6
39+
@test r[1 × 1] == 1
40+
@test r[2 × 1] == 2
41+
@test r[1 × 2] == 3
42+
@test r[2 × 2] == 4
43+
@test r[1 × 3] == 5
44+
@test r[2 × 3] == 6
3945

4046
r = @constinferred(cartesianrange(2 × 3, 2:7))
4147
@test r === cartesianrange(Base.OneTo(2) × Base.OneTo(3), 2:7)
@@ -44,6 +50,12 @@ elts = (Float32, Float64, ComplexF32, ComplexF64)
4450
@test length(r) == 6
4551
@test first(r) == 2
4652
@test last(r) == 7
53+
@test r[1 × 1] == 2
54+
@test r[2 × 1] == 3
55+
@test r[1 × 2] == 4
56+
@test r[2 × 2] == 5
57+
@test r[1 × 3] == 6
58+
@test r[2 × 3] == 7
4759

4860
# Test high-dimensional materialization.
4961
a = randn(elt, 2, 2, 2) randn(elt, 2, 2, 2)

0 commit comments

Comments
 (0)