Skip to content

Commit 3b7447b

Browse files
authored
define checkspaces and findfirstblock_sector (#47)
1 parent b971b51 commit 3b7447b

5 files changed

Lines changed: 38 additions & 1 deletion

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GradedArrays"
22
uuid = "bc96ca6e-b7c8-4bb6-888e-c93f838762c2"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.4.13"
4+
version = "0.4.14"
55

66
[deps]
77
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"

src/abstractsector.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ end
7676

7777
# ================================ GradedUnitRanges interface ==================================
7878
sector_type(S::Type{<:AbstractSector}) = S
79+
80+
function findfirstblock(g::AbstractGradedUnitRange, s::AbstractSector)
81+
return findfirstblock_sector(g::AbstractGradedUnitRange, s)
82+
end

src/gradedunitrange.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ function gradedunitrange_getindices(::NotAbelianStyle, g::AbstractUnitRange, ind
149149
return blockedunitrange_getindices(ungrade(g), indices)
150150
end
151151

152+
# do not overload BlockArrays.findblock to avoid ambiguity for findblock(g, 1)
153+
function findfirstblock_sector(g::AbstractGradedUnitRange, s)
154+
i = findfirst(==(s), sectors(g))
155+
isnothing(i) && return nothing
156+
return Block(i)
157+
end
158+
152159
# ================================== Base interface ======================================
153160

154161
# needed in BlockSparseArrays

src/gradedunitrange_interface.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,14 @@ sectors(v::AbstractBlockVector) = mapreduce(sectors, vcat, blocks(v))
5454
function space_isequal(a1::AbstractUnitRange, a2::AbstractUnitRange)
5555
return (isdual(a1) == isdual(a2)) && sectors(a1) == sectors(a2) && blockisequal(a1, a2)
5656
end
57+
58+
function checkspaces(::Type{Bool}, axes1, axes2)
59+
return length(axes1) == length(axes2) && all(space_isequal.(axes1, axes2))
60+
end
61+
62+
function checkspaces(ax1, ax2)
63+
return checkspaces(Bool, ax1, ax2) || throw(ArgumentError(lazy"$ax1 does not match $ax2"))
64+
end
65+
66+
checkspaces_dual(::Type{Bool}, axes1, axes2) = checkspaces(Bool, axes1, dual.(axes2))
67+
checkspaces_dual(axes1, axes2) = checkspaces(axes1, dual.(axes2))

test/test_gradedunitrange.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ using GradedArrays:
2121
SectorOneTo,
2222
SectorUnitRange,
2323
SU,
24+
checkspaces,
25+
checkspaces_dual,
2426
dual,
27+
findfirstblock,
28+
findfirstblock_sector,
2529
flip,
2630
gradedrange,
2731
isdual,
@@ -96,6 +100,8 @@ using Test: @test, @test_throws, @testset
96100
@test findblock(g, 2) == Block(1)
97101
@test findblock(g, 3) == Block(2)
98102
@test findblockindex(g, 3) == Block(2)[1]
103+
@test findfirstblock_sector(g, "y") == Block(2)
104+
@test isnothing(findfirstblock_sector(g, "a"))
99105

100106
@test axes(Base.Slice(g)) isa Tuple{typeof(g)}
101107
@test AbstractUnitRange{Int}(g) == 1:7
@@ -205,8 +211,15 @@ using Test: @test, @test_throws, @testset
205211
@test space_isequal(only(axes(a)), gradedrange(["y" => 6, "x" => 2]; isdual=isdual(g)))
206212
end
207213

214+
@test checkspaces(Bool, (g1, g1d), (g1, g1d))
215+
208216
@test space_isequal(g1d, dual(g1))
209217
@test space_isequal(dual(g1d), g1)
218+
@test checkspaces((g1, g1d), (g1, g1d))
219+
@test checkspaces_dual(Bool, (g1, g1d), (g1d, g1))
220+
@test checkspaces_dual((g1, g1d), (g1d, g1))
221+
@test_throws ArgumentError checkspaces((g1, g1), (g1, g1d))
222+
@test_throws ArgumentError checkspaces_dual((g1, g1), (g1, g1d))
210223

211224
for a in (
212225
combine_blockaxes(g1, b0),
@@ -274,6 +287,8 @@ end
274287
@test g[4] == 4
275288
@test g[Block(1)[1]] == 1
276289
@test g[Block(2)[1]] == 3
290+
@test findfirstblock(g, SU((1, 0))) == Block(2)
291+
@test isnothing(findfirstblock(g, SU((2, 0))))
277292

278293
# Non-abelian slicing operations
279294
a = g[2:4]

0 commit comments

Comments
 (0)