Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

[ENH] define SectorUnitRange #8

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/GradedUnitRanges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ include("gradedunitrangedual.jl")
include("onetoone.jl")
include("fusion.jl")

include("sectorunitrange.jl")
include("newgradedunitrange.jl")

end
109 changes: 109 additions & 0 deletions src/newgradedunitrange.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

using BlockArrays: BlockArrays

struct NewGradedUnitRange{T,BlockLasts,Sector} <: AbstractGradedUnitRange{T,BlockLasts}
nondual_labels::Vector{Sector}
multiplicity_range::BlockedOneTo{T,BlockLasts} # TBD offset != 1?
isdual::Bool

function NewGradedUnitRange(glabels, grange, gisdual)
return new{eltype(grange),Vector{eltype(grange)},eltype(glabels)}(

Check warning on line 10 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L9-L10

Added lines #L9 - L10 were not covered by tests
glabels, grange, gisdual
)
end
end

#
# Accessors
#
nondual_labels(g::NewGradedUnitRange) = g.nondual_labels
multiplicity_range(g::NewGradedUnitRange) = g.multiplicity_range
isdual(g::NewGradedUnitRange) = g.isdual

Check warning on line 21 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L19-L21

Added lines #L19 - L21 were not covered by tests

#
# GradedUnitRanges interface
#
function GradedUnitRanges.sector_type(

Check warning on line 26 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L26

Added line #L26 was not covered by tests
::Type{<:NewGradedUnitRange{T,BlockLasts,Sector}}
) where {T,BlockLasts,Sector}
return Sector

Check warning on line 29 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L29

Added line #L29 was not covered by tests
end

function blocklabels(g::NewGradedUnitRange)
return isdual(g) ? dual.(nondual_labels(g)) : nondual_labels(g)

Check warning on line 33 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L32-L33

Added lines #L32 - L33 were not covered by tests
end

function dual(g::NewGradedUnitRange)
return NewGradedUnitRange(nondual_labels(g), multiplicity_range(g), !isdual(g))

Check warning on line 37 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L36-L37

Added lines #L36 - L37 were not covered by tests
end

function flip(g::NewGradedUnitRange)
return NewGradedUnitRange(dual.(nondual_labels(g)), multiplicity_range(g), !isdual(g))

Check warning on line 41 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L40-L41

Added lines #L40 - L41 were not covered by tests
end

function space_isequal(g1::NewGradedUnitRange, g2::NewGradedUnitRange)
return nondual_labels(g1) == nondual_labels(g2) &&

Check warning on line 45 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L44-L45

Added lines #L44 - L45 were not covered by tests
blockisequal(multiplicity_range(g1), multiplicity_range(g2)) &&
isdual(g1) == isdual(g2)
end

#
# Base interface
#
Base.length(g::NewGradedUnitRange) = sum(length.(blocks(g)))

Check warning on line 53 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L53

Added line #L53 was not covered by tests

function Base.show(io::IO, g::NewGradedUnitRange)
return print(io, nameof(typeof(g)), blocklabels(g), multiplicity_range(g))

Check warning on line 56 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L55-L56

Added lines #L55 - L56 were not covered by tests
end

function Base.show(io::IO, ::MIME"text/plain", g::NewGradedUnitRange)
print(io, typeof(g))
if isdual(g)
print(io, " dual")

Check warning on line 62 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L59-L62

Added lines #L59 - L62 were not covered by tests
end
println()
return print(io, join(repr.(blocks(g)), '\n'))

Check warning on line 65 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L64-L65

Added lines #L64 - L65 were not covered by tests
end

#
# BlockArrays interface
#
function BlockArrays.mortar(v::Vector{<:SectorUnitRange})
glabels = nondual_sector.(v)
grange = blockedrange(length.(multiplicity_range.(v)))
gisdual = isdual(first(v))

Check warning on line 74 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L71-L74

Added lines #L71 - L74 were not covered by tests
# TODO add checks
return NewGradedUnitRange(glabels, grange, gisdual)

Check warning on line 76 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L76

Added line #L76 was not covered by tests
end

function BlockArrays.blocks(g::NewGradedUnitRange)
return sectorunitrange.(

Check warning on line 80 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L79-L80

Added lines #L79 - L80 were not covered by tests
nondual_labels(g), blocks(multiplicity_range(g)), isdual(g), blockfirsts(g)
)
end

BlockArrays.blocklengths(g::NewGradedUnitRange) = length.(blocks(g))

Check warning on line 85 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L85

Added line #L85 was not covered by tests

function BlockArrays.blockfirsts(g::NewGradedUnitRange)
return vcat([1], blocklasts(g)[begin:(end - 1)])

Check warning on line 88 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L87-L88

Added lines #L87 - L88 were not covered by tests
end

function BlockArrays.blocklasts(g::NewGradedUnitRange)
return cumsum(length.(nondual_labels(g)) .* blocklengths(multiplicity_range(g)))

Check warning on line 92 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L91-L92

Added lines #L91 - L92 were not covered by tests
end

#
# slicing
#
Base.getindex(g::NewGradedUnitRange, b::Block{1}) = blocks(g)[Int(b)]

Check warning on line 98 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L98

Added line #L98 was not covered by tests

function Base.getindex(

Check warning on line 100 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L100

Added line #L100 was not covered by tests
g::NewGradedUnitRange, br::BlockRange{1,R}
) where {R<:Tuple{AbstractUnitRange{Int64}}} # TODO remove ambiguities & use more generic def
return mortar(blocks(g)[Int.(br)])

Check warning on line 103 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L103

Added line #L103 was not covered by tests
end

# TODO replace Tuple with kronecker
function Base.getindex(g::NewGradedUnitRange, bx::Tuple{<:Block{1},<:Any})
return blocks(g)[Int(first(bx))][(:, last(bx))]

Check warning on line 108 in src/newgradedunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/newgradedunitrange.jl#L107-L108

Added lines #L107 - L108 were not covered by tests
end
111 changes: 111 additions & 0 deletions src/sectorunitrange.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

using BlockArrays: BlockArrays

struct SectorUnitRange{T,Sector,Range<:AbstractUnitRange{T}} <: AbstractUnitRange{T}
nondual_sector::Sector
multiplicity_range::Range
isdual::Bool
offset::T

function SectorUnitRange(s, r, b, offset)
return new{eltype(r),typeof(s),typeof(r)}(s, r, b, offset)

Check warning on line 11 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L10-L11

Added lines #L10 - L11 were not covered by tests
end
end

#
# Constructors
#
sectorunitrange(s, r, b=false, offset=1) = SectorUnitRange(s, r, b, offset)

Check warning on line 18 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L18

Added line #L18 was not covered by tests

#
# accessors
#
nondual_sector(sr::SectorUnitRange) = sr.nondual_sector
multiplicity_range(sr::SectorUnitRange) = sr.multiplicity_range
isdual(sr::SectorUnitRange) = sr.isdual
Base.first(sr::SectorUnitRange) = sr.offset

Check warning on line 26 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L23-L26

Added lines #L23 - L26 were not covered by tests

#
# Base interface
#
Base.axes(sr::SectorUnitRange) = Base.oneto(length(sr))

Check warning on line 31 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L31

Added line #L31 was not covered by tests

Base.eachindex(sr::SectorUnitRange) = Base.oneto(length(sr))

Check warning on line 33 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L33

Added line #L33 was not covered by tests

Base.lastindex(sr::SectorUnitRange) = length(sr)

Check warning on line 35 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L35

Added line #L35 was not covered by tests

function Base.length(sr::SectorUnitRange)
return length(nondual_sector(sr)) * length(multiplicity_range(sr))

Check warning on line 38 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L37-L38

Added lines #L37 - L38 were not covered by tests
end # TBD directly quantum_dimension(nondual_sector(sr))?

Base.iterate(sr::SectorUnitRange) = iterate(first(sr):last(sr))
Base.iterate(sr::SectorUnitRange, i::Integer) = iterate(first(sr):last(sr), i)

Check warning on line 42 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L41-L42

Added lines #L41 - L42 were not covered by tests

Base.last(sr::SectorUnitRange) = first(sr) + length(sr) - 1

Check warning on line 44 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L44

Added line #L44 was not covered by tests

# slicing
Base.getindex(sr::SectorUnitRange, i::Integer) = range(sr)[i]
function Base.getindex(sr::SectorUnitRange, r::AbstractUnitRange{T}) where {T<:Integer}
return range(sr)[r]

Check warning on line 49 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L47-L49

Added lines #L47 - L49 were not covered by tests
end

# TODO replace (:,x) indexing with kronecker(:, x)
Base.getindex(sr::SectorUnitRange, t::Tuple{Colon,<:Integer}) = sr[(:, last(t):last(t))]
function Base.getindex(sr::SectorUnitRange, t::Tuple{Colon,<:AbstractUnitRange})
return sectorunitrange(

Check warning on line 55 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L53-L55

Added lines #L53 - L55 were not covered by tests
nondual_sector(sr), multiplicity_range(sr)[last(t)], isdual(sr), first(sr)
)
end

Base.range(sr::SectorUnitRange) = first(sr):last(sr)

Check warning on line 60 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L60

Added line #L60 was not covered by tests

function Base.show(io::IO, sr::SectorUnitRange)
print(io, nameof(typeof(sr)), " ", first(sr), " .+ ")
if isdual(sr)
print(io, "dual(", nondual_sector(sr), ")")

Check warning on line 65 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L62-L65

Added lines #L62 - L65 were not covered by tests
else
print(io, nondual_sector(sr))

Check warning on line 67 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L67

Added line #L67 was not covered by tests
end
return print(io, " => ", multiplicity_range(sr))

Check warning on line 69 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L69

Added line #L69 was not covered by tests
end

#
# GradedUnitRanges interface
#
function blocklabels(sr::SectorUnitRange)
return isdual(sr) ? [dual(nondual_sector(sr))] : [nondual_sector(sr)]

Check warning on line 76 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L75-L76

Added lines #L75 - L76 were not covered by tests
end

function dual(sr::SectorUnitRange)
return sectorunitrange(nondual_sector(sr), multiplicity_range(sr), !isdual(sr), first(sr))

Check warning on line 80 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L79-L80

Added lines #L79 - L80 were not covered by tests
end

function flip(sr::SectorUnitRange)
return sectorunitrange(

Check warning on line 84 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L83-L84

Added lines #L83 - L84 were not covered by tests
dual(nondual_sector(sr)), multiplicity_range(sr), !isdual(sr), first(sr)
)
end

function map_blocklabels(f, sr::SectorUnitRange)
return sectorunitrange(

Check warning on line 90 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L89-L90

Added lines #L89 - L90 were not covered by tests
f(nondual_sector(sr)), multiplicity_range(sr), isdual(sr), first(sr)
)
end

sector_type(::Type{<:SectorUnitRange{T,Sector}}) where {T,Sector} = Sector

Check warning on line 95 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L95

Added line #L95 was not covered by tests

function space_isequal(sr1::SectorUnitRange, sr2::SectorUnitRange)
return nondual_sector(sr1) == nondual_sector(sr2) &&

Check warning on line 98 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L97-L98

Added lines #L97 - L98 were not covered by tests
isdual(sr1) == isdual(sr2) &&
multiplicity_range(sr1) == multiplicity_range(sr2) &&
first(sr1) == first(sr2)
end

#
# BlockArrays interface
#
BlockArrays.blocks(sr::SectorUnitRange) = [sr]

Check warning on line 107 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L107

Added line #L107 was not covered by tests

BlockArrays.blocklength(::SectorUnitRange) = 1

Check warning on line 109 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L109

Added line #L109 was not covered by tests

BlockArrays.blocklengths(sr::SectorUnitRange) = [length(sr)] # TBD return length(multiplicity_range(sr)) ?

Check warning on line 111 in src/sectorunitrange.jl

View check run for this annotation

Codecov / codecov/patch

src/sectorunitrange.jl#L111

Added line #L111 was not covered by tests
81 changes: 81 additions & 0 deletions test/test_newgradedunitrange.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Test: @test, @test_throws, @testset

using BlockArrays: Block, blocklength, blocklengths, blockisequal, blocks, mortar

using GradedUnitRanges:
AbstractGradedUnitRange,
NewGradedUnitRange,
SectorUnitRange,
blocklabels,
dual,
gradedrange,
flip,
isdual,
multiplicity_range,
nondual_sector,
sector_type,
sectorunitrange,
space_isequal
using SymmetrySectors: AbstractSector, SU, quantum_dimension

Base.length(s::AbstractSector) = quantum_dimension(s)

@testset "NewGradedUnitRange" begin
sr1 = sectorunitrange(SU((1, 0)), 1:2)
g1 = mortar([sr1])
@test g1 isa NewGradedUnitRange
@test blocklabels(g1) == [SU((1, 0))]
@test blockisequal(multiplicity_range(g1), blockedrange([2]))
@test !isdual(g1)

@test length(g1) == 6
@test blocklength(g1) == 1
@test blocklengths(g1) == [6]
@test space_isequal(only(blocks(g1)), sr1)

sr2 = sectorunitrange(SU((2, 1)), 3:3, false, 6)
g2 = mortar([sr1, sr2])
@test g2 isa NewGradedUnitRange
@test blocklabels(g2) == [SU((1, 0)), SU((2, 1))]
@test blockisequal(multiplicity_range(g2), blockedrange([2, 1]))
@test !isdual(g2)
@test space_isequal(g2, g2)
@test !space_isequal(g1, g2)

@test length(g2) == 14
@test blocklength(g2) == 2
@test blocklengths(g2) == [6, 8]
@test all(space_isequal.(blocks(g2), [sr1, sr2]))
@test space_isequal(g2[Block(1)], sr1)
@test space_isequal(g2[Block(2)], sr2)

g2b = dual(g2)
@test g2b isa NewGradedUnitRange
@test blocklabels(g2b) == [SU((1, 1)), SU((2, 1))]
@test blockisequal(multiplicity_range(g2b), blockedrange([2, 1]))
@test isdual(g2b)

g2f = flip(g2)
@test g2f isa NewGradedUnitRange
@test blocklabels(g2f) == [SU((1, 0)), SU((2, 1))]
@test blockisequal(multiplicity_range(g2f), blockedrange([2, 1]))
@test isdual(g2f)

@test blockfirsts(g2) == first.(blocks(g2))
@test blocklasts(g2) == [6, 14]
@test space_isequal(mortar(blocks(g2)), g2)

# slicing
@test space_isequal(g2[Block.(1:2)], g2)
@test space_isequal(g2[Block.(1:2)], g2)

@test space_isequal(g2[(Block(1), 1)], sectorunitrange(SU((1, 0)), 1:1, false, 1))
@test space_isequal(g2[(Block(1), 2)], sectorunitrange(SU((1, 0)), 2:2, false, 1))
@test space_isequal(g2[(Block(1), 1:2)], sectorunitrange(SU((1, 0)), 1:2, false, 1))
@test_throws BoundsError g2[(Block(1), 3)]

@test space_isequal(g2[(Block(2), 1)], sectorunitrange(SU((2, 1)), 3:3, false, 6))
@test space_isequal(g2[(Block(2), 1:1)], sectorunitrange(SU((2, 1)), 3:3, false, 6))
@test_throws BoundsError g2[(Block(2), 2)]
@test_throws BoundsError g2[(Block(2), 3:3)] # misleading?
end
Loading
Loading